Improve the container out of bounds messages. They are still not perfect.

This commit is contained in:
Daniel Marjamäki 2018-11-28 07:03:56 +01:00
parent 8e6013c8af
commit 0f2f807798
2 changed files with 20 additions and 11 deletions

View File

@ -115,14 +115,11 @@ void CheckStl::outOfBoundsError(const Token *tok, const ValueFlow::Value *contai
else
errmsg = "Accessing an item in container '$symbol' that is empty.";
} else if (index) {
if (containerSize->condition || index->condition)
errmsg = "Possible access out of bounds";
else
errmsg = "Access out of bounds";
errmsg += " of container '$symbol'; size=" +
MathLib::toString(containerSize->intvalue) + ", index=" +
MathLib::toString(index->intvalue);
errmsg = "Accessing $symbol[" + MathLib::toString(index->intvalue) + "] is out of bounds when $symbol size is " + MathLib::toString(containerSize->intvalue) + ".";
if (containerSize->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(containerSize->condition) + " or $symbol size can be " + MathLib::toString(containerSize->intvalue) + ". " + errmsg;
else if (index->condition)
errmsg = ValueFlow::eitherTheConditionIsRedundant(index->condition) + " or $symbol item " + MathLib::toString(index->intvalue) + " can be accessed. " + errmsg;
} else {
// should not happen
return;

View File

@ -197,6 +197,18 @@ private:
void outOfBounds() {
setMultiline();
checkNormal("void f() {\n"
" std::string s;\n"
" s[10] = 1;\n"
"}");
ASSERT_EQUALS("test.cpp:3:error:Accessing an item in container 's' that is empty.\n", errout.str());
checkNormal("void f() {\n"
" std::string s = \"abcd\";\n"
" s[10] = 1;\n"
"}");
ASSERT_EQUALS("test.cpp:3:error:Accessing s[10] is out of bounds when s size is 4.\n", errout.str());
checkNormal("void f(std::vector<int> v) {\n"
" v.front();\n"
" if (v.empty()) {}\n"
@ -209,7 +221,7 @@ private:
" if (v.size() == 3) {}\n"
" v[16] = 0;\n"
"}\n");
ASSERT_EQUALS("test.cpp:3:warning:Possible access out of bounds of container 'v'; size=3, index=16\n"
ASSERT_EQUALS("test.cpp:3:warning:Either the condition 'v.size()==3' is redundant or v size can be 3. Accessing v[16] is out of bounds when v size is 3.\n"
"test.cpp:2:note:condition 'v.size()==3'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());
@ -219,7 +231,7 @@ private:
" v[i] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("test.cpp:4:warning:Possible access out of bounds of container 'v'; size=3, index=16\n"
ASSERT_EQUALS("test.cpp:4:warning:Either the condition 'v.size()==3' is redundant or v size can be 3. Accessing v[16] is out of bounds when v size is 3.\n"
"test.cpp:3:note:condition 'v.size()==3'\n"
"test.cpp:4:note:Access out of bounds\n", errout.str());
@ -239,7 +251,7 @@ private:
" s[2] = 0;\n"
" }\n"
"}\n");
ASSERT_EQUALS("test.cpp:3:warning:Possible access out of bounds of container 's'; size=1, index=2\n"
ASSERT_EQUALS("test.cpp:3:warning:Either the condition 's.size()==1' is redundant or s size can be 1. Accessing s[2] is out of bounds when s size is 1.\n"
"test.cpp:2:note:condition 's.size()==1'\n"
"test.cpp:3:note:Access out of bounds\n", errout.str());