Adjust the severity of the uninitvar (#4234)

This commit is contained in:
Paul Fultz II 2022-06-28 15:52:35 -05:00 committed by GitHub
parent f8b796403b
commit 185294499c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View File

@ -1518,6 +1518,8 @@ void CheckUninitVar::uninitvarError(const Token *tok, const std::string &varname
void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v) void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v)
{ {
if (!mSettings->isEnabled(&v))
return;
if (diag(tok)) if (diag(tok))
return; return;
const Token* ltok = tok; const Token* ltok = tok;
@ -1526,13 +1528,15 @@ void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v)
const std::string& varname = ltok ? ltok->expressionString() : "x"; const std::string& varname = ltok ? ltok->expressionString() : "x";
ErrorPath errorPath = v.errorPath; ErrorPath errorPath = v.errorPath;
errorPath.emplace_back(tok, ""); errorPath.emplace_back(tok, "");
auto severity = v.isKnown() ? Severity::error : Severity::warning;
auto certainty = v.isInconclusive() ? Certainty::inconclusive : Certainty::normal;
if (v.subexpressions.empty()) { if (v.subexpressions.empty()) {
reportError(errorPath, reportError(errorPath,
Severity::error, severity,
"uninitvar", "uninitvar",
"$symbol:" + varname + "\nUninitialized variable: $symbol", "$symbol:" + varname + "\nUninitialized variable: $symbol",
CWE_USE_OF_UNINITIALIZED_VARIABLE, CWE_USE_OF_UNINITIALIZED_VARIABLE,
Certainty::normal); certainty);
return; return;
} }
std::string vars = v.subexpressions.size() == 1 ? "variable: " : "variables: "; std::string vars = v.subexpressions.size() == 1 ? "variable: " : "variables: ";
@ -1542,11 +1546,11 @@ void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v)
prefix = ", "; prefix = ", ";
} }
reportError(errorPath, reportError(errorPath,
Severity::error, severity,
"uninitvar", "uninitvar",
"$symbol:" + varname + "\nUninitialized " + vars, "$symbol:" + varname + "\nUninitialized " + vars,
CWE_USE_OF_UNINITIALIZED_VARIABLE, CWE_USE_OF_UNINITIALIZED_VARIABLE,
Certainty::normal); certainty);
} }
void CheckUninitVar::uninitStructMemberError(const Token *tok, const std::string &membername) void CheckUninitVar::uninitStructMemberError(const Token *tok, const std::string &membername)

View File

@ -3525,7 +3525,7 @@ private:
" if (!x) i = 0;\n" " if (!x) i = 0;\n"
" if (!x || i>0) {}\n" // <- error " if (!x || i>0) {}\n" // <- error
"}"); "}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Uninitialized variable: i\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Uninitialized variable: i\n", errout.str());
valueFlowUninit("void f(int x) {\n" valueFlowUninit("void f(int x) {\n"
" int i;\n" " int i;\n"
@ -3540,7 +3540,7 @@ private:
" else i = 0;\n" " else i = 0;\n"
" if (x || i>0) {}\n" " if (x || i>0) {}\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Uninitialized variable: i\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: i\n", errout.str());
valueFlowUninit("void f(int x) {\n" valueFlowUninit("void f(int x) {\n"
" int i;\n" " int i;\n"
@ -3573,7 +3573,7 @@ private:
" a = y;\n" " a = y;\n"
" return y ? 2*a : 3*a;\n" " return y ? 2*a : 3*a;\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: a\n", errout.str());
valueFlowUninit("void f() {\n" // Don't crash valueFlowUninit("void f() {\n" // Don't crash
" int a;\n" " int a;\n"
@ -4675,7 +4675,7 @@ private:
" s.x = 42;\n" " s.x = 42;\n"
" bar(&s);\n" " bar(&s);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (error) Uninitialized variable: s->flag\n", ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (warning) Uninitialized variable: s->flag\n",
errout.str()); errout.str());
// Ticket #2207 - False negative // Ticket #2207 - False negative
@ -4820,7 +4820,7 @@ private:
" p = new S(io);\n" " p = new S(io);\n"
" p->Write();\n" " p->Write();\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:10]: (error) Uninitialized variable: p.rIo\n", errout.str()); ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:10]: (warning) Uninitialized variable: p.rIo\n", errout.str());
// Unknown types // Unknown types
{ {
@ -5140,7 +5140,7 @@ private:
" }\n" " }\n"
" printf(\"\", value);\n" " printf(\"\", value);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:9]: (error) Uninitialized variable: value\n", errout.str()); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:9]: (warning) Uninitialized variable: value\n", errout.str());
valueFlowUninit("void f(int x)\n" valueFlowUninit("void f(int x)\n"
"{\n" "{\n"
@ -5242,7 +5242,7 @@ private:
" else\n" " else\n"
" return -1;\n" " return -1;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) Uninitialized variable: a\n", errout.str());
// #9772 // #9772
valueFlowUninit("int func(void) {\n" valueFlowUninit("int func(void) {\n"
@ -5366,7 +5366,7 @@ private:
" increment(n);\n" " increment(n);\n"
" return n;\n" " return n;\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Uninitialized variable: i\n", errout.str()); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: i\n", errout.str());
} }
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
@ -5418,7 +5418,7 @@ private:
" someType_t gVar;\n" " someType_t gVar;\n"
" bar(&gVar);\n" " bar(&gVar);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5]: (error) Uninitialized variable: p->flags\n", errout.str()); ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5]: (warning) Uninitialized variable: p->flags\n", errout.str());
valueFlowUninit("typedef struct\n" valueFlowUninit("typedef struct\n"
"{\n" "{\n"
@ -5619,7 +5619,7 @@ private:
" bool copied_all = true;\n" " bool copied_all = true;\n"
" g(&copied_all, 5, 6, &bytesCopied);\n" " g(&copied_all, 5, 6, &bytesCopied);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (error) Uninitialized variable: *buflen\n", errout.str()); ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (warning) Uninitialized variable: *buflen\n", errout.str());
// # 9953 // # 9953
valueFlowUninit("uint32_t f(uint8_t *mem) {\n" valueFlowUninit("uint32_t f(uint8_t *mem) {\n"
@ -5657,7 +5657,7 @@ private:
" ab.a = 0;\n" " ab.a = 0;\n"
" do_something(ab);\n" " do_something(ab);\n"
"}"); "}");
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (error) Uninitialized variable: ab.b\n", errout.str()); ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (warning) Uninitialized variable: ab.b\n", errout.str());
valueFlowUninit("struct AB { int a; int b; };\n" valueFlowUninit("struct AB { int a; int b; };\n"
"void f(void) {\n" "void f(void) {\n"
@ -6187,7 +6187,7 @@ private:
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->b\n" TODO_ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->b\n"
"[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->c\n", "[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->c\n",
"[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->b\n", "[test.cpp:8] -> [test.cpp:3]: (warning) Uninitialized variable: abc->b\n",
errout.str()); errout.str());
valueFlowUninit("struct S { int* p; };\n" // #10463 valueFlowUninit("struct S { int* p; };\n" // #10463