Skip to content

Commit 2412df4

Browse files
committed
Fix a crash when reporting an error at the end of a file
The compiler crashes with a segmentation fault when an unterminated C-style comment exists at the very end of a file. The root cause is a buffer over-read in the error() function, which attempts to construct a diagnostic message by reading the source line containing the error. When the error is on the last line of a file without a trailing newline, this logic would read past the end of the source buffer. Fix the issue by adding a bounds check to the loop, ensuring it does not read beyond the source buffer's size. This allows the compiler to correctly report the "Unenclosed C-style comment" error instead of crashing.
1 parent 4da0b6e commit 2412df4

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/globals.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,8 @@ void error(char *msg)
13951395
start_idx = offset + 1;
13961396

13971397
for (offset = 0;
1398-
offset < MAX_SOURCE && SOURCE->elements[start_idx + offset] != '\n';
1398+
offset < MAX_SOURCE && (start_idx + offset) < SOURCE->size &&
1399+
SOURCE->elements[start_idx + offset] != '\n';
13991400
offset++) {
14001401
diagnostic[i++] = SOURCE->elements[start_idx + offset];
14011402
}

0 commit comments

Comments
 (0)