From 586c29c772626eaffb21e55a2f4fcb939649b168 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 30 Sep 2022 14:43:21 +0200 Subject: [PATCH] Partial fix for #9157 False negative: stlOutOfBounds, cast (#4527) * Fix leakNoVarFunctionCall FP * Partial fix for #9157 False negative: stlOutOfBounds, cast --- lib/checkstl.cpp | 2 +- test/teststl.cpp | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index e19a1846b7c..9cd10645edb 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -177,7 +177,7 @@ void CheckStl::outOfBounds() return false; const Token* sizeTok = v.tokvalue; if (sizeTok && sizeTok->isCast()) - sizeTok = sizeTok->astOperand1(); + sizeTok = sizeTok->astOperand2() ? sizeTok->astOperand2() : sizeTok->astOperand1(); const Token* containerTok = getContainerFromSize(container, sizeTok); if (!containerTok) return false; diff --git a/test/teststl.cpp b/test/teststl.cpp index 5a31930306f..329e7d2eeb6 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -690,10 +690,16 @@ class TestStl : public TestFixture { " if (i <= (int)v.size()) {\n" " if (v[i]) {}\n" " }\n" + " if (i <= static_cast(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]' cause 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: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]' cause 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", errout.str()); check("template\n"