Fix: #2942 (segmentation fault of cppcheck ( #elif (){ ))

http://sourceforge.net/apps/trac/cppcheck/ticket/2942
This commit is contained in:
Reijo Tomperi 2011-08-02 22:06:27 +03:00
parent 6dc7554310
commit ee7b8d53c6
2 changed files with 16 additions and 1 deletions

View File

@ -1451,7 +1451,12 @@ void Preprocessor::simplifyCondition(const std::map<std::string, std::string> &c
Settings settings;
Tokenizer tokenizer(&settings, NULL);
std::istringstream istr(("(" + condition + ")").c_str());
tokenizer.tokenize(istr, "", "", true);
if (!tokenizer.tokenize(istr, "", "", true))
{
// If tokenize returns false, then there is syntax error in the
// code which we can't handle. So stop here.
return;
}
if (Token::Match(tokenizer.tokens(), "( %var% )"))
{

View File

@ -230,6 +230,7 @@ private:
// Test Preprocessor::simplifyCondition
TEST_CASE(simplifyCondition);
TEST_CASE(invalidElIf); // #2942 segfault
}
@ -2925,6 +2926,15 @@ private:
Preprocessor::simplifyCondition(cfg, condition, true);
ASSERT_EQUALS("1", condition);
}
void invalidElIf()
{
// #2942 - segfault
const char code[] = "#elif (){\n";
const Settings settings;
const std::string actual = Preprocessor::getcode(code, "TEST", "test.c", &settings, this);
ASSERT_EQUALS("\n", actual);
}
};
REGISTER_TEST(TestPreprocessor)