diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 6e5e743cc..61ad4a6eb 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1001,13 +1001,17 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const } if (line.compare(0, 8, "#define ") == 0) { - bool valid = true; + bool valid = false; for (std::string::size_type pos = 8; pos < line.size(); ++pos) { char ch = line[pos]; - if (ch=='_' || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (pos>8 && ch>='0' && ch<='9')) + if (ch=='_' || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (pos>8 && ch>='0' && ch<='9')) { + valid = true; continue; - if (ch==' ' || ch=='(') - break; + } + if (ch==' ' || ch=='(') { + if (valid) + break; + } valid = false; break; } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index d059e81ec..ea83acf92 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -232,7 +232,8 @@ private: TEST_CASE(testPreprocessorRead3); TEST_CASE(testPreprocessorRead4); - TEST_CASE(invalid_define); // #2605 - hang for: '#define =' + TEST_CASE(invalid_define_1); // #2605 - hang for: '#define =' + TEST_CASE(invalid_define_2); // #4036 - hang for: '#define () {(int f(x) }' // Show 'missing include' warnings TEST_CASE(missingInclude); @@ -2955,7 +2956,7 @@ private: } } - void invalid_define() { + void invalid_define_1() { Settings settings; Preprocessor preprocessor(&settings, this); @@ -2966,6 +2967,17 @@ private: preprocessor.preprocess(src, processedFile, cfg, "", paths); // don't hang } + void invalid_define_2() { // #4036 - hang + Settings settings; + Preprocessor preprocessor(&settings, this); + + std::istringstream src("#define () {(int f(x) }\n"); + std::string processedFile; + std::list cfg; + std::list paths; + preprocessor.preprocess(src, processedFile, cfg, "", paths); // don't hang + } + void missingInclude() { Settings settings; Preprocessor preprocessor(&settings, this);