Skip to content

Commit b7c50fa

Browse files
authored
Merge pull request #515 from avaraline/fix-parser-crash
wrap std::stod in try/catch to prevent crashes when garbage is parsed
2 parents 1dee183 + b575a6d commit b7c50fa

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/level/Parser.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939

4040
typedef short tokentype;
4141

42+
std::string currentlyRunning = "";
43+
4244
ParserVariables parserVar = {0, 0, 0, 0, {0}, 0, 0};
4345
long lastKeyword = 0;
4446
long lastVariable = 0;
@@ -515,7 +517,14 @@ void LexRead(LexSymbol *theSymbol) {
515517
if (matchCount > 0) {
516518
std::string s((char *)parserVar.input, matchCount);
517519
theSymbol->kind = kLexConstant;
518-
theSymbol->value.floating = std::stod(s);
520+
double floatResult = 0;
521+
try {
522+
floatResult = std::stod(s);
523+
} catch (...) {
524+
SDL_Log("Parser error: Invalid floating point value: %s", s.c_str());
525+
SDL_Log("In script: '%s'", currentlyRunning.c_str());
526+
}
527+
theSymbol->value.floating = floatResult;
519528
// SDL_Log("\natof(%s) --> %f\n", tempString, theSymbol->value.floating);
520529
parserVar.input += matchCount;
521530
}
@@ -825,6 +834,7 @@ void ParseStatement(LexSymbol *statement) {
825834
}
826835
}
827836

837+
828838
void SetupCompiler(unsigned char *theInput) {
829839
parserVar.input = theInput;
830840
parserVar.output = NewHandle(1024);
@@ -970,6 +980,7 @@ char *fixedString(unsigned char *s) {
970980
}
971981

972982
void RunThis(std::string script) {
983+
currentlyRunning = script;
973984
LexSymbol statement;
974985

975986
//char *scriptPtr = (StringPtr)script.c_str();

0 commit comments

Comments
 (0)