aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Gavlin <pgavlin@gmail.com>2011-01-04 13:42:50 -0500
committerRuss Cox <rsc@golang.org>2011-01-04 13:42:50 -0500
commit236f9638b41e94d8e21044df54f2281c52904d7f (patch)
tree5a2659691a1bb67259d9dc41b516f43782946fad
parent3cd10e3a0d9a1984eb1fd9e418885a989e8a454a (diff)
downloadgo-236f9638b41e94d8e21044df54f2281c52904d7f.tar.gz
go-236f9638b41e94d8e21044df54f2281c52904d7f.zip
encoding/binary: reject types with implementation-dependent sizes
Fixes #1201. R=rsc CC=golang-dev https://golang.org/cl/3787044
-rw-r--r--src/pkg/encoding/binary/binary.go4
-rw-r--r--src/pkg/encoding/binary/binary_test.go26
2 files changed, 30 insertions, 0 deletions
diff --git a/src/pkg/encoding/binary/binary.go b/src/pkg/encoding/binary/binary.go
index ebc2ae8b7c..6bbe7eb895 100644
--- a/src/pkg/encoding/binary/binary.go
+++ b/src/pkg/encoding/binary/binary.go
@@ -198,6 +198,10 @@ func sizeof(v reflect.Type) int {
return sum
case *reflect.UintType, *reflect.IntType, *reflect.FloatType, *reflect.ComplexType:
+ switch t := t.Kind(); t {
+ case reflect.Int, reflect.Uint, reflect.Uintptr, reflect.Float, reflect.Complex:
+ return -1
+ }
return int(v.Size())
}
return -1
diff --git a/src/pkg/encoding/binary/binary_test.go b/src/pkg/encoding/binary/binary_test.go
index d372d2d027..c378413f10 100644
--- a/src/pkg/encoding/binary/binary_test.go
+++ b/src/pkg/encoding/binary/binary_test.go
@@ -28,6 +28,15 @@ type Struct struct {
Array [4]uint8
}
+type T struct {
+ Int int
+ Uint uint
+ Float float
+ Complex complex
+ Uintptr uintptr
+ Array [4]int
+}
+
var s = Struct{
0x01,
0x0203,
@@ -136,3 +145,20 @@ func TestWriteSlice(t *testing.T) {
err := Write(buf, BigEndian, res)
checkResult(t, "WriteSlice", BigEndian, err, buf.Bytes(), src)
}
+
+func TestWriteT(t *testing.T) {
+ buf := new(bytes.Buffer)
+ ts := T{}
+ err := Write(buf, BigEndian, ts)
+ if err == nil {
+ t.Errorf("WriteT: have nil, want non-nil")
+ }
+
+ tv := reflect.Indirect(reflect.NewValue(ts)).(*reflect.StructValue)
+ for i, n := 0, tv.NumField(); i < n; i++ {
+ err = Write(buf, BigEndian, tv.Field(i).Interface())
+ if err == nil {
+ t.Errorf("WriteT.%v: have nil, want non-nil", tv.Field(i).Type())
+ }
+ }
+}