]> Cypherpunks repositories - gostls13.git/commitdiff
encoding/binary: reject types with implementation-dependent sizes
authorPatrick Gavlin <pgavlin@gmail.com>
Tue, 4 Jan 2011 18:42:50 +0000 (13:42 -0500)
committerRuss Cox <rsc@golang.org>
Tue, 4 Jan 2011 18:42:50 +0000 (13:42 -0500)
Fixes #1201.

R=rsc
CC=golang-dev
https://golang.org/cl/3787044

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

index ebc2ae8b7cff3bc1efa6ca65bf62127ae184fcee..6bbe7eb8959251b89a042b40273b91e60e5c8116 100644 (file)
@@ -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
index d372d2d0272cbc7587ceefbc8ee8a6f4a6ef5b33..c378413f102d79426abc7a0fe1f8d8a36222a5a6 100644 (file)
@@ -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())
+               }
+       }
+}