Fix #11697 FP negativeContainerIndex after index is validated (#5172)

This commit is contained in:
chrchr-github 2023-06-20 10:55:14 +02:00 committed by GitHub
parent e8de2aeeea
commit 78c7e3351f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -1326,7 +1326,9 @@ void CheckStl::negativeIndexError(const Token *tok, const ValueFlow::Value &inde
<< ", otherwise there is negative array index " << index.intvalue << ".";
else
errmsg << "Array index " << index.intvalue << " is out of bounds.";
reportError(errorPath, index.errorSeverity() ? Severity::error : Severity::warning, "negativeContainerIndex", errmsg.str(), CWE786, index.isInconclusive() ? Certainty::inconclusive : Certainty::normal);
const auto severity = index.errorSeverity() && index.isKnown() ? Severity::error : Severity::warning;
const auto certainty = index.isInconclusive() ? Certainty::inconclusive : Certainty::normal;
reportError(errorPath, severity, "negativeContainerIndex", errmsg.str(), CWE786, certainty);
}
void CheckStl::erase()

View File

@ -7217,7 +7217,7 @@ struct MultiValueFlowAnalyzer : ValueFlowAnalyzer {
void addErrorPath(const Token* tok, const std::string& s) override {
for (auto&& p:values) {
p.second.errorPath.emplace_back(tok, "Assuming condition is " + s);
p.second.errorPath.emplace_back(tok, s);
}
}

View File

@ -82,6 +82,7 @@ private:
TEST_CASE(STLSize);
TEST_CASE(STLSizeNoErr);
TEST_CASE(negativeIndex);
TEST_CASE(negativeIndexMultiline);
TEST_CASE(erase1);
TEST_CASE(erase2);
TEST_CASE(erase3);
@ -2324,7 +2325,28 @@ private:
settings = oldSettings;
}
void negativeIndexMultiline() {
setMultiline();
const auto oldSettings = settings;
settings.verbose = true;
check("bool valid(int);\n" // #11697
"void f(int i, const std::vector<int>& v) {\n"
" if (!valid(i))\n"
" return;\n"
" if (v[i]) {}\n"
"}\n"
"void g(const std::vector<int>& w) {\n"
" f(-1, w);\n"
"}\n");
ASSERT_EQUALS("test.cpp:5:warning:Array index -1 is out of bounds.\n"
"test.cpp:8:note:Calling function 'f', 1st argument '-1' value is -1\n"
"test.cpp:3:note:Assuming condition is false\n"
"test.cpp:5:note:Negative array index\n",
errout.str());
settings = oldSettings;
}
void erase1() {
check("void f()\n"