From 2412df4ea74056a5e2dcd58fc02f189d801725d7 Mon Sep 17 00:00:00 2001 From: Kuan-Wei Chiu Date: Sat, 11 Oct 2025 04:23:05 +0800 Subject: [PATCH] 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. --- src/globals.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/globals.c b/src/globals.c index 998ae862..b65d608f 100644 --- a/src/globals.c +++ b/src/globals.c @@ -1395,7 +1395,8 @@ void error(char *msg) start_idx = offset + 1; for (offset = 0; - offset < MAX_SOURCE && SOURCE->elements[start_idx + offset] != '\n'; + offset < MAX_SOURCE && (start_idx + offset) < SOURCE->size && + SOURCE->elements[start_idx + offset] != '\n'; offset++) { diagnostic[i++] = SOURCE->elements[start_idx + offset]; }