From 4f00189ee1827988c198fb03c9c2609e9a14e6b2 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Tue, 12 May 2015 14:00:43 +0200 Subject: [PATCH] Fixed crash #6684 --- lib/preprocessor.cpp | 14 +++++++------- test/testpreprocessor.cpp | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 03e9056a7..86b6bea47 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1136,7 +1136,7 @@ std::string Preprocessor::getdef(std::string line, bool def) } /** Simplify variable in variable map. */ -static Token *simplifyVarMapExpandValue(Token *tok, const std::map &variables, std::set seenVariables) +static Token *simplifyVarMapExpandValue(Token *tok, const std::map &variables, std::set seenVariables, const Settings* settings) { // TODO: handle function-macros too. @@ -1147,13 +1147,13 @@ static Token *simplifyVarMapExpandValue(Token *tok, const std::map::const_iterator it = variables.find(tok->str()); if (it != variables.end()) { - TokenList tokenList(nullptr); + TokenList tokenList(settings); std::istringstream istr(it->second); if (tokenList.createTokens(istr)) { // expand token list for (Token *tok2 = tokenList.front(); tok2; tok2 = tok2->next()) { if (tok2->isName()) { - tok2 = simplifyVarMapExpandValue(tok2, variables, seenVariables); + tok2 = simplifyVarMapExpandValue(tok2, variables, seenVariables, settings); } } @@ -1175,16 +1175,16 @@ static Token *simplifyVarMapExpandValue(Token *tok, const std::mapB, B=>1, then A=>B is simplified to A=>1. * @param [in,out] variables - a map of variable name to variable value. This map will be modified. */ -static void simplifyVarMap(std::map &variables) +static void simplifyVarMap(std::map &variables, const Settings* settings) { for (std::map::iterator i = variables.begin(); i != variables.end(); ++i) { - TokenList tokenList(nullptr); + TokenList tokenList(settings); std::istringstream istr(i->second); if (tokenList.createTokens(istr)) { for (Token *tok = tokenList.front(); tok; tok = tok->next()) { if (tok->isName()) { std::set seenVariables; - tok = simplifyVarMapExpandValue(tok, variables, seenVariables); + tok = simplifyVarMapExpandValue(tok, variables, seenVariables, settings); } } @@ -1742,7 +1742,7 @@ bool Preprocessor::match_cfg_def(std::map cfg, std::st std::cout << "def: \"" << def << "\"\n"; */ - simplifyVarMap(cfg); + simplifyVarMap(cfg, _settings); simplifyCondition(cfg, def, true); if (cfg.find(def) != cfg.end()) diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 53a3f73cb..639d7cae7 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -297,7 +297,9 @@ private: TEST_CASE(if_sizeof); TEST_CASE(double_include); // #5717 - TEST_CASE(invalid_ifs)// #5909 + TEST_CASE(invalid_ifs); // #5909 + + TEST_CASE(garbage); } @@ -4067,7 +4069,19 @@ private: Settings settings; Preprocessor preprocessor(&settings, this); preprocessor.preprocess(istr, actual, "file.c"); + } + void garbage() { + const char filedata[] = "V\n" + "#define X b #endif #line 0 \"x\" ;\n" + "#if ! defined ( Y ) #endif"; + + // Preprocess => don't crash.. + std::istringstream istr(filedata); + std::map actual; + Settings settings; + Preprocessor preprocessor(&settings, this); + preprocessor.preprocess(istr, actual, "file.c"); } };