Skip to content

Commit d563bd7

Browse files
committed
Fixed danmar#6583 (False positive uninitvar - exit() ignored?!)
1 parent 105de8e commit d563bd7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/checkuninitvar.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
13251325

13261326
bool possibleInitElse(number_of_if > 0 || suppressErrors);
13271327
bool noreturnElse = false;
1328-
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, nullptr, alloc, membervar);
1328+
const bool initelse = !alwaysTrue && checkScopeForVariable(tok->next(), var, &possibleInitElse, &noreturnElse, alloc, membervar);
13291329

13301330
std::map<unsigned int, int> varValueElse;
13311331
if (!alwaysTrue && !initelse && !noreturnElse) {
@@ -1347,11 +1347,12 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var
13471347
(alwaysTrue || initelse || noreturnElse))
13481348
return true;
13491349

1350-
if ((initif || initelse || possibleInitElse) && !noreturnIf && !noreturnElse) {
1350+
if (initif || initelse || possibleInitElse)
13511351
++number_of_if;
1352+
if (!initif && !noreturnIf)
13521353
variableValue.insert(varValueIf.begin(), varValueIf.end());
1354+
if (!initelse && !noreturnElse)
13531355
variableValue.insert(varValueElse.begin(), varValueElse.end());
1354-
}
13551356
}
13561357
}
13571358
}

test/testuninitvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,15 @@ class TestUninitVar : public TestFixture {
26072607
"}");
26082608
ASSERT_EQUALS("", errout.str());
26092609

2610+
checkUninitVarB("int f(int a) {\n" // #6583
2611+
" int x;\n"
2612+
" if (a < 2) exit(1);\n"
2613+
" else if (a == 2) x = 0;\n"
2614+
" else exit(2);\n"
2615+
" return x;\n"
2616+
"}");
2617+
ASSERT_EQUALS("", errout.str());
2618+
26102619
checkUninitVarB("int f(int a) {\n" // #4560
26112620
" int x = 1, y;\n"
26122621
" if (a) x = 0;\n"

0 commit comments

Comments
 (0)