Fixed #2605 (cppcheck hangs with 100% cpu load ( #define = ))

This commit is contained in:
Daniel Marjamäki 2011-02-22 19:55:02 +01:00
parent dd12fc177f
commit 9983aa5721
2 changed files with 26 additions and 1 deletions

View File

@ -927,7 +927,18 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
if (line.compare(0, 8, "#define ") == 0)
{
if (line.find(" ", 8) == std::string::npos)
bool valid = true;
for (std::string::size_type pos = 8; pos < line.size() && line[pos] != ' '; ++pos)
{
char ch = line[pos];
if (ch=='_' || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (pos>8 && ch>='0' && ch<='9'))
continue;
valid = false;
break;
}
if (!valid)
line.clear();
else if (line.find(" ", 8) == std::string::npos)
defines.insert(line.substr(8));
else
{

View File

@ -206,6 +206,8 @@ private:
TEST_CASE(testPreprocessorRead2);
TEST_CASE(testPreprocessorRead3);
TEST_CASE(testPreprocessorRead4);
TEST_CASE(invalid_define); // #2605 - hang for: '#define ='
}
@ -2720,6 +2722,18 @@ private:
ASSERT_EQUALS("#define A \" \\\\\\\\\" \" \"", preprocessor.read(istr, "test.cpp", 0));
}
}
void invalid_define()
{
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream src("#define =\n");
std::string processedFile;
std::list<std::string> cfg;
std::list<std::string> paths;
preprocessor.preprocess(src, processedFile, cfg, "", paths); // don't hang
}
};
REGISTER_TEST(TestPreprocessor)