Skip to content

Commit

Permalink
Fix #9157 False negative: stlOutOfBounds, cast (#7233)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Jan 19, 2025
1 parent b7d2a04 commit 841baa3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,8 @@ bool isConstFunctionCall(const Token* ftok, const Library& library)
return true;
if (!Token::Match(ftok, "%name% ("))
return false;
if (ftok->isStandardType())
return true;
if (const Function* f = ftok->function()) {
if (f->isAttributePure() || f->isAttributeConst())
return true;
Expand Down
2 changes: 2 additions & 0 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@ static void compileBinOp(Token *&tok, AST_state& state, void (*f)(Token *&tok, A
if (!state.op.empty()) {
binop->astOperand1(state.op.top());
state.op.pop();
if (binop->str() == "(" && binop->astOperand1()->isStandardType())
binop->isCast(true);
}
state.op.push(binop);
}
Expand Down
8 changes: 7 additions & 1 deletion test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,19 @@ class TestStl : public TestFixture {
" if (i <= static_cast<int>(v.size())) {\n"
" if (v[i]) {}\n"
" }\n"
" if (i <= int(v.size())) {\n"
" if (v[i]) {}\n"
" }\n"
"}\n");
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"
"test.cpp:2:note:condition 'i<=(int)v.size()'\n"
"test.cpp:3:note:Access out of bounds\n"
"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"
"test.cpp:5:note:condition 'i<=static_cast<int>(v.size())'\n"
"test.cpp:6:note:Access out of bounds\n",
"test.cpp:6:note:Access out of bounds\n"
"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"
"test.cpp:8:note:condition 'i<=int(v.size())'\n"
"test.cpp:9:note:Access out of bounds\n",
errout_str());

check("template<class Iterator>\n"
Expand Down

4 comments on commit 841baa3

@danmar
Copy link
Owner

@danmar danmar commented on 841baa3 Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chrchr-github I see problems caused by this. I would prefer that we don't set isCast for variable declarations. See:
https://trac.cppcheck.net/ticket/13579

@danmar
Copy link
Owner

@danmar danmar commented on 841baa3 Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revert this commit with #7254 please feel free to make a new attempt and consider the problem in 13579 then..

@danmar
Copy link
Owner

@danmar danmar commented on 841baa3 Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however I would like some test in TestTokenizer or TestSymbolDatabase that checks that the Token::isCast is set properly. The TestStl test does not test that explicitly and it could be possible to change the isCast in the future and maybe this test would still succeed..

however I feel it's also questionable if this is a cast int(expr) is that what the specification says? I would guess that it's a constructor call?

@chrchr-github
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functional-style casts are explicit type conversions, the same as regular c-style casts:
https://en.cppreference.com/w/cpp/language/explicit_cast

Please sign in to comment.