diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 82e3f2bbc..6771858fd 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1451,7 +1451,12 @@ void Preprocessor::simplifyCondition(const std::map &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% )")) { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index a33c2a641..87b7994ab 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -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)