Fix condition for assuming at EOF

Part of that condition's purpose is to ensure that we read the correct
lexer state; but it's possible now for the fstack to be non-empty
*before* the lexer state is registered, i.e. if there is an error
in the function that registers it.
This causes a NULL pointer deref.
This commit is contained in:
ISSOtm
2024-09-05 15:24:17 +02:00
parent e289387b09
commit 610f04beeb
7 changed files with 19 additions and 5 deletions

View File

@@ -131,6 +131,7 @@ static inline void lexer_SetGfxDigits(char const digits[4]) {
gfxDigits[3] = digits[3];
}
bool lexer_AtTopLevel();
void lexer_RestartRept(uint32_t lineNo);
void lexer_Init();
void lexer_SetMode(LexerMode mode);

View File

@@ -73,7 +73,7 @@ std::string const &FileStackNode::dump(uint32_t curLineNo) const {
}
void fstk_DumpCurrent() {
if (contextStack.empty()) {
if (lexer_AtTopLevel()) {
fputs("at top level", stderr);
return;
}

View File

@@ -338,6 +338,10 @@ static bool isWhitespace(int c) {
static LexerState *lexerState = nullptr;
static LexerState *lexerStateEOL = nullptr;
bool lexer_AtTopLevel() {
return lexerState == nullptr;
}
void LexerState::clear(uint32_t lineNo_) {
mode = LEXER_NORMAL;
atLineStart = true; // yylex() will init colNo due to this

View File

@@ -0,0 +1 @@
INCLUDE "nonexist-include.inc"

View File

@@ -0,0 +1,3 @@
error: nonexist-include.asm(1):
Unable to open included file 'nonexist-include.inc': No such file or directory
error: Assembly aborted (1 error)!

4
test/asm/notexist.err Normal file
View File

@@ -0,0 +1,4 @@
error: at top level:
Failed to stat file "notexist.asm": No such file or directory
FATAL: at top level:
Failed to open main file

View File

@@ -60,13 +60,13 @@ else
rm -f version.asm
fi
for i in *.asm; do
for i in *.asm notexist.asm; do
flags=${i%.asm}.flags
RGBASMFLAGS=-Weverything
if [ -f "$flags" ]; then
RGBASMFLAGS="$(head -n 1 "$flags")" # Allow other lines to serve as comments
fi
for variant in '' '.pipe'; do
for variant in '' ' piped'; do
(( tests++ ))
echo "${bold}${green}${i%.asm}${variant}...${rescolors}${resbold}"
if [ -e "${i%.asm}.out" ]; then
@@ -85,8 +85,9 @@ for i in *.asm; do
desired_errput=$desired_errname
else
# `include-recursion.asm` refers to its own name inside the test code.
# Skip testing with stdin input for that file.
if [ "$i" = "include-recursion.asm" ]; then
# "notexist" doesn't exist, so there's no point in trying to `cat` it.
# Skip testing with stdin input for those file.
if [[ "$i" = include-recursion.asm || "$i" = notexist.asm ]]; then
continue
fi