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:
Dmitriy Vyukov 2012-03-08 08:53:08 +11:00 committed by Rob Pike
parent b0beeb1501
commit c8b1f85493
2 changed files with 22 additions and 0 deletions

View File

@ -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
}

View File

@ -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)
}
}