diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index e6e5976a2..6aa4f0ce9 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -391,7 +391,7 @@ Token *CheckMemoryLeakClass::getcode(const Token *tok, std::list if (sz > 1 && Token::Match(tok->tokAt(2), "malloc ( %num% )") && - (std::atoi(tok->strAt(4)) % sz) != 0) + (std::atoi(tok->strAt(4)) % sz) != 0) { ErrorMessage::mismatchSize(_errorLogger, _tokenizer, tok->tokAt(4), tok->strAt(4)); } diff --git a/src/tokenize.cpp b/src/tokenize.cpp index d18dcea00..e5585d191 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -1261,7 +1261,7 @@ bool Tokenizer::simplifyConditions() Token::Match(tok->next(), "%num%") && (tok2->str() == ")" || tok2->str() == "&&" || tok2->str() == "||")) { - tok->next()->str(); + tok->next()->str((tok->next()->str() != "0") ? "true" : "false"); ret = true; } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index eee2a5b24..c5c269d86 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -84,6 +84,7 @@ private: TEST_CASE(sizeof2); TEST_CASE(sizeof3); TEST_CASE(sizeof4); + TEST_CASE(simplify_numeric_condition); } @@ -939,6 +940,33 @@ private: ostr << " " << tok->str(); ASSERT_EQUALS(std::string(" for ( int i = 0 ; i < 100 ; i + + ) { }"), ostr.str()); } + + void simplify_numeric_condition() + { + const char code[] = + "void f()\n" + "{\n" + "int x = 0;\n" + "if( !x || 0 )\n" + "{\n" + "}\n" + "}"; + + // tokenize.. + Tokenizer tokenizer; + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.setVarId(); + tokenizer.simplifyTokenList(); + + std::ostringstream ostr; + for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) + ostr << " " << tok->str(); + ASSERT_EQUALS(std::string(" void f ( ) { int x ; x = 0 ; if ( ! x ) { } }"), ostr.str()); + } + + }; REGISTER_TEST(TestTokenizer)