From fb3870071a8812b065b208e36004484a31fdc84c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 21 Jul 2011 16:13:35 +0200 Subject: [PATCH] Fixed #2885 (crash of cppcheck while checking gcc-testsuite) --- lib/tokenize.cpp | 32 +++++++++++++++++++------------- test/testsimplifytokens.cpp | 6 ++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c92f47dc6..434054b50 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5323,7 +5323,9 @@ void Tokenizer::simplifyConditionOperator() } - std::string str("if ( " + condition + " ) { " + var + " = " + value1 + " ; } else { " + var + " = " + value2 + " ; }"); + Token *starttok = 0; + + const std::string str("if ( condition ) { var = value1 ; } else { var = value2 ; }"); std::string::size_type pos1 = 0; while (pos1 != std::string::npos) { @@ -5339,19 +5341,23 @@ void Tokenizer::simplifyConditionOperator() pos1 = pos2 + 1; } tok = tok->next(); - } - if (isPointer) - { - Token::createMutualLinks(tok->tokAt(-17), tok->tokAt(-15)); - Token::createMutualLinks(tok->tokAt(-14), tok->tokAt(-8)); - Token::createMutualLinks(tok->tokAt(-6), tok); - } - else - { - Token::createMutualLinks(tok->tokAt(-15), tok->tokAt(-13)); - Token::createMutualLinks(tok->tokAt(-12), tok->tokAt(-7)); - Token::createMutualLinks(tok->tokAt(-5), tok); + // set links. + if (tok->str() == "(" || tok->str() == "{") + starttok = tok; + else if (starttok && (tok->str() == ")" || tok->str() == "}")) + { + Token::createMutualLinks(starttok, tok); + starttok = 0; + } + else if (tok->str() == "condition") + tok->str(condition); + else if (tok->str() == "var") + tok->str(var); + else if (tok->str() == "value1") + tok->str(value1); + else if (tok->str() == "value2") + tok->str(value2); } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 22446ac3f..4789ae62c 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -2739,6 +2739,12 @@ private: "}\n"; ASSERT_EQUALS("int f ( int b , int * d ) { if ( b ) { b ++ ; } if ( b ) { * d = b ; } else { * d = 10 ; } return * d ; }", tok(code)); } + + { + // Ticket #2885 + const char code[] = "; s = x ? \" \" : \"-\" ;"; + tok(code); + } } void calculations()