Skip to content

Commit 1852944

Browse files
authored
Adjust the severity of the uninitvar (#4234)
1 parent f8b7964 commit 1852944

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,8 @@ void CheckUninitVar::uninitvarError(const Token *tok, const std::string &varname
15181518

15191519
void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v)
15201520
{
1521+
if (!mSettings->isEnabled(&v))
1522+
return;
15211523
if (diag(tok))
15221524
return;
15231525
const Token* ltok = tok;
@@ -1526,13 +1528,15 @@ void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v)
15261528
const std::string& varname = ltok ? ltok->expressionString() : "x";
15271529
ErrorPath errorPath = v.errorPath;
15281530
errorPath.emplace_back(tok, "");
1531+
auto severity = v.isKnown() ? Severity::error : Severity::warning;
1532+
auto certainty = v.isInconclusive() ? Certainty::inconclusive : Certainty::normal;
15291533
if (v.subexpressions.empty()) {
15301534
reportError(errorPath,
1531-
Severity::error,
1535+
severity,
15321536
"uninitvar",
15331537
"$symbol:" + varname + "\nUninitialized variable: $symbol",
15341538
CWE_USE_OF_UNINITIALIZED_VARIABLE,
1535-
Certainty::normal);
1539+
certainty);
15361540
return;
15371541
}
15381542
std::string vars = v.subexpressions.size() == 1 ? "variable: " : "variables: ";
@@ -1542,11 +1546,11 @@ void CheckUninitVar::uninitvarError(const Token* tok, const ValueFlow::Value& v)
15421546
prefix = ", ";
15431547
}
15441548
reportError(errorPath,
1545-
Severity::error,
1549+
severity,
15461550
"uninitvar",
15471551
"$symbol:" + varname + "\nUninitialized " + vars,
15481552
CWE_USE_OF_UNINITIALIZED_VARIABLE,
1549-
Certainty::normal);
1553+
certainty);
15501554
}
15511555

15521556
void CheckUninitVar::uninitStructMemberError(const Token *tok, const std::string &membername)

test/testuninitvar.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,7 +3525,7 @@ class TestUninitVar : public TestFixture {
35253525
" if (!x) i = 0;\n"
35263526
" if (!x || i>0) {}\n" // <- error
35273527
"}");
3528-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Uninitialized variable: i\n", errout.str());
3528+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Uninitialized variable: i\n", errout.str());
35293529

35303530
valueFlowUninit("void f(int x) {\n"
35313531
" int i;\n"
@@ -3540,7 +3540,7 @@ class TestUninitVar : public TestFixture {
35403540
" else i = 0;\n"
35413541
" if (x || i>0) {}\n"
35423542
"}");
3543-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Uninitialized variable: i\n", errout.str());
3543+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: i\n", errout.str());
35443544

35453545
valueFlowUninit("void f(int x) {\n"
35463546
" int i;\n"
@@ -3573,7 +3573,7 @@ class TestUninitVar : public TestFixture {
35733573
" a = y;\n"
35743574
" return y ? 2*a : 3*a;\n"
35753575
"}");
3576-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (error) Uninitialized variable: a\n", errout.str());
3576+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (warning) Uninitialized variable: a\n", errout.str());
35773577

35783578
valueFlowUninit("void f() {\n" // Don't crash
35793579
" int a;\n"
@@ -4675,7 +4675,7 @@ class TestUninitVar : public TestFixture {
46754675
" s.x = 42;\n"
46764676
" bar(&s);\n"
46774677
"}");
4678-
ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (error) Uninitialized variable: s->flag\n",
4678+
ASSERT_EQUALS("[test.cpp:18] -> [test.cpp:12] -> [test.cpp:8]: (warning) Uninitialized variable: s->flag\n",
46794679
errout.str());
46804680

46814681
// Ticket #2207 - False negative
@@ -4820,7 +4820,7 @@ class TestUninitVar : public TestFixture {
48204820
" p = new S(io);\n"
48214821
" p->Write();\n"
48224822
"}");
4823-
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:10]: (error) Uninitialized variable: p.rIo\n", errout.str());
4823+
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:10]: (warning) Uninitialized variable: p.rIo\n", errout.str());
48244824

48254825
// Unknown types
48264826
{
@@ -5140,7 +5140,7 @@ class TestUninitVar : public TestFixture {
51405140
" }\n"
51415141
" printf(\"\", value);\n"
51425142
"}\n");
5143-
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:9]: (error) Uninitialized variable: value\n", errout.str());
5143+
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:9]: (warning) Uninitialized variable: value\n", errout.str());
51445144

51455145
valueFlowUninit("void f(int x)\n"
51465146
"{\n"
@@ -5242,7 +5242,7 @@ class TestUninitVar : public TestFixture {
52425242
" else\n"
52435243
" return -1;\n"
52445244
"}\n");
5245-
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (error) Uninitialized variable: a\n", errout.str());
5245+
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:6]: (warning) Uninitialized variable: a\n", errout.str());
52465246

52475247
// #9772
52485248
valueFlowUninit("int func(void) {\n"
@@ -5366,7 +5366,7 @@ class TestUninitVar : public TestFixture {
53665366
" increment(n);\n"
53675367
" return n;\n"
53685368
"}\n");
5369-
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Uninitialized variable: i\n", errout.str());
5369+
ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (warning) Uninitialized variable: i\n", errout.str());
53705370
}
53715371

53725372
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value
@@ -5418,7 +5418,7 @@ class TestUninitVar : public TestFixture {
54185418
" someType_t gVar;\n"
54195419
" bar(&gVar);\n"
54205420
"}");
5421-
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5]: (error) Uninitialized variable: p->flags\n", errout.str());
5421+
ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:5]: (warning) Uninitialized variable: p->flags\n", errout.str());
54225422

54235423
valueFlowUninit("typedef struct\n"
54245424
"{\n"
@@ -5619,7 +5619,7 @@ class TestUninitVar : public TestFixture {
56195619
" bool copied_all = true;\n"
56205620
" g(&copied_all, 5, 6, &bytesCopied);\n"
56215621
"}");
5622-
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (error) Uninitialized variable: *buflen\n", errout.str());
5622+
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (warning) Uninitialized variable: *buflen\n", errout.str());
56235623

56245624
// # 9953
56255625
valueFlowUninit("uint32_t f(uint8_t *mem) {\n"
@@ -5657,7 +5657,7 @@ class TestUninitVar : public TestFixture {
56575657
" ab.a = 0;\n"
56585658
" do_something(ab);\n"
56595659
"}");
5660-
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (error) Uninitialized variable: ab.b\n", errout.str());
5660+
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (warning) Uninitialized variable: ab.b\n", errout.str());
56615661

56625662
valueFlowUninit("struct AB { int a; int b; };\n"
56635663
"void f(void) {\n"
@@ -6187,7 +6187,7 @@ class TestUninitVar : public TestFixture {
61876187
"}\n");
61886188
TODO_ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->b\n"
61896189
"[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->c\n",
6190-
"[test.cpp:8] -> [test.cpp:3]: (error) Uninitialized variable: abc->b\n",
6190+
"[test.cpp:8] -> [test.cpp:3]: (warning) Uninitialized variable: abc->b\n",
61916191
errout.str());
61926192

61936193
valueFlowUninit("struct S { int* p; };\n" // #10463

0 commit comments

Comments
 (0)