Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

s2: Fix "better" out of bounds read. #291

Merged
merged 1 commit into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it be a good idea to restrict the capacity of src in EncodeBetter, too? (And maybe other places?)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greatroar That is exactly what this is doing. The test function tests both Encode and EncodeBetter using the data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean in the actual code, not in the test.

})
}
}
Expand Down
Binary file modified s2/testdata/enc_regressions.zip
Binary file not shown.