Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 24 additions & 3 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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",

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a ValueFlow issue, even known loop iterations have conditional values.

errout_str());

check("void f() {\n"
Expand Down Expand Up @@ -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() {
Expand Down
Loading