From 913b7ea5c1db9d006427429a67329448d68d192e Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 21 Sep 2026 20:44:51 +0200 Subject: [PATCH] Partial fix for #15048 (conditional containerOutOfBounds) --- lib/checkstl.cpp | 3 ++- test/teststl.cpp | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 637f5e1e8b8..3b71de86011 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -267,7 +267,8 @@ void CheckStlImpl::outOfBoundsError(const Token *tok, const std::string &contain } reportError(std::move(errorPath), - (containerSize && !containerSize->errorSeverity()) || (indexValue && !indexValue->errorSeverity()) ? Severity::warning : Severity::error, + (containerSize && (!containerSize->errorSeverity() || containerSize->conditional)) || + (indexValue && (!indexValue->errorSeverity() || indexValue->conditional)) ? Severity::warning : Severity::error, "containerOutOfBounds", "$symbol:" + containerName +"\n" + errmsg, CWE398, diff --git a/test/teststl.cpp b/test/teststl.cpp index 197cc3fafa7..7e5b2e1969e 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -371,7 +371,7 @@ class TestStl : public TestFixture { " return s[x];\n" "}\n"); ASSERT_EQUALS( - "[test.cpp:5:13]: error: Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 7 [containerOutOfBounds]\n", + "[test.cpp:5:13]: warning: Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 7 [containerOutOfBounds]\n", errout_str()); checkNormal("void f() {\n" @@ -534,7 +534,7 @@ class TestStl : public TestFixture { " v.resize(entries);\n" " v[0] = 1;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5:6]: error: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n", errout_str()); + ASSERT_EQUALS("[test.cpp:5:6]: warning: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n", errout_str()); checkNormal("void f(size_t entries) {\n" " if (entries < 2) return;\n" @@ -760,7 +760,7 @@ class TestStl : public TestFixture { " v[i] = 42;\n" " return v;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4:10]: error: Out of bounds access in 'v[i]', if 'v' size is 10 and 'i' is 10 [containerOutOfBounds]\n", + ASSERT_EQUALS("[test.cpp:4:10]: warning: Out of bounds access in 'v[i]', if 'v' size is 10 and 'i' is 10 [containerOutOfBounds]\n", errout_str()); check("void f() {\n" @@ -1140,6 +1140,27 @@ class TestStl : public TestFixture { "[test.cpp:3:11]: note: Assuming that condition 'i>5' is not redundant\n" "[test.cpp:5:13]: note: Access out of bounds\n", errout_str()); + + check("char f(std::string& s, bool b) {\n" + " if (b)\n" + " s.clear();\n" + " return s[0];\n" + "}\n" + "char g(bool b) {\n" + " std::string s = \"abc\";\n" + " int i = 0;\n" + " if (b)\n" + " i = 5;\n" + " return s[i];\n" + "}\n", s); + ASSERT_EQUALS("[test.cpp:4:13]: warning: Out of bounds access in expression 's[0]' because 's' is empty. [containerOutOfBounds]\n" + "[test.cpp:2:9]: note: Assuming condition is true\n" + "[test.cpp:4:13]: note: Access out of bounds\n" + "[test.cpp:11:13]: warning: Out of bounds access in 's[i]', if 's' size is 3 and 'i' is 5 [containerOutOfBounds]\n" + "[test.cpp:10:13]: note: Assignment 'i=5', assigned value is 5\n" + "[test.cpp:9:9]: note: Assuming condition is true\n" + "[test.cpp:11:13]: note: Access out of bounds\n", + errout_str()); } void iterator1() {