Skip to content

Escape all control characters in JSON writers - #129

Merged
sheredom merged 1 commit into
sheredom:mainfrom
Shy7777:fix/escape-control-characters
Sep 18, 2026
Merged

sheredom merged 1 commit into
sheredom:mainfrom
Shy7777:fix/escape-control-characters

Conversation

@Shy7777

@Shy7777 Shy7777 commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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 \u00hh in 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.

Copilot AI lite review requested due to automatic review settings September 12, 2026 04:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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.

@sheredom
sheredom merged commit 671f1a3 into sheredom:main Sep 18, 2026
24 checks passed
@sheredom

Copy link
Copy Markdown
Owner

Thanks for your change!

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.

3 participants