]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: make Read return an error when data is not a pointer
authorUdalov Max <re.udalov@gmail.com>
Wed, 3 Jul 2019 20:31:50 +0000 (23:31 +0300)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 8 Nov 2019 18:00:31 +0000 (18:00 +0000)
Make binary.Read return an error when passed `data` argument is not
a pointer to a fixed-size value or a slice of fixed-size values.

Fixes #32927

Change-Id: I04f48be55fe9b0cc66c983d152407d0e42cbcd95
Reviewed-on: https://go-review.googlesource.com/c/go/+/184957
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/encoding/binary/binary.go
src/encoding/binary/binary_test.go

index 43fa821b83f62397854d367b6f912ecdacea320f..33066fc77a60ded6f153645e74275a14e1b7407e 100644 (file)
@@ -219,8 +219,12 @@ func Read(r io.Reader, order ByteOrder, data interface{}) error {
                        for i := range data {
                                data[i] = order.Uint64(bs[8*i:])
                        }
+               default:
+                       n = 0 // fast path doesn't apply
+               }
+               if n != 0 {
+                       return nil
                }
-               return nil
        }
 
        // Fallback to reflect-based decoding.
index d7ae23a60e08249106c2d82b0fdd340f127bad73..778de6908ca3b9aea6ce271829ffd244d0bc9568 100644 (file)
@@ -6,6 +6,7 @@ package binary
 
 import (
        "bytes"
+       "fmt"
        "io"
        "io/ioutil"
        "math"
@@ -451,6 +452,35 @@ func TestEarlyBoundsChecks(t *testing.T) {
        }
 }
 
+func TestReadInvalidDestination(t *testing.T) {
+       testReadInvalidDestination(t, BigEndian)
+       testReadInvalidDestination(t, LittleEndian)
+}
+
+func testReadInvalidDestination(t *testing.T, order ByteOrder) {
+       destinations := []interface{}{
+               int8(0),
+               int16(0),
+               int32(0),
+               int64(0),
+
+               uint8(0),
+               uint16(0),
+               uint32(0),
+               uint64(0),
+
+               bool(false),
+       }
+
+       for _, dst := range destinations {
+               err := Read(bytes.NewReader([]byte{1, 2, 3, 4, 5, 6, 7, 8}), order, dst)
+               want := fmt.Sprintf("binary.Read: invalid type %T", dst)
+               if err == nil || err.Error() != want {
+                       t.Fatalf("for type %T: got %q; want %q", dst, err, want)
+               }
+       }
+}
+
 type byteSliceReader struct {
        remain []byte
 }