diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 9443931fc..6f491e51b 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -2283,7 +2283,7 @@ void Preprocessor::handleIncludes(std::string &code, const std::string &filePath // If endfile is encountered, we have moved to a next file in our stack, // so remove last path in our list. - while ((endfilePos = code.find("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos) { + while (!paths.empty() && (endfilePos = code.find("\n#endfile", endfilePos)) != std::string::npos && endfilePos < pos) { paths.pop_back(); endfilePos += 9; // size of #endfile } diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index ce58b62df..c606bb53a 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -300,6 +300,8 @@ private: TEST_CASE(validateCfg); TEST_CASE(if_sizeof); + + TEST_CASE(double_include); // #5717 } @@ -4021,6 +4023,18 @@ private: preprocessor.preprocess(istr, actual, "file.c"); ASSERT_EQUALS("\nFred & Wilma\n\n\n\n\n", actual[""]); } + + void double_include() { + const char code[] = "int x"; + + Preprocessor preprocessor(nullptr, this); + std::list includePaths; + includePaths.push_back("."); + includePaths.push_back("."); + std::map defs; + std::set pragmaOnce; + preprocessor.handleIncludes(code, "123.h", includePaths, defs, pragmaOnce, std::list()); + } }; REGISTER_TEST(TestPreprocessor)