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

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 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

Yes indeed, thank you.

@josharian
Copy link
Contributor

Related: #36225

@golang golang locked and limited conversation to collaborators Jan 3, 2022
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

4 participants