Skip to content

Commit

Permalink
s2: Fix "better" out of bounds read. (#291)
Browse files Browse the repository at this point in the history
Fixes #290
  • Loading branch information
klauspost authored Oct 28, 2020
1 parent 7b0dd51 commit 456984a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions s2/encode_better.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ func encodeBlockBetter(dst, src []byte) (d int) {
if offset > 65535 && s-base <= 5 {
// Bail if the match is equal or worse to the encoding.
s = base + 3
if s >= sLimit {
goto emitRemainder
}
cv = load64(src, s)
continue
}
Expand Down
16 changes: 15 additions & 1 deletion s2/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ func TestEncoderRegression(t *testing.T) {
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
return
}
comp = EncodeBetter(make([]byte, MaxEncodedLen(len(data))), data)
decoded, err = Decode(nil, comp)
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(data, decoded) {
t.Error("block decoder mismatch")
return
}
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
return
}
// Test writer and use "better":
var buf bytes.Buffer
encBetter.Reset(&buf)
Expand Down Expand Up @@ -131,7 +145,7 @@ func TestEncoderRegression(t *testing.T) {
t.Error(err)
return
}
test(t, b)
test(t, b[:len(b):len(b)])
})
}
}
Expand Down
Binary file modified s2/testdata/enc_regressions.zip
Binary file not shown.

0 comments on commit 456984a

Please sign in to comment.