Skip to content

fix: allow a comment between a trailing comma and ']' (#1500)#1696

Merged
baylesj merged 2 commits into
masterfrom
fix/1500-trailing-comma-comment
Jun 17, 2026
Merged

fix: allow a comment between a trailing comma and ']' (#1500)#1696
baylesj merged 2 commits into
masterfrom
fix/1500-trailing-comma-comment

Conversation

@baylesj

@baylesj baylesj commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #1500.

Problem

Trailing commas and comments are both allowed by default, but they didn't compose inside arrays:

input result (before)
[1,2,] ✅ OK
[1,2 /* c */] ✅ OK
[1,2, /* c */] ❌ "value, object or array expected"
[{}, // c\n] ❌ same

readArray detected a trailing-comma ] with a raw *current_ == ']' peek that skipped only whitespace, not comments. So after a trailing comma a following comment left current_ on /, the peek failed, and the parser tried to read another value — which immediately hit ]. readObject already handled this (it uses readTokenSkippingComments).

Fix

Add skipCommentTokens() — skip whitespace and comments, leaving current_ at the next significant character — and use it in readArray before the ] check. Consumed comments stay in commentsBefore_, so:

  • a comment before a real element is still attached to that element, and
  • if the array ends, the comments are simply not attached (matching object behavior).

This also makes an otherwise-empty array containing only a comment ([ /* c */ ]) parse as an empty array.

Tests

CharReaderTest/parseTrailingCommaWithComment covers line/block comments after a trailing comma, a nested value before the trailing comma, an empty array containing only a comment, the object form, and that a comment before a real element is still attached (hasComment(commentBefore)). All 128 unit tests pass, JSON conformance suite passes (96/96), clang-format clean. Targets 1.10.0.

baylesj and others added 2 commits June 17, 2026 00:45
Trailing commas and comments are both allowed by default, but they did
not compose inside arrays: readArray detected a trailing-comma ']' with a
raw `*current_ == ']'` peek that skipped only whitespace, not comments.
So `[1, 2, /* c */]` left current_ at the comment, the peek failed, and
the parser tried to read another value -- which hit ']' and reported
"value, object or array expected". readObject already handled this via
readTokenSkippingComments.

Add skipCommentTokens() (skip whitespace and comments, leaving current_
at the next significant character) and use it in readArray before the
']' check. Consumed comments stay in commentsBefore_, so a comment before
a real element is still attached to it; if the array ends, they are
simply not attached -- matching object behavior.

Adds CharReaderTest/parseTrailingCommaWithComment covering line/block
comments after a trailing comma, an empty array containing only a
comment, the object form, and that a comment before a real element is
still attached.
@coveralls

coveralls commented Jun 17, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 27673874228

Coverage decreased (-0.06%) to 89.903%

Details

  • Coverage decreased (-0.06%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (8 of 9 lines covered, 88.89%).
  • No coverage regressions found.

Uncovered Changes

File Changed Covered %
src/lib_json/json_reader.cpp 9 8 88.89%

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 2740
Covered Lines: 2615
Line Coverage: 95.44%
Relevant Branches: 2638
Covered Branches: 2220
Branch Coverage: 84.15%
Branches in Coverage %: Yes
Coverage Strength: 23827.05 hits per line

💛 - Coveralls

@baylesj baylesj merged commit 22c7ec3 into master Jun 17, 2026
48 checks passed
@baylesj baylesj deleted the fix/1500-trailing-comma-comment branch June 17, 2026 07:51
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.

Trailing comma before comment in array crashes

2 participants