Fix buffer overflow in block scanner serialize()#1
Open
rtfeldman wants to merge 2 commits into
Open
Conversation
6905ffc to
b6a6163
Compare
open_blocks can grow past the fixed TREE_SITTER_SERIALIZATION_BUFFER_SIZE buffer for deeply nested markdown; memcpy'ing them overflowed the buffer and corrupted the parser's adjacent state. Bail out (serialize no state, so the scanner deserializes fresh) when the state won't fit. Upstream report: tree-sitter-grammars#243
The deep-nesting test aborts on the unguarded scanner (debug tree-sitter asserts in ts_parser__external_scanner_serialize; release builds corrupt parser state instead) and must complete safely with the guard. The moderate-nesting test pins that inputs fitting the buffer still parse cleanly.
d4c6b85 to
d2e7d1c
Compare
Veykril
approved these changes
Jul 7, 2026
pull Bot
pushed a commit
to gaetschwartz/zed
that referenced
this pull request
Jul 7, 2026
…tries#60312) Zed bundles the markdown grammar's block scanner natively, and its `serialize()` `memcpy`s the open-block stack into tree-sitter's fixed 1024-byte serialization buffer with no bounds check. Markdown with roughly 255+ nested blocks overflows that buffer, and because it sits at the front of `struct TSParser`, the overflow clobbers the adjacent parse-stack pointer and heap. Debug builds of the tree-sitter runtime catch this with an assertion, but release builds like Zed's have no check and silently corrupt parser memory — which is why this surfaced as wild crashes deep in tree-sitter's parse stack rather than clean failures. Still open upstream as tree-sitter-grammars/tree-sitter-markdown#243. This PR points `tree-sitter-md` at a `zed-industries` fork whose `serialize()` refuses to write state that doesn't fit (bounded by the same running counter the header writes advance, so the check can't drift). The scanner then deserializes to a fresh state, and the pathologically nested region surfaces as ordinary tree-sitter `ERROR` nodes — visible, safe degradation for an adversarial input class, deliberately chosen over the two alternatives: truncating the block stack would deserialize into a plausible-but-wrong state and produce silently incorrect trees, and the scanner ABI offers no error channel at all (`serialize()` returns a length into a fixed buffer; there is no way to fail a parse). A nesting-depth cap at block-open time would give fully deterministic semantics, but that's a behavior change across 13 scanner call sites that belongs upstream, not in a hotfix fork. The pinned branch is upstream's `9a23c1a9` (the revision Zed already pinned) plus exactly two commits, for easy review: the guard (zed-industries/tree-sitter-markdown@179422e) and regression tests (zed-industries/tree-sitter-markdown@b596e73). The deep-nesting test aborts on the unguarded scanner and passes with the guard; a moderate-nesting test pins that inputs fitting the buffer still parse cleanly. The same change is also up as zed-industries/tree-sitter-markdown#1 into the fork's default branch (`split_parser`), so future pin bumps don't lose it; if upstream fixes zed-industries#243, we can drop the fork entirely on the next bump. Closes FR-115 Release Notes: - Fixed a potential crash when editing Markdown with deeply nested blocks
TakuroBreath
pushed a commit
to TakuroBreath/zed
that referenced
this pull request
Jul 7, 2026
…tries#60312) Zed bundles the markdown grammar's block scanner natively, and its `serialize()` `memcpy`s the open-block stack into tree-sitter's fixed 1024-byte serialization buffer with no bounds check. Markdown with roughly 255+ nested blocks overflows that buffer, and because it sits at the front of `struct TSParser`, the overflow clobbers the adjacent parse-stack pointer and heap. Debug builds of the tree-sitter runtime catch this with an assertion, but release builds like Zed's have no check and silently corrupt parser memory — which is why this surfaced as wild crashes deep in tree-sitter's parse stack rather than clean failures. Still open upstream as tree-sitter-grammars/tree-sitter-markdown#243. This PR points `tree-sitter-md` at a `zed-industries` fork whose `serialize()` refuses to write state that doesn't fit (bounded by the same running counter the header writes advance, so the check can't drift). The scanner then deserializes to a fresh state, and the pathologically nested region surfaces as ordinary tree-sitter `ERROR` nodes — visible, safe degradation for an adversarial input class, deliberately chosen over the two alternatives: truncating the block stack would deserialize into a plausible-but-wrong state and produce silently incorrect trees, and the scanner ABI offers no error channel at all (`serialize()` returns a length into a fixed buffer; there is no way to fail a parse). A nesting-depth cap at block-open time would give fully deterministic semantics, but that's a behavior change across 13 scanner call sites that belongs upstream, not in a hotfix fork. The pinned branch is upstream's `9a23c1a9` (the revision Zed already pinned) plus exactly two commits, for easy review: the guard (zed-industries/tree-sitter-markdown@179422e) and regression tests (zed-industries/tree-sitter-markdown@b596e73). The deep-nesting test aborts on the unguarded scanner and passes with the guard; a moderate-nesting test pins that inputs fitting the buffer still parse cleanly. The same change is also up as zed-industries/tree-sitter-markdown#1 into the fork's default branch (`split_parser`), so future pin bumps don't lose it; if upstream fixes zed-industries#243, we can drop the fork entirely on the next bump. Closes FR-115 Release Notes: - Fixed a potential crash when editing Markdown with deeply nested blocks
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.
The block scanner's
serialize()copiesopen_blocksinto tree-sitter's fixedTREE_SITTER_SERIALIZATION_BUFFER_SIZE(1024-byte) buffer with no bounds check. Deeply nested markdown (roughly 255+ open blocks) overflows the buffer; since it lives at the front ofstruct TSParser, the overflow clobbers the adjacent parse-stack pointer and heap, corrupting the parser and crashing later inside tree-sitter's stack management. This bails out of serialization when the state won't fit (serializing zero bytes, so the scanner deserializes to a fresh state) rather than writing out of bounds — degraded incremental-parse fidelity in that pathological case, instead of memory corruption.Reported upstream as tree-sitter-grammars#243. Zed currently pins this same fix rebased onto its pinned revision
9a23c1a(commit9506f128, via zed-industries/zed#60312); this PR brings it ontosplit_parserso the fork's default branch carries it and future pin bumps don't lose it.