]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: ReadUvarint return io.ErrUnexpectedEOF when read at least 1 byte
authorcuiweixie <cuiweixie@gmail.com>
Mon, 8 Aug 2022 16:48:55 +0000 (16:48 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 8 Aug 2022 17:29:13 +0000 (17:29 +0000)
Fixes #54139

Change-Id: Ifc73bd7f181b13970ee6a08968f9d8f6e55d7ff3
GitHub-Last-Rev: 1e0a79bd3eb3e4dfcbfd7e9f94e849b3248ffac1
GitHub-Pull-Request: golang/go#54143
Reviewed-on: https://go-review.googlesource.com/c/go/+/420274
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/encoding/binary/varint.go
src/encoding/binary/varint_test.go

index c807d15f442e7811d9273dbc9e666f94f6031164..18e1ff15115202ba5b0f10ea5010b1621d33916d 100644 (file)
@@ -126,12 +126,18 @@ func Varint(buf []byte) (int64, int) {
 var overflow = errors.New("binary: varint overflows a 64-bit integer")
 
 // ReadUvarint reads an encoded unsigned integer from r and returns it as a uint64.
+// The error is EOF only if no bytes were read.
+// If an EOF happens after reading some but not all the bytes,
+// ReadUvarint returns io.ErrUnexpectedEOF.
 func ReadUvarint(r io.ByteReader) (uint64, error) {
        var x uint64
        var s uint
        for i := 0; i < MaxVarintLen64; i++ {
                b, err := r.ReadByte()
                if err != nil {
+                       if i > 0 && err == io.EOF {
+                               err = io.ErrUnexpectedEOF
+                       }
                        return x, err
                }
                if b < 0x80 {
@@ -147,6 +153,9 @@ func ReadUvarint(r io.ByteReader) (uint64, error) {
 }
 
 // ReadVarint reads an encoded signed integer from r and returns it as an int64.
+// The error is EOF only if no bytes were read.
+// If an EOF happens after reading some but not all the bytes,
+// ReadVarint returns io.ErrUnexpectedEOF.
 func ReadVarint(r io.ByteReader) (int64, error) {
        ux, err := ReadUvarint(r) // ok to continue in presence of error
        x := int64(ux >> 1)
index 080a2148f0b4f8b51cd2862a1a2c9d8c9450a132..a3caea8a4303165cf000fb38ad1c59d60d7f168d 100644 (file)
@@ -128,7 +128,11 @@ func TestBufferTooSmall(t *testing.T) {
                }
 
                x, err := ReadUvarint(bytes.NewReader(buf))
-               if x != 0 || err != io.EOF {
+               wantErr := io.EOF
+               if i > 0 {
+                       wantErr = io.ErrUnexpectedEOF
+               }
+               if x != 0 || err != wantErr {
                        t.Errorf("ReadUvarint(%v): got x = %d, err = %s", buf, x, err)
                }
        }