Skip to content

Commit a5e3a22

Browse files
bindings/go: Fix bug with incomplete reads (#176)
Using Reader.read doesn't guarantee that the buffer passed will be filled if enough data is available, and the buffer has enough space. This meant that sometimes an initial Read call would not suffice for large Document data.
1 parent 9a03eb7 commit a5e3a22

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

bindings/go/scip/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (pi *IndexVisitor) ParseStreaming(r io.Reader) error {
7070
}
7171
// Keep going when len == 0 instead of short-circuiting to preserve empty sub-messages
7272
if dataLen > 0 {
73-
numRead, err := r.Read(dataBuf)
73+
numRead, err := io.ReadAtLeast(r, dataBuf, dataLen)
7474
if err != nil {
7575
return errors.Wrapf(err, "failed to read data for %s", indexFieldName(fieldNumber))
7676
}

bindings/go/scip/parse_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package scip
22

33
import (
44
"bytes"
5+
"compress/gzip"
6+
"os"
57
"regexp"
68
"testing"
79

@@ -46,3 +48,31 @@ func TestFuzz(t *testing.T) {
4648
}
4749
}
4850
}
51+
52+
func TestLargeDocuments(t *testing.T) {
53+
// Copied from the Sourcegraph monorepo, which triggered a bug
54+
// where Reader.read() didn't actually fill a buffer completely,
55+
// due to the presence of large documents.
56+
gzipped, err := os.Open("./testdata/index1.scip.gz")
57+
if err != nil {
58+
t.Fatalf("unexpected error reading test file: %s", err)
59+
}
60+
reader, err := gzip.NewReader(gzipped)
61+
if err != nil {
62+
t.Fatalf("unexpected error unzipping test file: %s", err)
63+
}
64+
65+
parsedIndex := Index{}
66+
67+
indexVisitor := IndexVisitor{func(metadata *Metadata) {
68+
parsedIndex.Metadata = metadata
69+
}, func(document *Document) {
70+
parsedIndex.Documents = append(parsedIndex.Documents, document)
71+
}, func(extSym *SymbolInformation) {
72+
parsedIndex.ExternalSymbols = append(parsedIndex.ExternalSymbols, extSym)
73+
}}
74+
75+
if err := indexVisitor.ParseStreaming(reader); err != nil {
76+
t.Fatalf("got error parsing index %v", err)
77+
}
78+
}
301 KB
Binary file not shown.

0 commit comments

Comments
 (0)