mirror of
https://github.com/golang/go
synced 2024-11-19 18:24:39 -07:00
explicitly catch attempt to decode into a value - must be a pointer to see the result.
R=rsc https://golang.org/cl/163070
This commit is contained in:
parent
f9810f1b12
commit
ff3ea68e52
@ -58,6 +58,13 @@ func (dec *Decoder) recvType(id typeId) {
|
|||||||
// The value underlying e must be the correct type for the next
|
// The value underlying e must be the correct type for the next
|
||||||
// data item received.
|
// data item received.
|
||||||
func (dec *Decoder) Decode(e interface{}) os.Error {
|
func (dec *Decoder) Decode(e interface{}) os.Error {
|
||||||
|
// If e represents a value, the answer won't get back to the
|
||||||
|
// caller. Make sure it's a pointer.
|
||||||
|
if _, ok := reflect.Typeof(e).(*reflect.PtrType); !ok {
|
||||||
|
dec.state.err = os.ErrorString("gob: attempt to decode into a non-pointer");
|
||||||
|
return dec.state.err;
|
||||||
|
}
|
||||||
|
|
||||||
// Make sure we're single-threaded through here.
|
// Make sure we're single-threaded through here.
|
||||||
dec.mutex.Lock();
|
dec.mutex.Lock();
|
||||||
defer dec.mutex.Unlock();
|
defer dec.mutex.Unlock();
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"io";
|
"io";
|
||||||
"os";
|
"os";
|
||||||
"reflect";
|
"reflect";
|
||||||
|
"strings";
|
||||||
"testing";
|
"testing";
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -195,7 +196,6 @@ func TestPtrTypeToType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
|
func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
|
||||||
// Encode a *T, decode a T
|
|
||||||
type Type2 struct {
|
type Type2 struct {
|
||||||
a ****float;
|
a ****float;
|
||||||
}
|
}
|
||||||
@ -215,7 +215,6 @@ func TestTypeToPtrPtrPtrPtrType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSlice(t *testing.T) {
|
func TestSlice(t *testing.T) {
|
||||||
// Encode a *T, decode a T
|
|
||||||
type Type3 struct {
|
type Type3 struct {
|
||||||
a []string;
|
a []string;
|
||||||
}
|
}
|
||||||
@ -225,3 +224,15 @@ func TestSlice(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValueError(t *testing.T) {
|
||||||
|
// Encode a *T, decode a T
|
||||||
|
type Type4 struct {
|
||||||
|
a int;
|
||||||
|
}
|
||||||
|
t4p := Type4{3}; // note: not a pointer, unlike the other tests.
|
||||||
|
var t4 Type4;
|
||||||
|
if err := encAndDec(t4, t4p); err == nil || strings.Index(err.String(), "pointer") <= 0 {
|
||||||
|
t.Error("expected error; got none or got wrong one")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user