summaryrefslogtreecommitdiff
path: root/src/encoding/binary/varint_test.go
diff options
context:
space:
mode:
authorKatie Hockman <katie@golang.org>2020-08-06 13:10:42 -0400
committerKatie Hockman <katie@golang.org>2020-08-06 13:10:52 -0400
commit2bc8d90fa21e9547aeb0f0ae775107dc8e05dc0a (patch)
treeefd528fea33c766e196a0d88c5e6533e7003f832 /src/encoding/binary/varint_test.go
parent5ebdfd905d8f2d70601aa11212d579fa60abdb0f (diff)
parente71b61180aa19a60c23b3b7e3f6586726ebe4fd1 (diff)
downloadgo-git-release-branch.go1.13.tar.gz
[release-branch.go1.13] all: merge release-branch.go1.13-security into release-branch.go1.13release-branch.go1.13
Change-Id: Ib8a4c5f58de71145525ba23562bf8a0c35b60c9c
Diffstat (limited to 'src/encoding/binary/varint_test.go')
-rw-r--r--src/encoding/binary/varint_test.go18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/encoding/binary/varint_test.go b/src/encoding/binary/varint_test.go
index ca411ecbd6..6ef4c99505 100644
--- a/src/encoding/binary/varint_test.go
+++ b/src/encoding/binary/varint_test.go
@@ -121,21 +121,27 @@ func TestBufferTooSmall(t *testing.T) {
}
}
-func testOverflow(t *testing.T, buf []byte, n0 int, err0 error) {
+func testOverflow(t *testing.T, buf []byte, x0 uint64, n0 int, err0 error) {
x, n := Uvarint(buf)
if x != 0 || n != n0 {
t.Errorf("Uvarint(%v): got x = %d, n = %d; want 0, %d", buf, x, n, n0)
}
- x, err := ReadUvarint(bytes.NewReader(buf))
- if x != 0 || err != err0 {
- t.Errorf("ReadUvarint(%v): got x = %d, err = %s; want 0, %s", buf, x, err, err0)
+ r := bytes.NewReader(buf)
+ len := r.Len()
+ x, err := ReadUvarint(r)
+ if x != x0 || err != err0 {
+ t.Errorf("ReadUvarint(%v): got x = %d, err = %s; want %d, %s", buf, x, err, x0, err0)
+ }
+ if read := len - r.Len(); read > MaxVarintLen64 {
+ t.Errorf("ReadUvarint(%v): read more than MaxVarintLen64 bytes, got %d", buf, read)
}
}
func TestOverflow(t *testing.T) {
- testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, -10, overflow)
- testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, -13, overflow)
+ testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x2}, 0, -10, overflow)
+ testOverflow(t, []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1, 0, 0}, 0, -13, overflow)
+ testOverflow(t, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, 1<<64-1, 0, overflow) // 11 bytes, should overflow
}
func TestNonCanonicalZero(t *testing.T) {