Skip to content

encoding/json: []string was left with redunt element after a fail unmarshal #71997

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

Closed
lwabish opened this issue Feb 27, 2025 · 1 comment
Closed
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@lwabish
Copy link

lwabish commented Feb 27, 2025

Go version

go version go1.23.4 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE='on'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/xxx/Library/Caches/go-build'
GOENV='/Users/xxx/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/xxx/go/pkg/mod'
GONOPROXY='xxx/*'
GONOSUMDB='xxx/*'
GOOS='darwin'
GOPATH='/Users/xxx/go'
GOPRIVATE='xxx/*'
GOPROXY='https://goproxy.cn,direct'
GOROOT='/opt/homebrew/opt/go/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='local'
GOTOOLDIR='/opt/homebrew/opt/go/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.4'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/xxx/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/xxx/Desktop/go-tmp/go.mod'
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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/69/cq904fld4v1gzx12qwwn01k00000gn/T/go-build4189542584=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

This is the code to reproduce my issue

func TestUnmarshalStringArray(t *testing.T) {
	var a []string
	if err := json.Unmarshal([]byte(`[1,2,3]`), &a); err != nil {
		t.Logf("unmarshal error: %v", err)
	}
	t.Logf("result: %v", a[0])
	t.Logf("len: %d", len(a))
}

What did you see happen?

The code tries to unmarshal wrong type of json into a variable and returns an error as expected.
But after that, the variable of []string was filled with 3 elements of empty string, which is unexpected.

What did you expect to see?

If error occurred during unmarshal , the target variable should be recoverd to original states indead of filled with unexpected emements

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

3 participants