From f8710cb98458c48a483d87d2d5d7d135948bc8f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 18 Oct 2013 06:47:55 +0200 Subject: [PATCH] Fixed #5102 (Crash if -D is used when scanning simple preprocessor code.) --- lib/preprocessor.cpp | 5 +++++ test/testpreprocessor.cpp | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 5e47bf5c1..4d6d37081 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -2052,6 +2052,11 @@ std::string Preprocessor::handleIncludes(const std::string &code, const std::str } } + if (elseIsTrueStack.empty()) { + writeError(filePath, linenr, _errorLogger, "syntaxError", "Syntax error in preprocessor code"); + return ""; + } + std::stack::reference elseIsTrue = elseIsTrueStack.top(); if (line == "#pragma once") { diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 2641f609e..f111228c7 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -93,6 +93,7 @@ private: TEST_CASE(test7d); TEST_CASE(test7e); TEST_CASE(test8); // #if A==1 => cfg: A=1 + TEST_CASE(test9); // Don't crash for invalid code // #error => don't extract any code TEST_CASE(error1); @@ -778,6 +779,21 @@ private: ASSERT_EQUALS("\n1\n\n", actual["A=1"]); } + void test9() { + const char filedata[] = "#if\n" + "#else\n" + "#endif\n"; + + // Preprocess => actual result.. + std::istringstream istr(filedata); + std::map actual; + Settings settings; + settings._maxConfigs = 1; + settings.userDefines = "X"; + Preprocessor preprocessor(&settings, this); + preprocessor.preprocess(istr, actual, "file.c"); // <- don't crash + } + void error1() { const char filedata[] = "#ifdef A\n" ";\n"