diff options
| author | Ian Lance Taylor <iant@golang.org> | 2023-06-20 09:38:39 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2023-06-20 18:28:44 +0000 |
| commit | e122ebabb657021964f2bdd31e683ddfa023fd0c (patch) | |
| tree | 20e44ebcd894cfd634890581ce3481328b3ddc2d /src/encoding/binary/binary.go | |
| parent | 8484f2fe0250f16304f3112b1caee5a32e685823 (diff) | |
| download | go-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.go')
| -rw-r--r-- | src/encoding/binary/binary.go | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/src/encoding/binary/binary.go b/src/encoding/binary/binary.go index 158e3e9d7f..3fb18a7a03 100644 --- a/src/encoding/binary/binary.go +++ b/src/encoding/binary/binary.go @@ -479,7 +479,6 @@ func dataSize(v reflect.Value) int { if s := sizeof(v.Type().Elem()); s >= 0 { return s * v.Len() } - return -1 case reflect.Struct: t := v.Type() @@ -491,8 +490,12 @@ func dataSize(v reflect.Value) int { return size default: - return sizeof(v.Type()) + if v.IsValid() { + return sizeof(v.Type()) + } } + + return -1 } // sizeof returns the size >= 0 of variables for the given type or -1 if the type is not acceptable. |
