mirror of
https://github.com/DeusData/codebase-memory-mcp.git
synced 2026-11-09 09:11:00 +08:00
fix: pad source read buffer for tree-sitter lookahead (ASan over-read)
read_file allocated size+1, but tree-sitter's lexer peeks a few bytes past the final UTF-8 char (ts_lexer_goto), reading past the buffer end. Under ASan this is a heap-buffer-overflow (intermittently flagged in the incremental suite on macOS); in production it is a benign over-read of adjacent heap. Over-allocate by 16 bytes and zero the tail so the lookahead stays in-bounds.
This commit is contained in:
@@ -45,7 +45,12 @@ static char *read_file(const char *path, int *out_len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char *buf = malloc(size + SKIP_ONE);
|
||||
/* +16 padding: tree-sitter's lexer peeks a few bytes past the final UTF-8
|
||||
* character when computing lookahead, reading beyond the logical end.
|
||||
* Over-allocate and zero the tail so that read stays in-bounds (ASan
|
||||
* flags it as a heap-buffer-overflow otherwise; harmless but real UB). */
|
||||
enum { CBM_TS_LOOKAHEAD_PAD = 16 };
|
||||
char *buf = malloc((size_t)size + CBM_TS_LOOKAHEAD_PAD);
|
||||
if (!buf) {
|
||||
(void)fclose(f);
|
||||
return NULL;
|
||||
@@ -57,7 +62,7 @@ static char *read_file(const char *path, int *out_len) {
|
||||
if (nread > (size_t)size) {
|
||||
nread = (size_t)size;
|
||||
}
|
||||
buf[nread] = '\0';
|
||||
memset(buf + nread, 0, CBM_TS_LOOKAHEAD_PAD);
|
||||
*out_len = (int)nread;
|
||||
return buf;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user