Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

dvyukov opened this issue Jun 8, 2015 · 1 comment

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

dvyukov opened this issue Jun 8, 2015 · 1 comment


Copy link

@dvyukov dvyukov commented Jun 8, 2015

The following program crashes with nil deref in println statement:

package main

import (

type X struct {
    A *int

func main() {
    x := &X{A: new(int)}
    // *x.A = 1
    buf := new(bytes.Buffer)
    x1 := &X{}

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

@dvyukov dvyukov added this to the Go1.5Maybe milestone Jun 8, 2015
@robpike robpike modified the milestones: Unplanned, Go1.5Maybe Jun 8, 2015
Copy link

@robpike robpike commented Jun 8, 2015

This would be a profound change to the logic and the semantics. Pointers do not exist in the model, so a pointer to a zero value is a zero value.

To say this is unfortunate is I think the best we can do here.

@robpike robpike closed this Jun 8, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.