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: Slice encoding is not symmetric #24495

Closed
rvolosatovs opened this issue Mar 22, 2018 · 2 comments
Closed

encoding/gob: Slice encoding is not symmetric #24495

rvolosatovs opened this issue Mar 22, 2018 · 2 comments

Comments

@rvolosatovs
Copy link

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

go version go1.10 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="/home/rvolosatovs/.local/bin.go"
GOCACHE="/home/rvolosatovs/.local/cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/rvolosatovs"
GORACE=""
GOROOT="/nix/store/r7p21cmhq0jimq2f8c8y5sm2vqg2ap99-go-1.10/share/go"
GOTMPDIR=""
GOTOOLDIR="/nix/store/r7p21cmhq0jimq2f8c8y5sm2vqg2ap99-go-1.10/share/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
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"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build642040554=/tmp/go-build -gno-record-gcc-switches"

What did you do?

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

Relevant part of the code:

for _, a := range [][]byte{
		nil,
		[]byte{},
		[]byte{42},
	} {
		var buf bytes.Buffer
		gob.NewEncoder(&buf).Encode(a)

		var b []byte
		gob.NewDecoder(&buf).Decode(&b)

		fmt.Println(reflect.DeepEqual(b, a))
	}

What did you expect to see?

true
true
true

What did you see instead?

true
false
true

Hence, zero-length slices are encoded in the same way as nil slices, which makes encoding non-symmetric.

@robpike
Copy link
Contributor

robpike commented Mar 22, 2018

Gob does not have a concept of nil in its data format, so this is working as designed.

@robpike robpike closed this as completed Mar 22, 2018
@rvolosatovs
Copy link
Author

But zero-length slice is not nil, right?
https://play.golang.org/p/aBW1WGw8RlV

@golang golang locked and limited conversation to collaborators Mar 22, 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