From 841baa302eb821f2c18ca7e8929dd3779306c07a Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sun, 19 Jan 2025 16:47:49 +0100 Subject: [PATCH] Fix #9157 False negative: stlOutOfBounds, cast (#7233) --- lib/astutils.cpp | 2 ++ lib/tokenlist.cpp | 2 ++ test/teststl.cpp | 8 +++++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index ef3d83c89aa..3fbd781b4e5 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index a0bbe654cb4..c42ee552e1a 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -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); } diff --git a/test/teststl.cpp b/test/teststl.cpp index 6f82250e33e..0778b104e9c 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -692,13 +692,19 @@ class TestStl : public TestFixture { " if (i <= static_cast(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(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(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\n"