fix: pad source read buffers in all parse passes (tree-sitter lookahead)

Same fix as read_file in pass_definitions: the calls/usages/semantic/k8s/lsp_cross passes each read a source file into a malloc'd buffer and re-parse it, so they hit the same tree-sitter lexer over-read past EOF under ASan. Over-allocate by 16 and zero the tail in every reader.
This commit is contained in:
Martin Vogel
2026-05-29 06:00:44 +02:00
parent c023931014
commit 90e85bb10c
5 changed files with 20 additions and 10 deletions
+4 -2
View File
@@ -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;
}
+4 -2
View File
@@ -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;
}
+4 -2
View File
@@ -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;
}
+4 -2
View File
@@ -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;
}
+4 -2
View File
@@ -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;
}