Skip to content

Commit

Permalink
Place bounds check within memUvarintReader's ReadUvarint
Browse files Browse the repository at this point in the history
This bounds check is to return an empty uint64 while
decoding a 64-bit integer, should the reader have already
read all the bytes available.

For: blevesearch/bleve#1651

```
Some context on the memUvarintReader ..

The code here reflects ReadUVarint(..) from
https://cs.opensource.google/go/go/+/refs/tags/go1.13.7:src/encoding/binary/varint.go

But as reported here: golang/go#40618,
it appears it contained an error prone path that was addressed in 1.15.
```
  • Loading branch information
abhinavdangeti committed Feb 22, 2022
1 parent 206e6c2 commit 7c9dd9f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion memuvarint.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ func (r *memUvarintReader) ReadUvarint() (uint64, error) {
var C = r.C
var S = r.S

if C >= len(S) {
// nothing else to read
return 0, nil
}

for {
b := S[C]
C++
Expand All @@ -59,7 +64,7 @@ func (r *memUvarintReader) ReadUvarint() (uint64, error) {
// why the "extra" >= check? The normal case is that s <
// 63, so we check this single >= guard first so that we
// hit the normal, nil-error return pathway sooner.
if s >= 63 && (s > 63 || s == 63 && b > 1) {
if s >= 63 && (s > 63 || b > 1) {
return 0, fmt.Errorf("memUvarintReader overflow")
}

Expand All @@ -74,6 +79,10 @@ func (r *memUvarintReader) ReadUvarint() (uint64, error) {
// SkipUvarint skips ahead one encoded uint64.
func (r *memUvarintReader) SkipUvarint() {
for {
if r.C >= len(r.S) {
return
}

b := r.S[r.C]
r.C++

Expand Down

0 comments on commit 7c9dd9f

Please sign in to comment.