Skip to content

Commit 841baa3

Browse files
Fix #9157 False negative: stlOutOfBounds, cast (#7233)
1 parent b7d2a04 commit 841baa3

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,8 @@ bool isConstFunctionCall(const Token* ftok, const Library& library)
19911991
return true;
19921992
if (!Token::Match(ftok, "%name% ("))
19931993
return false;
1994+
if (ftok->isStandardType())
1995+
return true;
19941996
if (const Function* f = ftok->function()) {
19951997
if (f->isAttributePure() || f->isAttributeConst())
19961998
return true;

lib/tokenlist.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ static void compileBinOp(Token *&tok, AST_state& state, void (*f)(Token *&tok, A
730730
if (!state.op.empty()) {
731731
binop->astOperand1(state.op.top());
732732
state.op.pop();
733+
if (binop->str() == "(" && binop->astOperand1()->isStandardType())
734+
binop->isCast(true);
733735
}
734736
state.op.push(binop);
735737
}

test/teststl.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,19 @@ class TestStl : public TestFixture {
692692
" if (i <= static_cast<int>(v.size())) {\n"
693693
" if (v[i]) {}\n"
694694
" }\n"
695+
" if (i <= int(v.size())) {\n"
696+
" if (v[i]) {}\n"
697+
" }\n"
695698
"}\n");
696699
ASSERT_EQUALS("test.cpp:3:warning:Either the condition 'i<=(int)v.size()' is redundant or 'i' can have the value v.size(). Expression 'v[i]' causes access out of bounds.\n"
697700
"test.cpp:2:note:condition 'i<=(int)v.size()'\n"
698701
"test.cpp:3:note:Access out of bounds\n"
699702
"test.cpp:6:warning:Either the condition 'i<=static_cast<int>(v.size())' is redundant or 'i' can have the value v.size(). Expression 'v[i]' causes access out of bounds.\n"
700703
"test.cpp:5:note:condition 'i<=static_cast<int>(v.size())'\n"
701-
"test.cpp:6:note:Access out of bounds\n",
704+
"test.cpp:6:note:Access out of bounds\n"
705+
"test.cpp:9:warning:Either the condition 'i<=int(v.size())' is redundant or 'i' can have the value v.size(). Expression 'v[i]' causes access out of bounds.\n"
706+
"test.cpp:8:note:condition 'i<=int(v.size())'\n"
707+
"test.cpp:9:note:Access out of bounds\n",
702708
errout_str());
703709

704710
check("template<class Iterator>\n"

0 commit comments

Comments
 (0)