diff --git a/src/pipeline/pass_calls.c b/src/pipeline/pass_calls.c index e5cdbf76a..b0b28371e 100644 --- a/src/pipeline/pass_calls.c +++ b/src/pipeline/pass_calls.c @@ -46,7 +46,9 @@ static char *read_file(const char *path, int *out_len) { return NULL; } - char *buf = malloc(size + SKIP_ONE); + /* +pad: tree-sitter lexer lookahead reads past EOF; keep it in-bounds */ + enum { CBM_TS_LOOKAHEAD_PAD = 16 }; + char *buf = malloc((size_t)size + CBM_TS_LOOKAHEAD_PAD); if (!buf) { (void)fclose(f); return NULL; @@ -58,7 +60,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; } diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c index b03e65a1a..383c57cf8 100644 --- a/src/pipeline/pass_k8s.c +++ b/src/pipeline/pass_k8s.c @@ -44,7 +44,9 @@ static char *k8s_read_file(const char *path, int *out_len) { return NULL; } - char *buf = malloc(size + SKIP_ONE); + /* +pad: tree-sitter lexer lookahead reads past EOF; keep it in-bounds */ + enum { CBM_TS_LOOKAHEAD_PAD = 16 }; + char *buf = malloc((size_t)size + CBM_TS_LOOKAHEAD_PAD); if (!buf) { (void)fclose(f); return NULL; @@ -55,7 +57,7 @@ static char *k8s_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; } diff --git a/src/pipeline/pass_lsp_cross.c b/src/pipeline/pass_lsp_cross.c index fe636801f..fa1a06a91 100644 --- a/src/pipeline/pass_lsp_cross.c +++ b/src/pipeline/pass_lsp_cross.c @@ -64,7 +64,9 @@ static char *pxc_read_file(const char *path, int *out_len) { (void)fclose(f); return NULL; } - char *buf = (char *)malloc((size_t)size + 1); + /* +pad: tree-sitter lexer lookahead reads past EOF; keep it in-bounds */ + enum { CBM_TS_LOOKAHEAD_PAD = 16 }; + char *buf = (char *)malloc((size_t)size + CBM_TS_LOOKAHEAD_PAD); if (!buf) { (void)fclose(f); return NULL; @@ -73,7 +75,7 @@ static char *pxc_read_file(const char *path, int *out_len) { (void)fclose(f); 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; } diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.c index 423375c7c..d2231e2d0 100644 --- a/src/pipeline/pass_semantic.c +++ b/src/pipeline/pass_semantic.c @@ -36,7 +36,9 @@ static char *read_file(const char *path, int *out_len) { (void)fclose(f); return NULL; } - char *buf = malloc(size + SKIP_ONE); + /* +pad: tree-sitter lexer lookahead reads past EOF; keep it in-bounds */ + enum { CBM_TS_LOOKAHEAD_PAD = 16 }; + char *buf = malloc((size_t)size + CBM_TS_LOOKAHEAD_PAD); if (!buf) { (void)fclose(f); return NULL; @@ -46,7 +48,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; } diff --git a/src/pipeline/pass_usages.c b/src/pipeline/pass_usages.c index f5b2df971..1afcc7107 100644 --- a/src/pipeline/pass_usages.c +++ b/src/pipeline/pass_usages.c @@ -36,7 +36,9 @@ static char *read_file(const char *path, int *out_len) { (void)fclose(f); return NULL; } - char *buf = malloc(size + SKIP_ONE); + /* +pad: tree-sitter lexer lookahead reads past EOF; keep it in-bounds */ + enum { CBM_TS_LOOKAHEAD_PAD = 16 }; + char *buf = malloc((size_t)size + CBM_TS_LOOKAHEAD_PAD); if (!buf) { (void)fclose(f); return NULL; @@ -46,7 +48,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; }