aboutsummaryrefslogtreecommitdiff
path: root/src/encoding
diff options
context:
space:
mode:
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>2018-05-14 13:50:56 +0200
committerGopher Robot <gobot@golang.org>2023-04-04 22:12:53 +0000
commit2afaa018550ceb42e7a89d1bb53e7fe2d3890377 (patch)
tree42b1d8cfab673e6dc23420f38a5afec381c3389d /src/encoding
parent2f2b874b0aab741a8628fd971f4b3aa41e2fdffb (diff)
downloadgo-2afaa018550ceb42e7a89d1bb53e7fe2d3890377.tar.xz
encoding/binary: add word size to the error message of the failed constraint
Test added. Fixes #22860 Change-Id: I08304834a2b7b10b4ac729bf36761692eb4731da Reviewed-on: https://go-review.googlesource.com/c/go/+/113075 Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/encoding')
-rw-r--r--src/encoding/binary/binary.go2
-rw-r--r--src/encoding/binary/binary_test.go24
2 files changed, 25 insertions, 1 deletions
diff --git a/src/encoding/binary/binary.go b/src/encoding/binary/binary.go
index cb2ad1a7f8..158e3e9d7f 100644
--- a/src/encoding/binary/binary.go
+++ b/src/encoding/binary/binary.go
@@ -451,7 +451,7 @@ func Write(w io.Writer, order ByteOrder, data any) error {
v := reflect.Indirect(reflect.ValueOf(data))
size := dataSize(v)
if size < 0 {
- return errors.New("binary.Write: invalid type " + reflect.TypeOf(data).String())
+ return errors.New("binary.Write: some values are not fixed-sized in type " + reflect.TypeOf(data).String())
}
buf := make([]byte, size)
e := &encoder{order: order, buf: buf}
diff --git a/src/encoding/binary/binary_test.go b/src/encoding/binary/binary_test.go
index 341cd86766..4e1fb59f03 100644
--- a/src/encoding/binary/binary_test.go
+++ b/src/encoding/binary/binary_test.go
@@ -540,6 +540,30 @@ func testReadInvalidDestination(t *testing.T, order ByteOrder) {
}
}
+func TestNoFixedSize(t *testing.T) {
+ type Person struct {
+ Age int
+ Weight float64
+ Height float64
+ }
+
+ person := Person{
+ Age: 27,
+ Weight: 67.3,
+ Height: 177.8,
+ }
+
+ buf := new(bytes.Buffer)
+ err := Write(buf, LittleEndian, &person)
+ if err == nil {
+ t.Fatal("binary.Write: unexpected success as size of type *binary.Person is not fixed")
+ }
+ errs := "binary.Write: some values are not fixed-sized in type *binary.Person"
+ if err.Error() != errs {
+ t.Fatalf("got %q, want %q", err, errs)
+ }
+}
+
type byteSliceReader struct {
remain []byte
}