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: eof error of NewDecoder().Decode() should be same with Unmarshal() #25956

Open
isayme opened this issue Jun 19, 2018 · 17 comments
Labels
help wanted NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@isayme
Copy link

isayme commented Jun 19, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.10.1 darwin/amd64

Does this issue reproduce with the latest release?

yes

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

GOARCH="amd64" GOOS="darwin"

What did you do?

https://play.golang.org/p/gr5cHhrpmbK

What did you expect to see?

shown in previous link

What did you see instead?

shown in previous link

@agnivade agnivade changed the title eof error of json.NewDecoder().Decode() should be same with json.Unmarshal encoding/json: eof error of NewDecoder().Decode() should be same with Unmarshal() Jun 19, 2018
@agnivade agnivade added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 19, 2018
@agnivade agnivade added this to the Unplanned milestone Jun 19, 2018
@agnivade
Copy link
Contributor

/cc @dsnet

@isayme
Copy link
Author

isayme commented Jun 19, 2018

BTW. a solution is change io.ErrUnexpectedEOF to &SyntaxError{"unexpected end of JSON input", dec.scan.bytes} in

err = io.ErrUnexpectedEOF

@ianlancetaylor ianlancetaylor added NeedsFix The path to resolution is known, but the work has not been done. help wanted labels Jun 19, 2018
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jun 19, 2018
@ianlancetaylor
Copy link
Contributor

Seems worth changing, want to send a CL?

@agnivade
Copy link
Contributor

@ianlancetaylor - Just concerned, can it be considered a borderline breaking change ?

@ianlancetaylor
Copy link
Contributor

@agnivade Yes, it's clearly possible to write a program that will observe this change. I'm guessing that few programs test explicitly for io.ErrUnexpectedEOF. But I don't actually know. The only way to find out would be to try it.

Actually thinking about it a bit more I'm not sure which way to change it. Is it better to always return a json.SyntaxError? Or is is better to not wrap io.ErrUnexpectedEOF in a json.SyntaxError, just as we generally avoid wrapping io.EOF?

@agnivade
Copy link
Contributor

A quick github search for TestInvalidJSON gives some hits which are potential breakages.

There might be more but I stopped searching after I got these.

Both of these do json.Unmarshal, so I guess it might be safer to change json.NewDecoder. But again, there just might be some code out there which breaks.

@dsnet
Copy link
Member

dsnet commented Jun 19, 2018

Arguments for changing it to *json.SyntaxError:

  • io.ErrUnexpectedEOF is documented as indicating that "EOF was encountered in the middle of reading a fixed-size block or data structure", for which this does not seem to be the case.
  • This situation really does look like syntax error. There is no amount of characters that you can add to {"name":"} to make it valid JSON. EDIT: not true, you can append "} as well here.

That being said, I may have expected {"name":" to return io.ErrUnexpectedEOF, since it is possible to append "} and obtain valid JSON. However, this is not the case today, either.

See https://play.golang.org/p/93ozJzoQSRR

@dsnet
Copy link
Member

dsnet commented Jun 19, 2018

I support changing it to io.ErrUnexpectedEOF for the following reasons:

  • Despite the documentation on ErrUnexpectedEOF, I (and my observation of other parser code) has commonly interpreted that error as indicating that is some number of bytes you could be appended to the string to turn in into a valid input.
  • It is generally safer to move from a less distinguishable to a more distinguishable error. While SyntaxError is distinguishable, it is functionally less so than ErrUnexpectedEOF, which only requires a comparison instead of a type assertion.

@agnivade
Copy link
Contributor

@dsnet - I tried changing it to io.ErrUnexpectedEOF and several tests failed -.

--- FAIL: TestUnmarshal (0.00s)
    decode_test.go:1016: #38: checkValid: &errors.errorString{s:"unexpected EOF"}
--- FAIL: TestUnmarshalSyntax (0.00s)
    decode_test.go:1962: expected syntax error for Unmarshal("\"hello"): got *errors.errorString
    decode_test.go:1962: expected syntax error for Unmarshal("[1,2,3"): got *errors.errorString
    decode_test.go:1962: expected syntax error for Unmarshal("{\"key\":1"): got *errors.errorString
    decode_test.go:1962: expected syntax error for Unmarshal("{\"key\":1,"): got *errors.errorString
FAIL
FAIL	encoding/json	1.086s

Whereas, if I do the other way and change to SyntaxError, no tests fail. It seems to me that changing to SyntaxError may actually be less invasive.

But your call. Let me know.

@agnivade
Copy link
Contributor

agnivade commented Jan 7, 2019

ping @dsnet @mvdan

@mvdan
Copy link
Member

mvdan commented Jan 8, 2019

I know very little about encoding/json's history with errors, sorry. It sounds to me like parsing "hello is a SyntaxError by definition, but I can see the points in favor of using io errors without wrapping.

While SyntaxError is distinguishable, it is functionally less so than ErrUnexpectedEOF, which only requires a comparison instead of a type assertion.

Sounds to me like the Go2 error proposals would help a bit here :) Maybe we can wrap io errors in json.SyntaxError once/if those proposals are implemented.

It seems to me that changing to SyntaxError may actually be less invasive.

If it's just tests that break, and the breakage is consistent with what we're changing here, I'd just fix the tests. Unless what you mean is that unrelated tests are starting to fail. The cases you pasted do look like unexpected EOFs.

@agnivade
Copy link
Contributor

agnivade commented Jan 8, 2019

Sure, I can fix the tests. Just needed a confirmation on whether to go ahead or not.

@montanaflynn
Copy link

I'm also hitting this, it's awkward to have to check if err == io.ErrUnexpectedEOF before a type assertion switch containing *json.SyntaxError.

@agnivade agnivade added NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsFix The path to resolution is known, but the work has not been done. labels Jul 10, 2019
@s3rj1k
Copy link

s3rj1k commented Dec 10, 2019

Any news on this?

@mvdan
Copy link
Member

mvdan commented Dec 10, 2019

I think the solution here needs to be adapted given that error wrapping is a reality, e.g. see https://golang.org/pkg/errors/#Is.

SyntaxError should now wrap the underlying error, such as io.ErrUnexpectedEOF. And the json APIs should all be consistent with this.

If anyone wants to work on a patch and send it, I'm happy to review it. Otherwise, I don't think anyone is actively working on this, so there won't be any news unless someone leaves a comment with an update.

@mvdan
Copy link
Member

mvdan commented Dec 10, 2019

I should also clarify - such a change is too risky now that we're in the middle of the 1.14 freeze, so a CL would probably be merged once the tree reopens in roughly six weeks from now.

@ali2210
Copy link

ali2210 commented Apr 28, 2020

Sir I'm using go1.14

var member Member // struct type
err := json.NewDecoder(request.Body).Decode(&member); if err != nil{println(err)} // yeah this return eof & I expected value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

9 participants