#5909 crash: clang: test/Preprocessor/ifdef-recover.c. Avoid segfault in Preprocessor::getcfgs() on invalid code.

This commit is contained in:
Alexander Mai 2014-06-08 10:02:16 +02:00
parent 56e3e150c2
commit e1513090e2
2 changed files with 24 additions and 2 deletions

View File

@ -1432,7 +1432,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
} }
else if (line.compare(0, 5, "#else") == 0 && ! deflist.empty()) { else if (line.compare(0, 5, "#else") == 0 && ! deflist.empty()) {
if (deflist.back() == "!") { if (deflist.back() == "!" && !ndeflist.empty()) {
deflist.back() = ndeflist.back(); deflist.back() = ndeflist.back();
ndeflist.pop_back(); ndeflist.pop_back();
} else { } else {
@ -1442,7 +1442,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
} }
else if (line.compare(0, 6, "#endif") == 0 && ! deflist.empty()) { else if (line.compare(0, 6, "#endif") == 0 && ! deflist.empty()) {
if (deflist.back() == "!") if (deflist.back() == "!" && !ndeflist.empty())
ndeflist.pop_back(); ndeflist.pop_back();
deflist.pop_back(); deflist.pop_back();
} }

View File

@ -302,6 +302,7 @@ private:
TEST_CASE(if_sizeof); TEST_CASE(if_sizeof);
TEST_CASE(double_include); // #5717 TEST_CASE(double_include); // #5717
TEST_CASE(invalid_ifs)// #5909
} }
@ -4042,6 +4043,27 @@ private:
std::set<std::string> pragmaOnce; std::set<std::string> pragmaOnce;
preprocessor.handleIncludes(code, "123.h", includePaths, defs, pragmaOnce, std::list<std::string>()); preprocessor.handleIncludes(code, "123.h", includePaths, defs, pragmaOnce, std::list<std::string>());
} }
void invalid_ifs() {
const char filedata[] = "#ifdef\n"
"#endif\n"
"#ifdef !\n"
"#endif\n"
"#if defined\n"
"#endif\n"
"#define f(x) x\n"
"#if f(2\n"
"#endif\n"
"int x;\n";
// Preprocess => don't crash..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
}
}; };
REGISTER_TEST(TestPreprocessor) REGISTER_TEST(TestPreprocessor)