mirror of https://github.com/golang/go.git
encoding/gob: fix memory corruption
Fixes #3175. R=golang-dev, iant, rsc, r CC=golang-dev https://golang.org/cl/5758069
This commit is contained in:
parent
b0beeb1501
commit
c8b1f85493
|
|
@ -707,6 +707,9 @@ func (dec *Decoder) decodeInterface(ityp reflect.Type, state *decoderState, p ui
|
|||
if name == "" {
|
||||
// Copy the representation of the nil interface value to the target.
|
||||
// This is horribly unsafe and special.
|
||||
if indir > 0 {
|
||||
p = allocate(ityp, p, 1) // All but the last level has been allocated by dec.Indirect
|
||||
}
|
||||
*(*[2]uintptr)(unsafe.Pointer(p)) = ivalue.InterfaceData()
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -573,3 +573,22 @@ func TestGobEncodeIsZero(t *testing.T) {
|
|||
t.Fatalf("%v != %v", x, y)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGobEncodePtrError(t *testing.T) {
|
||||
var err error
|
||||
b := new(bytes.Buffer)
|
||||
enc := NewEncoder(b)
|
||||
err = enc.Encode(&err)
|
||||
if err != nil {
|
||||
t.Fatal("encode:", err)
|
||||
}
|
||||
dec := NewDecoder(b)
|
||||
err2 := fmt.Errorf("foo")
|
||||
err = dec.Decode(&err2)
|
||||
if err != nil {
|
||||
t.Fatal("decode:", err)
|
||||
}
|
||||
if err2 != nil {
|
||||
t.Fatalf("expected nil, got %v", err2)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue