Skip to content

Commit c8ae1e4

Browse files
committed
Fixed cppcheck-opensource#5658 (false positive: (error) Uninitialized variable: s)
1 parent baf8d09 commit c8ae1e4

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/checkuninitvar.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,10 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
16071607
assign = false;
16081608
break;
16091609
} else if (tok2->str() == "(") {
1610-
++indentlevel;
1610+
if (Token::Match(tok2->astOperand1(), "sizeof"))
1611+
tok2 = tok2->link();
1612+
else
1613+
++indentlevel;
16111614
} else if (tok2->str() == ")") {
16121615
if (indentlevel <= 1U)
16131616
break;

test/testuninitvar.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,6 +3265,15 @@ class TestUninitVar : public TestFixture {
32653265
" }\n"
32663266
"}");
32673267
ASSERT_EQUALS("", errout.str());
3268+
3269+
checkUninitVar2("void f(void) {\n" // #5658
3270+
" struct Foo *foo;\n"
3271+
" while (true) {\n"
3272+
" foo = malloc(sizeof(*foo));\n"
3273+
" foo->x = 0;\n"
3274+
" }\n"
3275+
"}");
3276+
ASSERT_EQUALS("", errout.str());
32683277
}
32693278

32703279
void uninitvar2_4494() {

0 commit comments

Comments
 (0)