diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f0a8f6318..aa4c00af8 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6636,6 +6636,12 @@ bool Tokenizer::simplifyKnownVariablesSimplify(Token **tok2, Token *tok3, unsign Token::Match(tok3, ("!|==|!=|<|<=|>|>= " + structname + " %varid% ==|!=|<|<=|>|>=|)|;").c_str(), varid) || Token::Match(tok3->previous(), "strlen|free ( %varid% )", varid)) { + if (value[0] == '\"' && tok3->strAt(-1) != "strlen") + { + // bail out if value is a string unless if it's just given + // as parameter to strlen + break; + } if (!structname.empty()) { tok3->deleteNext(); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 76ddfc0cb..72763bc01 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -2053,17 +2053,33 @@ private: void simplifyKnownVariables42() { - const char code[] = "void f() {\n" - " char str1[10], str2[10];\n" - " strcpy(str1, \"abc\");\n" - " strcpy(str2, str1);\n" - "}"; - const char expected[] = "void f ( ) {\n" - "char str1 [ 10 ] ; char str2 [ 10 ] ;\n" - "strcpy ( str1 , \"abc\" ) ;\n" - "strcpy ( str2 , \"abc\" ) ;\n" + { + const char code[] = "void f() {\n" + " char str1[10], str2[10];\n" + " strcpy(str1, \"abc\");\n" + " strcpy(str2, str1);\n" "}"; - ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + const char expected[] = "void f ( ) {\n" + "char str1 [ 10 ] ; char str2 [ 10 ] ;\n" + "strcpy ( str1 , \"abc\" ) ;\n" + "strcpy ( str2 , \"abc\" ) ;\n" + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } + + { + const char code[] = "void f() {" + " char *s = malloc(10);" + " strcpy(s, \"\");" + " free(s);" + "}"; + const char expected[] = "void f ( ) {" + " char * s ; s = malloc ( 10 ) ;" + " strcpy ( s , \"\" ) ;" + " free ( s ) ; " + "}"; + ASSERT_EQUALS(expected, tokenizeAndStringify(code, true)); + } } void simplifyKnownVariablesBailOutAssign()