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

Merovius opened this issue Sep 3, 2016 · 5 comments

encoding/gob: encoding of single values doesn't agree with documentation #16978

Merovius opened this issue Sep 3, 2016 · 5 comments


Copy link

@Merovius 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)?

GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build316359239=/tmp/go-build -gno-record-gcc-switches"

What did you do?

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
Copy link

@Merovius 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))*


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 label Oct 10, 2016
Copy link

@rsc rsc commented Oct 20, 2016

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

/cc @robpike

@rsc rsc added the Documentation label Oct 20, 2016
Copy link

@rsc 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
Copy link

@SamWhited SamWhited commented Feb 28, 2017

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:!topic/golang-nuts/xJH1a9w47Mk

Copy link

@gopherbot gopherbot commented Mar 21, 2017

CL mentions this issue.

@gopherbot gopherbot closed this in 67a46cc Mar 21, 2017
@golang golang locked and limited conversation to collaborators Mar 21, 2018
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
6 participants
You can’t perform that action at this time.