Skip to content

Commit f7e7e9b

Browse files
pfultz2danmar
authored andcommitted
Fix issue 8736: Iterators to containers from different expressions (a.begin().x == b.begin().x) (cppcheck-opensource#1370)
1 parent a5064b0 commit f7e7e9b

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/checkstl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,9 @@ static const Token * getIteratorExpression(const Token * tok)
467467
if (iter2)
468468
return iter2;
469469
} else if (Token::Match(tok, "begin|cbegin|rbegin|crbegin|end|cend|rend|crend (")) {
470-
if (Token::Match(tok->previous(), ". %name% ( )"))
470+
if (Token::Match(tok->previous(), ". %name% ( ) !!."))
471471
return tok->previous()->astOperand1();
472-
if (Token::Match(tok, "%name% ( !!)"))
472+
if (Token::Match(tok, "%name% ( !!)") && !Token::simpleMatch(tok->linkAt(1), ") ."))
473473
return tok->next()->astOperand2();
474474
}
475475
return nullptr;

test/teststl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,12 @@ class TestStl : public TestFixture {
705705
" if(f().begin()+1 == f().end()+1) {}\n"
706706
"}\n");
707707
ASSERT_EQUALS("", errout.str());
708+
709+
check("void f() {\n"
710+
" if (a.begin().x == b.begin().x) {}\n"
711+
" if (begin(a).x == begin(b).x) {}\n"
712+
"}\n");
713+
ASSERT_EQUALS("", errout.str());
708714
}
709715

710716
void iteratorSameExpression() {

0 commit comments

Comments
 (0)