diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 87f200bdb..4ac614610 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -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 << "'."; if (invalidValue) reportError(getErrorPath(tok, invalidValue, "Invalid argument"), - invalidValue->errorSeverity() ? Severity::error : Severity::warning, + invalidValue->errorSeverity() && invalidValue->isKnown() ? Severity::error : Severity::warning, "invalidFunctionArg", errmsg.str(), CWE628, diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index bc94487a4..05e16b6f8 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -464,6 +464,44 @@ private: check("void f() { strtol(a,b,10); }"); ASSERT_EQUALS("", errout.str()); + + check("void f(std::vector& 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& 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& 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& 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& 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() {