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/asn1: Marshal doesn't handle nil interface values correctly #39071

Open
chriskaly opened this issue May 14, 2020 · 3 comments
Open

encoding/asn1: Marshal doesn't handle nil interface values correctly #39071

chriskaly opened this issue May 14, 2020 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@chriskaly
Copy link

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

$ go version go1.14.2 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/christoph/.cache/go-build"
GOENV="/home/christoph/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOOS="linux"
GOPATH="/home/christoph/.gopath"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build641307088=/tmp/go-build -gno-record-gcc-switches"

What did you do?

asn1.Marshal a struct containing a nil interface with omitempty tag.
https://play.golang.org/p/x-hrKEJH0Fe

What did you expect to see?

No error and the corresponding field should be omitted from the output.
It works with nil slices (see example).

What did you see instead?

Error: asn1: cannot marshal nil value

chriskaly added a commit to chriskaly/go that referenced this issue May 14, 2020
The existing marshal implementation throws an error if it encounters an invalid value. A nil interface thus always causes errors, even if the corresponding field has the omitempty tag. Check for omitempty and return a nil encoder if it is set.

Fixes golang#39071
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/234017 mentions this issue: encoding/asn1: respect omitempty when marshaling nil values

@smasher164 smasher164 changed the title asn1.Marshal doesn't handle nil interface values correctly encoding/asn1: Marshal doesn't handle nil interface values correctly May 14, 2020
chriskaly added a commit to chriskaly/go that referenced this issue May 18, 2020
@chriskaly
Copy link
Author

For anyone who encounters this problem: As a workaround, you can test if the corresponding interface field is nil before marshaling and assign []byte(nil) to it.

@cagedmantis cagedmantis added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 19, 2020
@cagedmantis cagedmantis added this to the Backlog milestone May 19, 2020
@cagedmantis
Copy link
Contributor

/cc @FiloSottile @agl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants