From 1b217679549e5720c259b59c91e8f05eb871d789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 26 Dec 2016 17:13:35 +0100 Subject: [PATCH] Fixed #5666 (False positive when modifiying std::string by pointer) --- lib/tokenize.cpp | 2 +- test/testtokenize.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index fd34fc6be..8f7f86cb0 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6249,7 +6249,7 @@ bool Tokenizer::simplifyKnownVariables() (Token::Match(tok2, "%name% = %bool%|%char%|%num%|%str%|%name% ;") || Token::Match(tok2, "%name% [ %num%| ] = %str% ;") || Token::Match(tok2, "%name% = & %name% ;") || - Token::Match(tok2, "%name% = & %name% [ 0 ] ;"))) { + (Token::Match(tok2, "%name% = & %name% [ 0 ] ;") && arrays.find(tok2->tokAt(3)->varId()) != arrays.end()))) { const unsigned int varid = tok2->varId(); if (varid == 0) continue; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 6059fef6a..faf9662be 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -179,6 +179,7 @@ private: TEST_CASE(simplifyKnownVariables59); // skip for header TEST_CASE(simplifyKnownVariables60); // #6829 TEST_CASE(simplifyKnownVariables61); // #7805 + TEST_CASE(simplifyKnownVariables62); // #5666 - p=&str[0] TEST_CASE(simplifyKnownVariablesBailOutAssign1); TEST_CASE(simplifyKnownVariablesBailOutAssign2); TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments @@ -2663,6 +2664,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void simplifyKnownVariables62() { // #5666 + ASSERT_EQUALS("void foo ( std :: string str ) {\n" + "char * p ; p = & str [ 0 ] ;\n" + "* p = 0 ;\n" + "}", + tokenizeAndStringify("void foo(std::string str) {\n" + " char *p = &str[0];\n" + " *p = 0;\n" + "}", /*simplify=*/true)); + } + void simplifyKnownVariablesBailOutAssign1() { const char code[] = "int foo() {\n" " int i; i = 0;\n"