Commit c444ec30 authored by Udalov Max's avatar Udalov Max Committed by Brad Fitzpatrick

encoding/binary: make Read return an error when data is not a pointer

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/+/184957Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent a84ac189
......@@ -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.
......
......@@ -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
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment