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: incorrect decoding of slices when they are reused across multiple decode iterations #64071

Closed
inso opened this issue Nov 11, 2023 · 2 comments

Comments

@inso
Copy link

inso commented Nov 11, 2023

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

$ go version
go version go1.21.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/inso/.cache/go-build'
GOENV='/home/inso/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS='-buildvcs=false'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/inso/.gvm/pkgsets/go1.21/global/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/inso/.gvm/pkgsets/go1.21/global'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/inso/.gvm/gos/go1.21'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/inso/.gvm/gos/go1.21/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build484173072=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Playground

I have created application in Go which collects data and saves the stream of data to the file using encoding/gob.
Later collected data is read and reports are produced. The amount of elements in the file can be large so I am reusing Data structure in the read loop calling Reset method to reset its state. Inside Data I have a slice of items Items []Item. To reset the slice and reuse the memory I do s.Items = s.Items[:0]. And when decoded data has default value of its type the decoder is not properly filling decoded slice.
What I assume is happening: if target slice has enough capacity the decoder expands slice to the target size (s.Items = s.Items[:1] in my case) and then it skips filling target fields if decoded item has default type value and decoded element got values from previous iteration.

What did you expect to see?

Ok: encData == decData

What did you see instead?

Error: encData != decData
encData: main.Data{Items:[]main.Item{main.Item{Id:1, V1:0, V2:32}}}
decData: main.Data{Items:[]main.Item{main.Item{Id:1, V1:11, V2:32}}}
@seankhliao
Copy link
Member

It's not slices, it's just zero fields aren't sent or written:

If a field has the zero value for its type (except for arrays; see above), it is omitted from the transmission.

@seankhliao
Copy link
Member

Duplicate of #8997

@seankhliao seankhliao marked this as a duplicate of #8997 Nov 11, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants