Skip to content

fix(scan): throw when the scan result does not fit in memory - #184

Closed
pyramation wants to merge 1 commit into
mainfrom
fix/scan-oom-throws
Closed

pyramation wants to merge 1 commit into
mainfrom
fix/scan-oom-throws

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

Summary

scan()/scanSync() returned { version: 0, tokens: [] } with no error when the JSON result could not be allocated. Large inputs therefore looked like empty SQL. Example: 'SELECT 1;\n'.repeat(2_000_000) (20 MB, 6M tokens) gives tokens.length === 0 on 18.2.1 (and 18.2.0), while parse() handles the same input without trouble.

The cause is build_scan_json. It pre-sizes the buffer at about 200 bytes per token, so the buffer passes the 1 GiB WASM cap at roughly 5M tokens. When malloc failed, the function returned an empty JSON literal. A failed malloc for a single token's text was also skipped with continue, which silently dropped that token.

Now any of those allocation failures makes build_scan_json return NULL, and wasm_scan returns "ERROR: out of memory building the scan result". The existing JS check startsWith('ERROR') turns that into a thrown Error. The module stays usable after the throw.

Regression test added to versions/18/test/scan.test.js:

  • With the published 18.2.1 WASM it fails (26/27).
  • With this build it passes. The full v18 suite is 144/144.

An 8.8 MB real-world file (830k tokens) scans to byte-identical JSON before and after the change.

Link to Devin session: https://app.devin.ai/sessions/cf0e109c71cf4f2a9be8f31ede68e369
Open in Devin Desktop: https://app.devin.ai/desktop/session/cf0e109c71cf4f2a9be8f31ede68e369?variant=devin
Requested by: @pyramation

@devin-ai-integration

Copy link
Copy Markdown

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

@tenki-reviewer

tenki-reviewer Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 1 nitpick below.

🧹 Nitpicks (1) — 🟢 1 low
  • 🟢 OOM fallback safe_strdup can return NULL across FFI (wasm_wrapper.c:407) — The new out-of-memory fallback return json_result ? json_result : safe_strdup("ERROR: out of memory building the scan result") can itself return NULL: safe_strdup returns NULL when strdup fails (versions/18/src/wasm_wrapper.c:19), and this branch is only reached when the WASM heap is already exhausted.

This PR makes wasm_scan return an explicit ERROR: out of memory... string instead of crashing when the JSON result buffer cannot be allocated, in both the template wrapper and the generated v18 copy, and adds an OOM regression test using a very large multi-statement SQL string.

Files Change
templates/full/wasm_wrapper.c, versions/18/src/wasm_wrapper.c Fallback to an out-of-memory error string when the scan-result JSON buffer fails to allocate.
versions/18/test/scan.test.js New test asserting a huge input triggers the OOM error and that a subsequent scanSync still works.

Reviewed commit: fbd8177

@devin-ai-integration

Copy link
Copy Markdown

Closing in favor of #183, which consolidates the v18 scan/fingerprint/normalize error-handling fixes (including the scan out-of-memory → empty tokens case found here). This PR's repro is being ported there as a regression test.

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