Skip to content

encoding/json: Decoder.Token does not return an error for incomplete JSON #69449

@gazerro

Description

@gazerro

When dealing with incomplete JSON, the (*Decoder).Token method returns io.EOF at the end of the input instead of providing an appropriate error. For example, the following code does not produce an error:

https://go.dev/play/p/HHEwVkRCs7a

dec := json.NewDecoder(strings.NewReader(`[123,`))
for {
	_, err := dec.Token()
	if err == io.EOF {
		break
	}
	if err != nil {
		panic(err)
	}
}

According to the documentation, Token guarantees that the delimiters [ ] { } it returns are properly nested and matched. However, in this example, [ is not properly nested and matched.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions