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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an AI review take it with a grain of salt. Please feel free to reject it by clicking on Resolve button

The change only lowers the severity, so it can't add false positives, and it matches what negativeIndex does in checkbufferoverrun.cpp:464. Looks good to me.

As you noted, this #10779 case is a definite bug (the loop always reaches i == 10), and it is now downgraded only because of the ValueFlow issue with conditional values in known loop iterations. Could you add a short comment or a TODO_ASSERT_EQUALS with the error: expectation here? A ticket reference would also work. Otherwise the downgrade is easy to forget once the ValueFlow side is fixed.

errout_str());

check("void f() {\n"
Expand Down Expand Up @@ -1154,6 +1154,27 @@ class TestStl : public TestFixture {
"[test.cpp:3:29]: note: Assuming that condition 'it!=v.end()' is not redundant\n"
"[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",
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