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

Decoder hangs when decoding invalid json #18

Closed
pelletier opened this issue Dec 9, 2019 · 4 comments · Fixed by #19
Closed

Decoder hangs when decoding invalid json #18

pelletier opened this issue Dec 9, 2019 · 4 comments · Fixed by #19
Labels
bug Something isn't working

Comments

@pelletier
Copy link
Contributor

pelletier commented Dec 9, 2019

func TestHangingDecoder(t *testing.T) {
	b := []byte(`{
	"userId": "blah",
	}`)

	d := NewDecoder(bytes.NewReader(b))

	var a struct {
		UserId string `json:"userId"`
	}
	err := d.Decode(&a)
	if err == nil {
		t.Fatal("should have errored")
	}
}

This test hangs on master. Light on details for now, will add more when I have them.

Looks like it's an issue in Decoder. It works fine with Unmarshal:

// this works
func TestHangingDecoder(t *testing.T) {
	b := []byte(`{
	"userId": "blah",
	}`)
	var a struct {
		UserId string `json:"userId"`
	}
	err := Unmarshal(b, &a)
	if err == nil {
		t.Fatal("should have errored")
	}
}
@pelletier pelletier added the bug Something isn't working label Dec 9, 2019
@achille-roussel
Copy link
Contributor

This is also not valid JSON, the trailing comma is forbidden (I'll work on a fix).

@pelletier
Copy link
Contributor Author

yeah, it's the point

@pelletier
Copy link
Contributor Author

Decode() should error in this scenario. Not hang :)

@pelletier
Copy link
Contributor Author

I think the issue comes from

encoding/json/json.go

Lines 285 to 289 in 4980b2f

v, r, err = parseValue(dec.remain)
if err == nil {
dec.remain = skipSpaces(r)
return
}

We continue looping even though parseValue returns an error.

@pelletier pelletier changed the title Decoder hangs Decoder hangs when decoding invalid json Dec 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants