Skip to content

fix: reject oversized state in deserialize - #370

Open
dekobon wants to merge 1 commit into
tree-sitter:masterfrom
dekobon:fix/scanner-deserialize-bounds
Open

fix: reject oversized state in deserialize#370
dekobon wants to merge 1 commit into
tree-sitter:masterfrom
dekobon:fix/scanner-deserialize-bounds

Conversation

@dekobon

@dekobon dekobon commented Aug 3, 2026

Copy link
Copy Markdown

This PR seeks to fix #368 where in src/scanner.c:135-143, we have the following function:

void tree_sitter_cpp_external_scanner_deserialize(void *payload, const char *buffer, unsigned length) {
    assert(length % sizeof(wchar_t) == 0 && "Can't decode serialized delimiter!");

    Scanner *scanner = (Scanner *)payload;
    scanner->delimiter_length = length / sizeof(wchar_t);
    if (length > 0) {
        memcpy(&scanner->delimiter[0], buffer, length);
    }
}

Here the greater than zero check is insufficiently guarding memcpy.

The variable Scanner is { uint8_t delimiter_length; wchar_t delimiter[16] } (68 bytes total on Linux/macOS, of which 64 are the delimiter; 32 where wchar_t is 2 bytes).

Elsewhere in the file we check to ensure that delimiter_length <= MAX_DELIMITER_LENGTH, but only in this function are we able to break that contract.

I've taken this through ASan and have a PoC of breaking through this guard, but in interest of safety, I think this is all the information needed to see why we need a better guard here.

AI disclosure: used for debugging and running ASan, formatting, and verification - poorly written prose is 100% organic human

Thank you for the great work with tree-sitter!

Downstream issue prompting PR: dekobon/big-code-analysis#1058
Fixes #368

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.

tree_sitter_cpp_external_scanner_deserialize doesn't bound length against the size of the delimiter buffer

1 participant