]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: add word size to the error message of the failed constraint
authorConstantin Konstantinidis <constantinkonstantinidis@gmail.com>
Mon, 14 May 2018 11:50:56 +0000 (13:50 +0200)
committerGopher Robot <gobot@golang.org>
Tue, 4 Apr 2023 22:12:53 +0000 (22:12 +0000)
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>

src/encoding/binary/binary.go
src/encoding/binary/binary_test.go

index cb2ad1a7f890d084b962022b469d331ae2d628c1..158e3e9d7f18805c1f73501712c136ef7ac9d1a6 100644 (file)
@@ -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}
index 341cd86766e33f133619eb46d916730a23ab1629..4e1fb59f03c11e20a105f951e021a0107148e1f1 100644 (file)
@@ -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
 }