Fixed #5102 (Crash if -D is used when scanning simple preprocessor code.)

This commit is contained in:
Daniel Marjamäki 2013-10-18 06:47:55 +02:00
parent 072c80bde8
commit f8710cb984
2 changed files with 21 additions and 0 deletions

View File

@ -2052,6 +2052,11 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
}
}
if (elseIsTrueStack.empty()) {
writeError(filePath, linenr, _errorLogger, "syntaxError", "Syntax error in preprocessor code");
return "";
}
std::stack<bool>::reference elseIsTrue = elseIsTrueStack.top();
if (line == "#pragma once") {

View File

@ -93,6 +93,7 @@ private:
TEST_CASE(test7d);
TEST_CASE(test7e);
TEST_CASE(test8); // #if A==1 => cfg: A=1
TEST_CASE(test9); // Don't crash for invalid code
// #error => don't extract any code
TEST_CASE(error1);
@ -778,6 +779,21 @@ private:
ASSERT_EQUALS("\n1\n\n", actual["A=1"]);
}
void test9() {
const char filedata[] = "#if\n"
"#else\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
settings._maxConfigs = 1;
settings.userDefines = "X";
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c"); // <- don't crash
}
void error1() {
const char filedata[] = "#ifdef A\n"
";\n"