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: Decode does not error on invalid leading zeros #43474

Closed
davecheney opened this issue Jan 3, 2021 · 3 comments
Closed

encoding/json: Decode does not error on invalid leading zeros #43474

davecheney opened this issue Jan 3, 2021 · 3 comments

Comments

@davecheney
Copy link
Contributor

@davecheney davecheney commented Jan 3, 2021

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

go 1.16.beta1

Does this issue reproduce with the latest release?

yes

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

darwin/amd64

What did you do?

package main

import (
	"encoding/json"
	"fmt"
	"strings"
)

func main() {
	s := `01`
	sr := strings.NewReader(s)
	dec := json.NewDecoder(sr)
	var f float64
	err := dec.Decode(&f)
	fmt.Println(f, err)
}

What did you expect to see?

an error related to an invalid number

What did you see instead?

0 <nil>
@cespare
Copy link
Contributor

@cespare cespare commented Jan 3, 2021

The decoder decodes a valid JSON value (0) and gives it back. The 1 is unconsumed. If you decode again, you will get the next JSON value in the stream (1).

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

@cespare cespare changed the title encoding/json: Decode does not error on invalid leading zeros. encoding/json: Decode does not error on invalid leading zeros Jan 3, 2021
@davecheney
Copy link
Contributor Author

@davecheney davecheney commented Jan 3, 2021

Yes indeed, thank you.

@davecheney davecheney closed this Jan 3, 2021
@josharian
Copy link
Contributor

@josharian josharian commented Jan 3, 2021

Related: #36225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants