Skip to content

Commit 6eb9ccb

Browse files
Partial fix for #15048 (conditional containerOutOfBounds) (#8881)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent f6d5c69 commit 6eb9ccb

2 files changed

Lines changed: 29 additions & 5 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: 27 additions & 4 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,8 +760,10 @@ 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",
764-
errout_str());
763+
TODO_ASSERT_EQUALS(
764+
"[test.cpp:4:10]: error: Out of bounds access in 'v[i]', if 'v' size is 10 and 'i' is 10 [containerOutOfBounds]\n",
765+
"[test.cpp:4:10]: warning: Out of bounds access in 'v[i]', if 'v' size is 10 and 'i' is 10 [containerOutOfBounds]\n",
766+
errout_str());
765767

766768
check("void f() {\n"
767769
" int s = 2;\n"
@@ -1154,6 +1156,27 @@ class TestStl : public TestFixture {
11541156
"[test.cpp:3:29]: note: Assuming that condition 'it!=v.end()' is not redundant\n"
11551157
"[test.cpp:7:7]: note: Either the condition is redundant or function 'erase()' is called on the iterator 'it' which is out of bounds.\n",
11561158
errout_str());
1159+
1160+
check("char f(std::string& s, bool b) {\n"
1161+
" if (b)\n"
1162+
" s.clear();\n"
1163+
" return s[0];\n"
1164+
"}\n"
1165+
"char g(bool b) {\n"
1166+
" std::string s = \"abc\";\n"
1167+
" int i = 0;\n"
1168+
" if (b)\n"
1169+
" i = 5;\n"
1170+
" return s[i];\n"
1171+
"}\n", s);
1172+
ASSERT_EQUALS("[test.cpp:4:13]: warning: Out of bounds access in expression 's[0]' because 's' is empty. [containerOutOfBounds]\n"
1173+
"[test.cpp:2:9]: note: Assuming condition is true\n"
1174+
"[test.cpp:4:13]: note: Access out of bounds\n"
1175+
"[test.cpp:11:13]: warning: Out of bounds access in 's[i]', if 's' size is 3 and 'i' is 5 [containerOutOfBounds]\n"
1176+
"[test.cpp:10:13]: note: Assignment 'i=5', assigned value is 5\n"
1177+
"[test.cpp:9:9]: note: Assuming condition is true\n"
1178+
"[test.cpp:11:13]: note: Access out of bounds\n",
1179+
errout_str());
11571180
}
11581181

11591182
void iterator1() {

0 commit comments

Comments
 (0)