Commit 236f9638 authored by Patrick Gavlin's avatar Patrick Gavlin Committed by Russ Cox

encoding/binary: reject types with implementation-dependent sizes

Fixes #1201.

R=rsc
CC=golang-dev
https://golang.org/cl/3787044
parent 3cd10e3a
......@@ -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
......
......@@ -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())
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment