fix: escape control characters in build_scan_json() for multi-line tokens#147
Merged
pyramation merged 2 commits intomainfrom Apr 8, 2026
Merged
Conversation
…kens The JSON escape loop in build_scan_json() only handled '"' and '\', but not '\n', '\r', '\t'. When token text contains literal newlines (e.g., dollar-quoted function bodies like $$\nBEGIN\n...$$), the raw JSON string had unescaped control characters, causing JSON.parse to throw 'Bad control character in string literal'. This adds proper escape sequences for \n, \r, and \t in the token text escaping loop, matching standard JSON string escaping rules. New tests added for multi-line dollar-quoted strings, tabs, and multi-line C-style comments.
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
devin-ai-integration bot
pushed a commit
that referenced
this pull request
Apr 8, 2026
These tests demonstrate that scanSync throws 'Bad control character in string literal' when scanning SQL with multi-line tokens (dollar-quoted function bodies, tabs, multi-line C-style comments). The root cause is that build_scan_json() in wasm_wrapper.c only escapes '"' and '\\' in token text, but not '\n', '\r', '\t'. When token text contains literal newlines, the JSON output has unescaped control chars that break JSON.parse. These tests are expected to FAIL on this branch (no fix applied). See PR #147 for the fix.
3 tasks
5 tasks
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.
Summary
Fixes a bug in
build_scan_json()where literal control characters (\n,\r,\t) in token text were written directly into the JSON string without escaping. This causedJSON.parseto throw "Bad control character in string literal" when scanning SQL containing multi-line tokens (e.g., dollar-quoted function bodies like$$\nBEGIN\n...$$or multi-line/* */comments).The existing escape loop only handled
"and\. This adds the three missing control character cases to produce valid JSON escape sequences (\n→\\n, etc.).Three new tests added covering dollar-quoted strings with newlines, dollar-quoted tokens with tabs, and multi-line block comments. Tests use template literals with actual newlines/tabs so the multi-line SQL is visually obvious in the source code.
Review & Testing Checklist for Human
wasm_wrapper.c:379-396): The original code used a flatif+ unconditionalescaped_text[escaped_pos++] = cwhich worked for"and\but silently passed through control chars. The new code uses anif/else if/elsechain where each branch writes exactly the right pair of characters. Confirm no character is double-written or skipped.escaped_textis allocated astoken_length * 2 + 1. Worst case is every char needing a 2-byte escape, so this is sufficient. But worth a glance.\n,\r,\tare handled. Other control chars are technically invalid in JSON strings but extremely unlikely in PostgreSQL token text. Decide if a generalc < 0x20guard is worth adding.Test plan: The new tests will only pass after a WASM rebuild (they exercise the C code path). CI should rebuild and run them. To verify manually: rebuild WASM with
pnpm wasm:buildin/full, then runnode --test test/scan.test.jsand confirm all tests pass, especially the three new multi-line token tests.See also PR #148 which contains only these same tests without the C fix — CI fails there with the exact
"Bad control character in string literal"errors, proving the fix is necessary.Notes
scanandscanSyncfunctions inindex.cjscallJSON.parseon the raw string returned by_wasm_scan. This fix ensures that string is always valid JSON.pgsql-parse(PR #290 in pgsql-parser) has asafeScanSync()workaround that monkey-patchesJSON.parseto fix this at the JS level. Once this fix is published, that workaround can be removed.Link to Devin session: https://app.devin.ai/sessions/67facbcfe0ae424bad3eafb4e6ca9059
Requested by: @pyramation