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/json: Error() methods not handling null pointers gracefully #57292

Closed
havelock opened this issue Dec 13, 2022 · 1 comment
Closed

encoding/json: Error() methods not handling null pointers gracefully #57292

havelock opened this issue Dec 13, 2022 · 1 comment

Comments

@havelock
Copy link

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

$ go version
# 1.19 - reproducible on https://go.dev/play/

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
# Go playground

What did you do?

https://go.dev/play/p/UzR0iDwh8eI

Failed type assertion generates a "special" zero value for json.SyntaxError here with its' msg being a nil pointer.
This causes a panic on trying to use its Error() method.

What did you expect to see?

Error() not causing null pointer dereference on an existing value that it's relatively simple to obtain; perhaps some simple "no error message" logics in https://cs.opensource.google/go/go/+/refs/tags/go1.19.4:src/encoding/json/scanner.go;l=52

This can be the case for other errors too, at least in this package.

https://go.dev/tour/methods/15

If not, ok will be false and t will be the zero value of type T, and no panic occurs.

It's also somewhat misleading that "zero value of type T" can mean two very different things; one generated by the process described in https://go.dev/ref/spec#The_zero_value which is the case here I believe, and one by json.SyntaxError{}. Apologies in advance if this goes beyond this bug report.

What did you see instead?

A segmentation fault due to null pointer dereference.

@havelock havelock changed the title affected/package: encoding/json Error() methods not handling null pointers gracefully encoding/json: Error() methods not handling null pointers gracefully Dec 13, 2022
@seankhliao
Copy link
Member

https://go.dev/doc/faq#nil_error

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

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