Skip to content

fix(scan): escape all control characters in scanner JSON output - #205

Merged
pyramation merged 2 commits into
mainfrom
fix/scan-json-control-chars
Nov 25, 2026
Merged

pyramation merged 2 commits into
mainfrom
fix/scan-json-control-chars

Conversation

@pyramation

@pyramation pyramation commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

build_scan_json in templates/full/wasm_wrapper.c only escaped ", \, \n, \r, \t, so token text containing other control bytes (e.g. \x01 inside a string literal or comment) produced invalid JSON and scan/scanSync failed to parse it.

  • Bytes < 0x20 not already handled are now written as \u00XX.
  • Escape buffers and the overall size estimate use 6 bytes per input byte (was 2), with an overflow guard.
  • Regenerated versions/18/src/wasm_wrapper.c via pnpm copy:templates.
  • Added a PG 18 test in versions/18/test/scan.test.js checking both scan APIs keep control bytes in literals and comments.

Redo of just the scanner part of #183. PG 18 build + pnpm test pass locally (151/151).


libpg-query and parser opened this PR for Dan
Implemented in session Escape control characters in scanner JSON output
Review with Devin · Open in Devin Desktop


Summary by CodeRabbit

  • Bug Fixes
    • Scan results now preserve control characters in SQL literals and block comments, keeping token text consistent with the original input.
    • Special characters are correctly escaped in JSON output, preventing malformed scan results when input contains control characters.
    • The fix applies to both synchronous and asynchronous scanning, including control characters from U+0001 through U+001F.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — 2 nitpicks below.

🧹 Nitpicks (2) — 🟢 2 low
  • 🟢 Handle negative snprintf return in escape loop (wasm_wrapper.c:341) — The new control-character escape branch adds snprintf's int return directly to escaped_pos, which the PR changed to size_t (templates/full/wasm_wrapper.c:341).
  • 🟢 Cover "/\ escaping and overflow guard via async scan (scan.test.js:307) — The newly hardened paths in build_scan_json — the "/\ escape branch (src/wasm_wrapper.c:335) and the large-token overflow guard (src/wasm_wrapper.c:289-296) — are exercised only through query.scanSync in tests at versions/18/test/scan.test.js:279-305.

This PR hardens the JSON serialization path in build_scan_json by escaping control characters (<0x20) as \uXXXX, widening escaped_pos to size_t, tightening the per-token buffer estimate, and adding a SIZE_MAX-based overflow guard for very large tokens. The changes are applied consistently to the template and the generated versions/18 copy, and the regression test suite gains a sync/async parity case for control characters.

Files Change
templates/full/wasm_wrapper.c, versions/18/src/wasm_wrapper.c Escape control characters in token text as \uXXXX, switch the escape position to size_t, and add overflow guards for large-token buffer sizing.
versions/18/test/scan.test.js Add tests for large-token overflow, quote/backslash escaping, and scan/scanSync parity for control characters.

Reviewed commit: 0dbd46b

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The scan JSON wrappers now size token buffers using escaped text lengths and encode control bytes below 0x20 as JSON escapes. A test checks that synchronous and asynchronous scans preserve token text containing control characters.

Changes: Scan JSON serialization

Layer / File(s) Summary
Token buffer sizing and overflow checks
templates/full/wasm_wrapper.c, versions/18/src/wasm_wrapper.c
Both wrappers calculate escaped token-text lengths, check size arithmetic for overflow, and allocate escaped-text buffers using the calculated lengths.
Control-byte escaping and scan validation
templates/full/wasm_wrapper.c, versions/18/src/wasm_wrapper.c, versions/18/test/scan.test.js
Both wrappers encode otherwise-unhandled control bytes as \u00xx escapes. The test checks token text and order for control characters in dollar-quoted literals and block comments through synchronous and asynchronous scans.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to d13d9

This change fixes invalid JSON produced when scanned token text contains control bytes. Buffer sizing and overflow checks were reviewed, and no merge-blocking risk remains.

Architecture risk: Medium · up to d13d9

The change affects 2 systems.

Changed systems: versions, templates

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — versions (service) was modified; 2 changed files map to changed impact.
  • observed — templates (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in versions/18/test/scan.test.js: Added a test that repeats control characters U+0001–U+001F in a dollar-quoted literal and includes them in a block comment, then checks that both synchronous and asynchronous scan results preserve the expected token text and order.
  • observed — Modified behavior in templates/full/wasm_wrapper.c: Added scan_json_escaped_length, which counts JSON-escaped bytes for quotes, backslashes, newline, carriage return, tab, other control characters, and ordinary bytes; it returns SIZE_MAX if accumulating the length would overflow.
  • observed — Modified behavior in templates/full/wasm_wrapper.c: build_scan_json now estimates each token’s escaped-text space from its actual escaped length instead of assuming twice the source length. It returns "out of memory" if adding the fixed per-token allowance would overflow.
  • observed — Modified behavior in templates/full/wasm_wrapper.c: The token-text allocation now casts the nonnegative token length to size_t.

Reliability and maintainability

  • inferred — Risk-relevant change factors for versions: blast_radius_1; direct_dependents_1
  • inferred — Risk-relevant change factors for templates: blast_radius_1; direct_dependents_1
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the scanner JSON output now escapes all control characters. The raw summary confirms this. Bytes below 0x20 now become \u00xx escapes, and the buffer sizing accounts f…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the scanner JSON output now escapes all control characters. The raw summary confirms this. Bytes below 0x20 now become \u00xx escapes, and the buffer sizing accounts for them. The title is specific and matches the PR objectives.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0dbd46b and d13d998.

Files selected for processing: 2

  • templates/full/wasm_wrapper.c
  • versions/18/src/wasm_wrapper.c

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Devin Review found 1 finding. Devin automatically fixed it.

Fixed by Devin (1)
  • Scan failures on large literals — Buffers now sized to the exact escaped length; 75 MiB literal round-trips.

View findings in Devin Review · Follow Devin's work

Devin Review

@pyramation
pyramation merged commit abbbe25 into main Nov 25, 2026
61 checks passed
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