fix(scan): size build_scan_json buffer from token text length, never write past it - #168
Merged
Merged
Conversation
…write past it Fixes #167
🤖 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:
|
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 #167.
build_scan_json()(v18 only) sized its output buffer from the token count (1024 + 200*n_tokens) and only grew it after a boundedsnprintfhad already clipped a write. Becauseposaccumulatedsnprintf's would-have-written length,poscould exceedestimated_size, makingestimated_size - posunderflow and the nextsnprintfwrite past the allocation.Change (in
templates/full/wasm_wrapper.c, propagated toversions/18/src/wasm_wrapper.c):1024 + Σ (token_len*2 + 200)— 2× covers worst-case JSON escaping, 200 covers the fixed per-token JSON (max ~170 bytes incl. commas).reallocpath.posis nowsize_t, each append is guarded bypos < estimated_size, andposis clamped toestimated_size - 1soestimated_size - poscan never underflow even if the estimate were ever wrong.Regression tests added to
versions/18/test/scan.test.js: 1400 / 5000 / 300000-char literals followed by more statements, and long dollar-quoted tokens full of"\\n\t. Verified the new tests fail on publishedlibpg-query@18.1.4(Unterminated string in JSON at position 1623) and pass on the rebuilt WASM; the issue's Part 2 heap-clobber probe reports no clobber against the rebuilt module.Link to Devin session: https://app.devin.ai/sessions/332ad0c592c8445dae0d0e498973ca79
Open in Devin Desktop: https://app.devin.ai/desktop/session/332ad0c592c8445dae0d0e498973ca79?variant=devin
Requested by: @pyramation