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
json.NewDecoder.Decode(buf) seg faults #40751
Comments
Please provide a full program to reproduce this. Also, try building your program with |
Hi @mvdan thanks for the quick response! I've created a short program to illustrate the issue: https://play.golang.org/p/QwbR7J99nZs |
You're trying to read from a nil reader. This is very much expected. Wrap your code with a nil check. The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions. |
Thanks for pointing that out @mvdan . Although this wasn't intended to be a question. I'm already using a nil check in my code, but felt this behavior could be included in the go standard library. |
Unless the docs explicitly say that they handle a nil interface value, you can't assume it will work. Passing |
One case where this would be helpful: validating JSON data sent to REST APIs. Supposing the handler received an empty request body, and passes it into func createSample(w http.ResponseWriter, r *http.Request) {
if r.Body == nil {
// error handling
}
var s Sample
res, err := json.NewDecoder.Decode(&s)
if err != nil {
// error handling
}
} |
Yes, and that seems fine to me. Because "no input was provided" is an entirely different error than "there was a problem decoding the JSON". |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Update: https://play.golang.org/p/QwbR7J99nZs
What did you expect to see?
err == InvalidUnmarshalError
What did you see instead?
Segmentation fault inside Decode
The text was updated successfully, but these errors were encountered: