fix(jsonx): reject trailing JSON values by lowgame · Pull Request #5809 · zeromicro/go-zero · GitHub
Skip to content

fix(jsonx): reject trailing JSON values - #5809

Open
lowgame wants to merge 1 commit into
zeromicro:masterfrom
lowgame:fix-jsonx-trailing-values-6341652
Open

lowgame wants to merge 1 commit into
zeromicro:masterfrom
lowgame:fix-jsonx-trailing-values-6341652

Conversation

@lowgame

@lowgame lowgame commented Oct 4, 2026

Copy link
Copy Markdown

Summary

  • reject additional top-level JSON values in jsonx unmarshalling
  • preserve UseNumber behavior and allow trailing whitespace
  • cover all three public unmarshal entry points with a regression test

Reproduction

Before this change, each jsonx unmarshal helper returned nil for input containing two top-level values, such as:

var value map[string]any
err := jsonx.Unmarshal([]byte(`{"first": 1} {"second": 2}`), &value)

The first object was decoded and the second was silently ignored. The added regression test failed before the production change with An error is expected but got nil.

Root cause

unmarshalUseNumber called json.Decoder.Decode only once. A decoder accepts a stream of JSON values, so a successful first decode does not prove that the input contains exactly one value.

Solution

After decoding the requested value, perform one more decode and require io.EOF. A second successful decode is reported as multiple JSON values, while malformed trailing data returns the decoder error.

Tests

  • go test ./core/jsonx -count=1 — pass
  • go vet ./core/jsonx — pass
  • go test -race ./core/jsonx -count=1 — pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant