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: mis-decodes pointers to int 0 to nil pointers #23498

Closed
peterebden opened this issue Jan 21, 2018 · 5 comments
Closed

encoding/gob: mis-decodes pointers to int 0 to nil pointers #23498

peterebden opened this issue Jan 21, 2018 · 5 comments

Comments

@peterebden
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9.2 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/pebers/go"
GORACE=""
GOROOT="/opt/tm/tools/go/1.9.2/usr/go"
GOTOOLDIR="/opt/tm/tools/go/1.9.2/usr/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="cc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build126697151=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Round-tripped a struct with a pointer to an int with value 0 through gob (i.e. encoded then decoded).
e.g. https://play.golang.org/p/9LWD9cg04ni

What did you expect to see?

Decoded struct has a pointer to int 0 and hence matches the original.

What did you see instead?

Decoded struct has a nil pointer.

peterebden added a commit to peterebden/please that referenced this issue Jan 21, 2018
peterebden added a commit to peterebden/please that referenced this issue Jan 21, 2018
@ALTree
Copy link
Member

ALTree commented Jan 21, 2018

gob doc:

Pointers are not transmitted, but the things they point to are transmitted;

So you're encoding an int with value 0, not a pointer. You have to read the data back as a

struct { 
  int 
}

.

@ALTree ALTree changed the title gob mis-decodes pointers to int 0 to nil pointers encoding/gob: mis-decodes pointers to int 0 to nil pointers Jan 21, 2018
@ALTree
Copy link
Member

ALTree commented Jan 21, 2018

see #11119

@peterebden
Copy link
Author

Thanks, I understand why that's the case, but it's surprising (to me at least) that gob doesn't round-trip the struct back to the same thing as the original. The obvious workaround indeed is to use another layer of struct, but while that's easy in this case, it'd have been nice not to have to work out why the two things weren't equivalant.

@ALTree
Copy link
Member

ALTree commented Jan 21, 2018

It surely can be surprising but, as Rob noted in the old issue I linked above, changes to the gob model at this point are pretty unlikely and there's not much to do other that label this as unfortunate.

@ALTree ALTree closed this as completed Jan 21, 2018
@gopherbot
Copy link

Change https://golang.org/cl/90695 mentions this issue: cmd/vet: disable complaint about 0 flag in print

gopherbot pushed a commit that referenced this issue Jan 30, 2018
The problem is that vet complains about 0 as a Printf flag in some
situations where fmt allows it but probably shouldn't. The two
need to be brought in line, but it's too late in the release cycle.

The situation is messy and should be resolved properly in 1.11. This
CL is a simple fix to disable a spurious complaint for 1.10 that will be
resolved in a more thorough way in 1.11.

The workaround is just to be silent about flag 0, as suggested in
issue 23605.

Fixes #23605
Update #23498

Change-Id: Ice1a4f4d86845d70c1340a0a6430d74e5de9afd4
Reviewed-on: https://go-review.googlesource.com/90695
Run-TryBot: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
@golang golang locked and limited conversation to collaborators Jan 30, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants