Fix #10754 FP invalidFunctionArg with resize() (#3737)

This commit is contained in:
chrchr-github 2022-01-25 12:13:49 +01:00 committed by GitHub
parent 87b7243a9f
commit ea81ce933e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 1 deletions

View File

@ -154,7 +154,7 @@ void CheckFunctions::invalidFunctionArgError(const Token *tok, const std::string
errmsg << " The value is 0 or 1 (boolean) but the valid values are '" << validstr << "'."; errmsg << " The value is 0 or 1 (boolean) but the valid values are '" << validstr << "'.";
if (invalidValue) if (invalidValue)
reportError(getErrorPath(tok, invalidValue, "Invalid argument"), reportError(getErrorPath(tok, invalidValue, "Invalid argument"),
invalidValue->errorSeverity() ? Severity::error : Severity::warning, invalidValue->errorSeverity() && invalidValue->isKnown() ? Severity::error : Severity::warning,
"invalidFunctionArg", "invalidFunctionArg",
errmsg.str(), errmsg.str(),
CWE628, CWE628,

View File

@ -464,6 +464,44 @@ private:
check("void f() { strtol(a,b,10); }"); check("void f() { strtol(a,b,10); }");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("void f(std::vector<int>& v) {\n" // #10754
" int N = -1;\n"
" for (long i = 0; i < g(); i++)\n"
" N = h(N);\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (warning) Invalid v.resize() argument nr 1. The value is -1 but the valid values are '0:'.\n", errout.str());
check("void f(std::vector<int>& v, int N) {\n"
" if (N < -1)\n"
" return;\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (warning) Either the condition 'N<-1' is redundant or v.resize() argument nr 1 can have invalid value. The value is -1 but the valid values are '0:'.\n",
errout.str());
check("void f(std::vector<int>& v, int N) {\n"
" if (N == -1) {}\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Either the condition 'N==-1' is redundant or v.resize() argument nr 1 can have invalid value. The value is -1 but the valid values are '0:'.\n",
errout.str());
check("void f(std::vector<int>& v, int N, bool b) {\n"
" if (b)\n"
" N = -1;\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Invalid v.resize() argument nr 1. The value is -1 but the valid values are '0:'.\n",
errout.str());
check("void f(std::vector<int>& v) {\n"
" int N = -1;\n"
" v.resize(N);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Invalid v.resize() argument nr 1. The value is -1 but the valid values are '0:'.\n",
errout.str());
} }
void invalidFunctionUsageStrings() { void invalidFunctionUsageStrings() {