aboutsummaryrefslogtreecommitdiff
path: root/src/encoding/binary/binary_test.go
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2023-06-20 09:38:39 -0700
committerGopher Robot <gobot@golang.org>2023-06-20 18:28:44 +0000
commite122ebabb657021964f2bdd31e683ddfa023fd0c (patch)
tree20e44ebcd894cfd634890581ce3481328b3ddc2d /src/encoding/binary/binary_test.go
parent8484f2fe0250f16304f3112b1caee5a32e685823 (diff)
downloadgo-e122ebabb657021964f2bdd31e683ddfa023fd0c.tar.xz
encoding/binary: on invalid type return -1 from Size
Size is defined as returning -1 if the type is not fixed-size. Before this CL cases like Size((*[]int)(nil)) would crash. Fixes #60892 Change-Id: Iee8e20a0aee24b542b78cb4160c3b2c5a3eb02c2 Reviewed-on: https://go-review.googlesource.com/c/go/+/504575 Auto-Submit: Ian Lance Taylor <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/encoding/binary/binary_test.go')
-rw-r--r--src/encoding/binary/binary_test.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/encoding/binary/binary_test.go b/src/encoding/binary/binary_test.go
index 4e1fb59f03..4b22b28843 100644
--- a/src/encoding/binary/binary_test.go
+++ b/src/encoding/binary/binary_test.go
@@ -351,6 +351,26 @@ func TestSizeStructCache(t *testing.T) {
}
}
+func TestSizeInvalid(t *testing.T) {
+ testcases := []any{
+ int(0),
+ new(int),
+ (*int)(nil),
+ [1]uint{},
+ new([1]uint),
+ (*[1]uint)(nil),
+ []int{},
+ []int(nil),
+ new([]int),
+ (*[]int)(nil),
+ }
+ for _, tc := range testcases {
+ if got := Size(tc); got != -1 {
+ t.Errorf("Size(%T) = %d, want -1", tc, got)
+ }
+ }
+}
+
// An attempt to read into a struct with an unexported field will
// panic. This is probably not the best choice, but at this point
// anything else would be an API change.