Skip to content

Commit 913b7ea

Browse files
committed
Partial fix for #15048 (conditional containerOutOfBounds)
1 parent f1c0820 commit 913b7ea

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎lib/checkstl.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ void CheckStlImpl::outOfBoundsError(const Token *tok, const std::string &contain
267267
}
268268

269269
reportError(std::move(errorPath),
270-
(containerSize && !containerSize->errorSeverity()) || (indexValue && !indexValue->errorSeverity()) ? Severity::warning : Severity::error,
270+
(containerSize && (!containerSize->errorSeverity() || containerSize->conditional)) ||
271+
(indexValue && (!indexValue->errorSeverity() || indexValue->conditional)) ? Severity::warning : Severity::error,
271272
"containerOutOfBounds",
272273
"$symbol:" + containerName +"\n" + errmsg,
273274
CWE398,

‎test/teststl.cpp‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ class TestStl : public TestFixture {
371371
" return s[x];\n"
372372
"}\n");
373373
ASSERT_EQUALS(
374-
"[test.cpp:5:13]: error: Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 7 [containerOutOfBounds]\n",
374+
"[test.cpp:5:13]: warning: Out of bounds access in 's[x]', if 's' size is 6 and 'x' is 7 [containerOutOfBounds]\n",
375375
errout_str());
376376

377377
checkNormal("void f() {\n"
@@ -534,7 +534,7 @@ class TestStl : public TestFixture {
534534
" v.resize(entries);\n"
535535
" v[0] = 1;\n"
536536
"}\n");
537-
ASSERT_EQUALS("[test.cpp:5:6]: error: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n", errout_str());
537+
ASSERT_EQUALS("[test.cpp:5:6]: warning: Out of bounds access in expression 'v[0]' because 'v' is empty. [containerOutOfBounds]\n", errout_str());
538538

539539
checkNormal("void f(size_t entries) {\n"
540540
" if (entries < 2) return;\n"
@@ -760,7 +760,7 @@ class TestStl : public TestFixture {
760760
" v[i] = 42;\n"
761761
" return v;\n"
762762
"}\n");
763-
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",
763+
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",
764764
errout_str());
765765

766766
check("void f() {\n"
@@ -1140,6 +1140,27 @@ class TestStl : public TestFixture {
11401140
"[test.cpp:3:11]: note: Assuming that condition 'i>5' is not redundant\n"
11411141
"[test.cpp:5:13]: note: Access out of bounds\n",
11421142
errout_str());
1143+
1144+
check("char f(std::string& s, bool b) {\n"
1145+
" if (b)\n"
1146+
" s.clear();\n"
1147+
" return s[0];\n"
1148+
"}\n"
1149+
"char g(bool b) {\n"
1150+
" std::string s = \"abc\";\n"
1151+
" int i = 0;\n"
1152+
" if (b)\n"
1153+
" i = 5;\n"
1154+
" return s[i];\n"
1155+
"}\n", s);
1156+
ASSERT_EQUALS("[test.cpp:4:13]: warning: Out of bounds access in expression 's[0]' because 's' is empty. [containerOutOfBounds]\n"
1157+
"[test.cpp:2:9]: note: Assuming condition is true\n"
1158+
"[test.cpp:4:13]: note: Access out of bounds\n"
1159+
"[test.cpp:11:13]: warning: Out of bounds access in 's[i]', if 's' size is 3 and 'i' is 5 [containerOutOfBounds]\n"
1160+
"[test.cpp:10:13]: note: Assignment 'i=5', assigned value is 5\n"
1161+
"[test.cpp:9:9]: note: Assuming condition is true\n"
1162+
"[test.cpp:11:13]: note: Access out of bounds\n",
1163+
errout_str());
11431164
}
11441165

11451166
void iterator1() {

0 commit comments

Comments
 (0)