mirror of
https://github.com/golang/go.git
synced 2026-06-27 11:20:41 +00:00
index/suffixarray: protect against another data corruption
Suffix array indices x must be in range 0 <= x < len(data). Make sure this is the case when reading them from a file. Trivial and cheap fix. Found during fly-by reading of the code. While at it, also fix format string in previous test. Change-Id: I64ed31a10ca6bf103fee5b572597f4ab02a0423f Reviewed-on: https://go-review.googlesource.com/c/go/+/771782 Reviewed-by: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
This commit is contained in:
parent
2098279730
commit
e929fb78e4
2 changed files with 24 additions and 3 deletions
|
|
@ -143,9 +143,11 @@ func readSlice(r io.Reader, buf []byte, data ints) (n int, err error) {
|
|||
len := data.len()
|
||||
for p := binary.MaxVarintLen64; p < size; n++ {
|
||||
x, w := binary.Uvarint(buf[p:])
|
||||
// prevent index-out-of-bounds panic if there are more indices than expected
|
||||
// - prevent index-out-of-bounds panic if there are more indices than expected
|
||||
// (was go.dev/issue/53352)
|
||||
if n >= len {
|
||||
// - prevent index-out-of-bounds panic in a future Lookup
|
||||
// by ensuring all indices x satisfy x < len
|
||||
if n >= len || x >= uint64(len) {
|
||||
return n, errCorrupted
|
||||
}
|
||||
data.set(n, int64(x))
|
||||
|
|
|
|||
|
|
@ -659,6 +659,25 @@ func TestIssue53352(t *testing.T) {
|
|||
// Reading back the corrupted encoding should report an error.
|
||||
// Before fixing go.dev/issue/53352, this resulted in an index-out-of-range panic.
|
||||
if err := index.Read(bytes.NewBuffer(encoding)); err != errCorrupted {
|
||||
t.Fatalf("got %q; want %q", err, errCorrupted)
|
||||
t.Fatalf("got %v; want %v", err, errCorrupted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexCorruption(t *testing.T) {
|
||||
data := []byte("x")
|
||||
index := New(data)
|
||||
var buf bytes.Buffer
|
||||
if err := index.Write(&buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// The index data is at the end of the encoding (see TestIssue53352 for the encoding format).
|
||||
// The encoded indices i must be 0 <= i < len(data) because they are used to index into the
|
||||
// data. Corrupt one and make sure we get an error when reading the encoding.
|
||||
buf.Bytes()[buf.Len()-1] = byte(len(data))
|
||||
|
||||
// Reading back the corrupted encoding should report an error.
|
||||
if err := index.Read(&buf); err != errCorrupted {
|
||||
t.Fatalf("got %v; want %v", err, errCorrupted)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue