Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Report the comma position for illegal trailing comma errors - #382

Merged
etrepum merged 1 commit into
simplejson:mainfrom
dylanpulver:fix-trailing-comma-position
Aug 26, 2026
Merged

Report the comma position for illegal trailing comma errors#382
etrepum merged 1 commit into
simplejson:mainfrom
dylanpulver:fix-trailing-comma-position

Conversation

@dylanpulver

Copy link
Copy Markdown
Contributor

Differential fuzzing of the C scanner against the pure-Python fallback (_toggle_speedups) found 61 divergences in 12,000 random decode inputs, all of them the position reported for an illegal trailing comma in an array:

>>> simplejson.loads('[42, ]')
# C scanner:      ... line 1 column 6 (char 5)   <- the ']'
# Python scanner: ... line 1 column 5 (char 4)   <- the space before it
# stdlib json:    ... line 1 column 4 (char 3)   <- the comma

Cause

ERR_TRAILING_COMMA_* is raised after the whitespace following the comma has been skipped, so the index passed no longer refers to it. The C scanner passes idx, the closing bracket. JSONArray passes end - 1, an idiom copied from the delimiter error a few lines up where end had been incremented; here it has not, so the reported character is whatever precedes ].

Fix

Capture the comma index when the delimiter is confirmed and report that, as json/decoder.py does with its own comma_idx. Four sites: JSONObject, JSONArray, and the object and array branches of the scan template. Trailing-comma detection landed in 4.0.0 as CPython parity (#371), so stdlib breaks the tie.

Result

Same corpus: 61 divergences before, 0 after. Refiltering it to drop trailing commas gives 0 both before and after. Encode was fuzzed alongside and showed 0.

test_trailing_comma_position pins the offsets for both containers, whitespace after the comma, and nesting, and _cibw_runner runs it on both paths. Reverting only decoder.py fails it on the Python path, only _speedups_scan.h on the C path. Suite passes on both (460 tests, was 458). The C change builds clean under the -Werror flags in AGENTS.md. I could not exercise the Py2 _str instantiation locally.

Both scanners reported the position of the closing bracket, or in the
pure-Python array case whatever character happened to precede it, rather
than the offending comma.

The comma index is captured right after the delimiter is confirmed, so
it survives the whitespace skip that follows. This is what CPython's
json.decoder does with its own comma_idx, and it makes all four sites
(object and array, C and pure-Python) report the same position as the
stdlib.
@etrepum
etrepum added this pull request to the merge queue Aug 26, 2026
Merged via the queue into simplejson:main with commit b84ad54 Aug 26, 2026
26 checks passed
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Aug 28, 2026
Version 4.1.2 released 2026-08-26

* Fix control character error position when content precedes the control char
  simplejson/simplejson#379
* Report the offending char, not the backslash, for invalid \X escapes
  simplejson/simplejson#380
* Handle non-finite Decimals like floats in the encoder
  simplejson/simplejson#381
* Parse failures due to trailing commas are now reported consistently
  across implementations
  simplejson/simplejson#382
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.

2 participants