Repository navigation
Escape all control characters in JSON writers - #129
Merged
Merged
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No unresolved issues were identified, and all reviewers assessed it as ready.
Pull request overview
This PR updates JSON writers to escape all control characters (U+0000–U+001F) correctly while preserving short escapes and UTF-8 output.
Changes:
- Updates string size calculations.
- Escapes controls in values and object keys.
- Adds regression coverage for minified and pretty writers.
File summaries
| File | Description |
|---|---|
test/test_shared.h |
Adds comprehensive serialization regression tests. |
json.h |
Implements control-character escaping and matching size calculations. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Owner
|
Thanks for your change! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A valid JSON string containing an escaped U+0000 character can be parsed successfully, but writing it back currently emits a literal NUL. Other control characters without a short escape are also written verbatim, producing invalid JSON and potentially truncating the output for callers that treat it as a C string.
This escapes the remaining U+0000–U+001F characters as
\u00hhin both the minified and pretty writers, including object keys. The size calculation accounts for all six bytes. Existing short escapes and UTF-8 bytes are preserved.The new regressions check all 32 control characters, embedded NUL followed by more text, object keys and values, output sizes, and UTF-8. They fail against the original writer and pass with this change. The full CMake/CTest suite passes locally with Apple Clang 17 across all nine C/C++ test variants, including the AddressSanitizer, UndefinedBehaviorSanitizer, and ThreadSanitizer targets.
Related to #103. This addresses serialization only; parser acceptance rules are unchanged. The output follows the escaping requirement in RFC 8259, section 7.