Skip to content

Fix buffer overflow in block scanner serialize()#1

Open
rtfeldman wants to merge 2 commits into
split_parserfrom
fix-serialize-buffer-overflow
Open

Fix buffer overflow in block scanner serialize()#1
rtfeldman wants to merge 2 commits into
split_parserfrom
fix-serialize-buffer-overflow

Conversation

@rtfeldman

Copy link
Copy Markdown
Collaborator

The block scanner's serialize() copies open_blocks into tree-sitter's fixed TREE_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 of struct 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 (commit 9506f128, via zed-industries/zed#60312); this PR brings it onto split_parser so the fork's default branch carries it and future pin bumps don't lose it.

rtfeldman and others added 2 commits July 6, 2026 10:00
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.
@rtfeldman rtfeldman force-pushed the fix-serialize-buffer-overflow branch from d4c6b85 to d2e7d1c Compare July 6, 2026 14:00
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
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.

2 participants