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: encoding of single values doesn't agree with documentation #16978

Closed
Merovius opened this issue Sep 3, 2016 · 5 comments
Closed
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@Merovius
Copy link
Contributor

Merovius commented Sep 3, 2016

Please answer these questions before submitting your issue. Thanks!

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

go version go1.7 linux/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/mero"
GORACE=""
GOROOT="/home/mero/go"
GOTOOLDIR="/home/mero/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build316359239=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

https://play.golang.org/p/tXSmbPjlD2

What did you expect to see?

Above program printing []byte{0x2,0x6,0x1} (see comment for why)

What did you see instead?

Above program printing []byet{0x3, 0x6, 0x0, 0x1} with an extra 0-byte.

I tried very hard to find any mention of what this 0-byte means in the documentation but couldn't find anything (though I can't conclusively say it isn't there :) ). This only happens for single primitive values (not for structs) and I think it's an artifact of how the state machine works (i.e. it emits an end-of-struct-tag, even though there never was one).

As the 0-byte carries no information, this is probably a bug in the encoder, but as there probably is encoding of this in the wild and gob isn't version, it probably can't be fixed. In that case, the documentation should be updated to include this.

@josharian josharian changed the title gob encoding of single values doesn't agree with documentation encoding/gob: encoding of single values doesn't agree with documentation Sep 3, 2016
@Merovius
Copy link
Contributor Author

Merovius commented Sep 3, 2016

I found another inconsistency between implementation and documentation: It says

In summary, a gob stream looks like

(byteCount (-type id, encoding of a wireType)* (type id, encoding of a value))*

But:

https://play.golang.org/p/W_cjfC0Q85

This generates two messages:

msg1 = []byte{0xe, 0xff, 0x81, 0x1, 0x1, 0x2, 0xff, 0x82, 0x0, 0x1, 0x4, 0x1, 0x2, 0x0, 0x0}
msg2 = []byte{0x5,0xff, 0x82, 0x0, 0x1, 0xa}

(AIUI messages are length-prefixed). The first is a valid encoding for the wireType of [1]int, but contains no value. According to above section from godoc, every message in the stream has exactly one encoded value.

@quentinmit quentinmit added this to the Go1.8Maybe milestone Sep 6, 2016
@quentinmit quentinmit added the NeedsFix The path to resolution is known, but the work has not been done. label Oct 10, 2016
@rsc
Copy link
Contributor

rsc commented Oct 20, 2016

My guess is that the code is OK and the docs need updating.

/cc @robpike

@rsc
Copy link
Contributor

rsc commented Nov 11, 2016

I started to look at this. Definitely a doc problem. Can wait.

@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Nov 11, 2016
@robpike robpike self-assigned this Feb 28, 2017
@SamWhited
Copy link
Member

Cross linking for reference: It appears that primitive values in Gob's are always encoded as a struct (that extra byte is the struct field separator). See also: https://groups.google.com/forum/#!topic/golang-nuts/xJH1a9w47Mk

@gopherbot
Copy link

CL https://golang.org/cl/38414 mentions this issue.

@golang golang locked and limited conversation to collaborators Mar 21, 2018
@rsc rsc unassigned robpike Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

6 participants