Skip to content

Commit 1b21767

Browse files
committed
Fixed #5666 (False positive when modifiying std::string by pointer)
1 parent 7e7aa21 commit 1b21767

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6249,7 +6249,7 @@ bool Tokenizer::simplifyKnownVariables()
62496249
(Token::Match(tok2, "%name% = %bool%|%char%|%num%|%str%|%name% ;") ||
62506250
Token::Match(tok2, "%name% [ %num%| ] = %str% ;") ||
62516251
Token::Match(tok2, "%name% = & %name% ;") ||
6252-
Token::Match(tok2, "%name% = & %name% [ 0 ] ;"))) {
6252+
(Token::Match(tok2, "%name% = & %name% [ 0 ] ;") && arrays.find(tok2->tokAt(3)->varId()) != arrays.end()))) {
62536253
const unsigned int varid = tok2->varId();
62546254
if (varid == 0)
62556255
continue;

test/testtokenize.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class TestTokenizer : public TestFixture {
179179
TEST_CASE(simplifyKnownVariables59); // skip for header
180180
TEST_CASE(simplifyKnownVariables60); // #6829
181181
TEST_CASE(simplifyKnownVariables61); // #7805
182+
TEST_CASE(simplifyKnownVariables62); // #5666 - p=&str[0]
182183
TEST_CASE(simplifyKnownVariablesBailOutAssign1);
183184
TEST_CASE(simplifyKnownVariablesBailOutAssign2);
184185
TEST_CASE(simplifyKnownVariablesBailOutAssign3); // #4395 - nested assignments
@@ -2663,6 +2664,17 @@ class TestTokenizer : public TestFixture {
26632664
ASSERT_EQUALS("", errout.str());
26642665
}
26652666

2667+
void simplifyKnownVariables62() { // #5666
2668+
ASSERT_EQUALS("void foo ( std :: string str ) {\n"
2669+
"char * p ; p = & str [ 0 ] ;\n"
2670+
"* p = 0 ;\n"
2671+
"}",
2672+
tokenizeAndStringify("void foo(std::string str) {\n"
2673+
" char *p = &str[0];\n"
2674+
" *p = 0;\n"
2675+
"}", /*simplify=*/true));
2676+
}
2677+
26662678
void simplifyKnownVariablesBailOutAssign1() {
26672679
const char code[] = "int foo() {\n"
26682680
" int i; i = 0;\n"

0 commit comments

Comments
 (0)