[go] index/suffixarray: protect against another data corruption

0 views
Skip to first unread message

Robert Griesemer (Gerrit)

unread,
Apr 28, 2026, 7:00:13 PM (10 hours ago) Apr 28
to goph...@pubsubhelper.golang.org, Robert Griesemer, golang-co...@googlegroups.com

Robert Griesemer has uploaded the change for review

Commit message

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.
Change-Id: I64ed31a10ca6bf103fee5b572597f4ab02a0423f

Change diff

diff --git a/src/index/suffixarray/suffixarray.go b/src/index/suffixarray/suffixarray.go
index 5541f09..4fced50 100644
--- a/src/index/suffixarray/suffixarray.go
+++ b/src/index/suffixarray/suffixarray.go
@@ -143,9 +143,11 @@
len := data.len()
for p := binary.MaxVarintLen64; p < size; n++ {
x, w := binary.Uvarint(buf[p:])
- // prevent index-out-of-bounds error if data is corrupted
+ // - prevent index-out-of-bounds error if data is corrupted
// (was go.dev/issue/53352)
- if n >= len {
+ // - prevent index-out-of-bounds error 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))
diff --git a/src/index/suffixarray/suffixarray_test.go b/src/index/suffixarray/suffixarray_test.go
index 410f9e8..b2f2787 100644
--- a/src/index/suffixarray/suffixarray_test.go
+++ b/src/index/suffixarray/suffixarray_test.go
@@ -659,6 +659,24 @@
// 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) {
+ index := New([]byte("x"))
+ 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] = 100
+
+ // Reading back the corrupted encoding should report an error.
+ if err := index.Read(&buf); err != errCorrupted {
+ t.Fatalf("got %v; want %v", err, errCorrupted)
}
}

Change information

Files:
  • M src/index/suffixarray/suffixarray.go
  • M src/index/suffixarray/suffixarray_test.go
Change size: S
Delta: 2 files changed, 23 insertions(+), 3 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I64ed31a10ca6bf103fee5b572597f4ab02a0423f
Gerrit-Change-Number: 771782
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Griesemer <g...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Apr 28, 2026, 7:03:59 PM (10 hours ago) Apr 28
to Robert Griesemer, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Robert Griesemer uploaded new patchset

Robert Griesemer uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I64ed31a10ca6bf103fee5b572597f4ab02a0423f
Gerrit-Change-Number: 771782
Gerrit-PatchSet: 2
Gerrit-Owner: Robert Griesemer <g...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Apr 28, 2026, 7:04:42 PM (10 hours ago) Apr 28
to Robert Griesemer, goph...@pubsubhelper.golang.org, Mark Freeman, golang-co...@googlegroups.com
Attention needed from Mark Freeman and Robert Griesemer

Robert Griesemer voted

Auto-Submit+1
Code-Review+2
Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Mark Freeman
  • Robert Griesemer
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I64ed31a10ca6bf103fee5b572597f4ab02a0423f
Gerrit-Change-Number: 771782
Gerrit-PatchSet: 2
Gerrit-Owner: Robert Griesemer <g...@golang.org>
Gerrit-Reviewer: Mark Freeman <markf...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Mark Freeman <markf...@google.com>
Gerrit-Comment-Date: Tue, 28 Apr 2026 23:04:36 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages