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: reusing Encoder results in corrupt encoded data #49844

Closed
andig opened this issue Nov 29, 2021 · 1 comment
Closed

encoding/gob: reusing Encoder results in corrupt encoded data #49844

andig opened this issue Nov 29, 2021 · 1 comment

Comments

@andig
Copy link
Contributor

andig commented Nov 29, 2021

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

$ go version
go version go1.17.2 darwin/arm64

Does this issue reproduce with the latest release?

Reproduces with gotip

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

go env Output
$ go env
GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/andig/Library/Caches/go-build"
GOENV="/Users/andig/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/andig/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/andig/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.17.2/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.17.2/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.2"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/andig/htdocs/evcc/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/sv/rs_453y57xj86xsbz3kw1mbc0000gn/T/go-build2192259178=/tmp/go-build -gno-record-gcc-switches -fno-common"
GOROOT/bin/go version: go version go1.17.2 darwin/arm64
GOROOT/bin/go tool compile -V: compile version go1.17.2
uname -v: Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:24 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T8101
ProductName:	macOS
ProductVersion:	12.0.1
BuildVersion:	21A559
lldb --version: lldb-1300.0.32.4
Swift version 5.5.1-dev

What did you do?

Encode values using gob while reusing the encoder in a loop: https://go.dev/play/p/T3aHmYmKjid. As soon as the encoder is created inside the loop the problem is solved.

What did you expect to see?

No error.

What did you see instead?

Error during decoding:

panic: gob: decoding into local type []float64, received remote type []float
@andig
Copy link
Contributor Author

andig commented Nov 29, 2021

@robpike commented on golang nuts:

The encoder delivers a stream that includes type information. If you
reuse the stream to a different decoder, that decoder will not have
seen the type information sent to the first one.
Or to look at it another way, the encoder just sends the values you
give it, along with any type information that has not already been
transmitted. In effect, it can't tell the difference between calling
it twice to deliver to one encoder and calling it twice to deliver to
two decoders.
Don't reuse the encoder.

-rob

That said: is that clear enough from the gob docs? It contained information about how types are encoded, but I couldn't find anything related to usage.

@andig andig closed this as completed Nov 29, 2021
@golang golang locked and limited conversation to collaborators Nov 29, 2022
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

2 participants