Fix crash on infinite macro expansion

This commit is contained in:
drmortalwombat 2023-08-06 11:51:11 +02:00
parent 1931f25475
commit 8161ff88a8
4 changed files with 13 additions and 2 deletions

View File

@ -35,7 +35,7 @@ void Errors::Error(const Location& loc, ErrorID eid, const char* msg, const char
else
fprintf(stderr, "%s(%d, %d) : %s %d: %s\n", loc.mFileName, loc.mLine, loc.mColumn, level, eid, msg);
if (mErrorCount > 10)
if (mErrorCount > 10 || eid >= EFATAL_GENERIC)
exit(20);
}

View File

@ -36,7 +36,6 @@ enum ErrorID
EERR_RUNTIME_CODE,
EERR_UNIMPLEMENTED,
EERR_COMMAND_LINE,
EERR_OUT_OF_MEMORY,
EERR_OBJECT_NOT_FOUND,
EERR_SYNTAX,
EERR_EXECUTION_FAILED,
@ -86,6 +85,10 @@ enum ErrorID
ERRR_INVALID_NUMBER,
EERR_INVALID_PREPROCESSOR,
EFATAL_GENERIC = 4000,
EFATAL_OUT_OF_MEMORY,
EFATAL_MACRO_EXPANSION_DEPTH,
};
class Errors

View File

@ -318,6 +318,7 @@ Scanner::Scanner(Errors* errors, Preprocessor* preprocessor)
mAssemblerMode = false;
mPreprocessorMode = false;
mMacroExpansion = nullptr;
mMacroExpansionDepth = 0;
mDefines = new MacroDict();
mDefineArguments = nullptr;
@ -817,6 +818,9 @@ void Scanner::NextToken(void)
ex->mChar = mTokenChar;
mMacroExpansion = ex;
mMacroExpansionDepth++;
if (mMacroExpansionDepth > 1024)
mErrors->Error(mLocation, EFATAL_MACRO_EXPANSION_DEPTH, "Maximum macro expansion depth exceeded", mTokenIdent);
mLine = def->mString;
mOffset = 0;
NextChar();
@ -1812,6 +1816,8 @@ bool Scanner::NextChar(void)
delete mMacroExpansion;
mMacroExpansion = mac;
mMacroExpansionDepth--;
return true;
}
else if (mPreprocessor->NextLine())

View File

@ -259,6 +259,8 @@ protected:
MacroDict* mDefinedArguments;
} * mMacroExpansion;
int mMacroExpansionDepth;
MacroDict* mDefines, * mDefineArguments;
Token mUngetToken;