Commit c8b1f854 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov Committed by Rob Pike

encoding/gob: fix memory corruption

Fixes #3175.

R=golang-dev, iant, rsc, r
CC=golang-dev
https://golang.org/cl/5758069
parent b0beeb15
......@@ -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)
}
}
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