From ad3188f038581c21bcf992d2e9fe69f2f44ceb26 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Thu, 4 Dec 2025 11:21:06 -0500 Subject: [PATCH] Fix garbage characters at EOF causing an infinite loop --- src/asm/lexer.cpp | 6 +++++- test/asm/garbage-before-eof.asm | 1 + test/asm/garbage-before-eof.err | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 test/asm/garbage-before-eof.asm create mode 100644 test/asm/garbage-before-eof.err diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 4889cede..8ba89b2c 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -1636,6 +1636,10 @@ static Token yylex_SKIP_TO_ENDC(); // Forward declaration for `yylex_NORMAL` // Must stay in sync with the `switch` in `yylex_NORMAL`! static bool isGarbageCharacter(int c) { + // EOF is not garbage (it can't be reported anyway) + if (c == EOF) { + return false; + } // Whitespace characters are not garbage, even the non-"printable" ones if (isWhitespace(c)) { return false; @@ -1645,7 +1649,7 @@ static bool isGarbageCharacter(int c) { return true; } // All other printable characters are not garbage (i.e. `yylex_NORMAL` handles them), and - // all other nonprintable characters are garbage (including '\0' and EOF) + // all other nonprintable characters are garbage (including '\0') return !isPrintable(c); } diff --git a/test/asm/garbage-before-eof.asm b/test/asm/garbage-before-eof.asm new file mode 100644 index 00000000..f9dd66f4 --- /dev/null +++ b/test/asm/garbage-before-eof.asm @@ -0,0 +1 @@ +MACROë \ No newline at end of file diff --git a/test/asm/garbage-before-eof.err b/test/asm/garbage-before-eof.err new file mode 100644 index 00000000..e5190e81 --- /dev/null +++ b/test/asm/garbage-before-eof.err @@ -0,0 +1,5 @@ +error: Invalid character 0xEB (is the file UTF-8?) + at garbage-before-eof.asm(1) +error: syntax error, unexpected end of buffer, expecting ? or symbol + at garbage-before-eof.asm(1) +Assembly aborted with 2 errors!