From 6178459c15da04b6e4719c24267036d8c6601e8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 26 Dec 2010 20:34:07 +0100 Subject: [PATCH] Fixed #2304 (Tokenizer::simplifyKnownVariables: known strcpy parameter) --- lib/tokenize.cpp | 25 +++++++++++++++++++++++++ test/testtokenize.cpp | 12 ++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0ea72af9d..bb8bb7f67 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6262,6 +6262,31 @@ bool Tokenizer::simplifyKnownVariables() } } + // Variable is used as 2nd parameter in function call.. + if (Token::Match(tok3, ("%var% ( %any% , " + structname + " %varid% ,|)").c_str(), varid)) + { + const char * const functionName[] = + { + "memcmp","memcpy","memmove", + "strcmp","strcpy","strncmp","strncpy" + }; + for (unsigned int i = 0; i < (sizeof(functionName) / sizeof(*functionName)); ++i) + { + if (tok3->str() == functionName[i]) + { + Token *par = tok3->tokAt(4); + if (!structname.empty()) + { + par->deleteThis(); + par->deleteThis(); + } + par->str(value); + par->varId(valueVarId); + break; + } + } + } + // array usage if (Token::Match(tok3, ("[(,] " + structname + " %varid% [+-*/[]").c_str(), varid)) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 2ae0e01a0..b349dda0a 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -123,6 +123,7 @@ private: TEST_CASE(simplifyKnownVariables33); // struct variable TEST_CASE(simplifyKnownVariables34); TEST_CASE(simplifyKnownVariables35); // ticket #2353 - False positive: Division by zero 'if (x == 0) return 0; return 10 / x;' + TEST_CASE(simplifyKnownVariables36); // ticket #2304 - known value for strcpy parameter TEST_CASE(simplifyKnownVariablesBailOutAssign); TEST_CASE(simplifyKnownVariablesBailOutFor1); TEST_CASE(simplifyKnownVariablesBailOutFor2); @@ -1898,6 +1899,17 @@ private: ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); } + void simplifyKnownVariables36() + { + // Ticket #2304 + const char code[] = "void f() {" + " const char *q = \"hello\";" + " strcpy(p, q);" + "}"; + const char expected[] = "void f ( ) { const char * q ; q = \"hello\" ; strcpy ( p , \"hello\" ) ; }"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + void simplifyKnownVariablesBailOutAssign() { const char code[] = "int foo() {\n"