Fixed #4868 (Segmentation fault in Preprocessor::handleIncludes())

This commit is contained in:
Daniel Marjamäki 2013-06-21 20:10:46 +02:00
parent e4270bab78
commit bfc40ffe9f
2 changed files with 23 additions and 7 deletions

View File

@ -1970,15 +1970,15 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
if (indent == indentmatch + 1)
elseIsTrue = true;
} else if (!suppressCurrentCodePath && line.compare(0,4,"#if ") == 0) {
if (indent == indentmatch && match_cfg_def(defs, line.substr(4))) {
} else if (line.compare(0,4,"#if ") == 0) {
if (!suppressCurrentCodePath && indent == indentmatch && match_cfg_def(defs, line.substr(4))) {
elseIsTrue = false;
indentmatch++;
}
++indent;
if (indent == indentmatch + 1)
elseIsTrue = true;
elseIsTrue = true; // this value doesn't matter when suppressCurrentCodePath is true
} else if (line.compare(0,6,"#elif ") == 0 || line.compare(0,5,"#else") == 0) {
if (!elseIsTrue) {
if (indentmatch == indent) {
@ -1994,10 +1994,6 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str
}
}
}
if (suppressCurrentCodePath) {
suppressCurrentCodePath = false;
indentmatch = indent;
}
} else if (line.compare(0, 6, "#endif") == 0) {
if (indent > 0)
--indent;

View File

@ -268,6 +268,7 @@ private:
TEST_CASE(def_missingInclude);
TEST_CASE(def_handleIncludes_ifelse1); // problems in handleIncludes for #else
TEST_CASE(def_handleIncludes_ifelse2);
TEST_CASE(def_handleIncludes_ifelse3); // #4868 - crash
TEST_CASE(def_valueWithParentheses); // #3531
@ -3510,6 +3511,25 @@ private:
preprocessor.handleIncludes(code, "test.c", includePaths, defs).find("123"));
}
void def_handleIncludes_ifelse3() { // #4865
const char code[] = "#ifdef A\n"
"#if defined(SOMETHING_NOT_DEFINED)\n"
"#else\n"
"#endif\n"
"#else\n"
"#endif";
Settings settings;
settings.userUndefs.insert("A");
Preprocessor preprocessor(&settings, this);
const std::list<std::string> includePaths;
std::map<std::string,std::string> defs;
defs["B"] = "1";
defs["C"] = "1";
preprocessor.handleIncludes(code, "test.c", includePaths, defs); // don't crash
}
void def_valueWithParentheses() {
// #define should introduce a new symbol regardless of parentheses in the value
// and regardless of white space in weird places (people do this for some reason).