Skip to content

encoding/gob: pointers to zero values are not initialized in Decode #11119

Closed
@dvyukov

Description

@dvyukov

The following program crashes with nil deref in println statement:

package main

import (
    "bytes"
    "encoding/gob"
)

type X struct {
    A *int
}

func main() {
    gob.Register(X{})
    x := &X{A: new(int)}
    // *x.A = 1
    buf := new(bytes.Buffer)
    gob.NewEncoder(buf).Encode(x)
    x1 := &X{}
    gob.NewDecoder(buf).Decode(x1)
    println(*x1.A)
}

If "*x.A = 1" is uncommented the program does not crash.
It is perfectly fine to save wire bandwidth for zero values, but a variable value should not affect program behavior in such radical way. If a field points to a zero value, it is not zero, so it does not qualify for If a field has the zero value for its type, it is omitted from the transmission. I think that pointers to zero values should be explicitly transmitted as zero values, that will force Decode to restore the pointers.

go version devel +b0532a9 Mon Jun 8 05:13:15 2015 +0000 linux/amd64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions