Skip to content

Commit abe810d

Browse files
committed
Uninitialized variables; Fix false positive in switch inside loop
1 parent c70b879 commit abe810d

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,12 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va
859859
}
860860

861861
if (tok->str() == "{") {
862+
// switch => bailout
863+
if (tok->scope() && tok->scope()->type == Scope::ScopeType::eSwitch) {
864+
bailout = true;
865+
return nullptr;
866+
}
867+
862868
const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout);
863869
tok = tok->link();
864870
if (Token::simpleMatch(tok, "} else {")) {

test/testuninitvar.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,22 @@ class TestUninitVar : public TestFixture {
13301330
" }\n"
13311331
"}\n");
13321332
ASSERT_EQUALS("", errout.str());
1333+
1334+
// switch in loop
1335+
checkUninitVar("int foo(int *p) {\n"
1336+
" int x;\n"
1337+
" while (true) {\n"
1338+
" switch (*p) {\n"
1339+
" case 1:\n"
1340+
" return x;\n"
1341+
" case 2:\n"
1342+
" x = 123;\n"
1343+
" break;\n"
1344+
" };\n"
1345+
" ++p\n"
1346+
" }\n"
1347+
"}");
1348+
ASSERT_EQUALS("", errout.str());
13331349
}
13341350

13351351
// switch..

0 commit comments

Comments
 (0)