Skip to content

Commit 7e6f5e6

Browse files
Fix #14448 FN uninitdata after pointer check (danmar#8163)
1 parent 8b64840 commit 7e6f5e6

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

lib/checkuninitvar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparentheses, const Va
907907
continue;
908908
uninitvarError(errorToken, errorToken->expressionString(), alloc);
909909
}
910-
return true;
910+
return !Token::Match(tok->astParent(), "!|%comp%");
911911
}
912912
// skip sizeof / offsetof
913913
if (isUnevaluated(tok))

test/testuninitvar.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,7 @@ class TestUninitVar : public TestFixture {
20422042
" return;\n"
20432043
" char c = *s;\n"
20442044
"}");
2045-
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Memory is allocated but not initialized: s\n", "", errout_str());
2045+
ASSERT_EQUALS("[test.cpp:6:15]: (error) Memory is allocated but not initialized: s [uninitdata]\n", errout_str());
20462046

20472047
// #3708 - false positive when using ptr typedef
20482048
checkUninitVar("void f() {\n"
@@ -2146,6 +2146,24 @@ class TestUninitVar : public TestFixture {
21462146
" *(p + i) = 0;\n"
21472147
"}\n");
21482148
ASSERT_EQUALS("", errout_str());
2149+
2150+
checkUninitVar("int* f() {\n" // #14448
2151+
" int* p = (int*)malloc(4);\n"
2152+
" if (!p)\n"
2153+
" return nullptr;\n"
2154+
" if (*p) {}\n"
2155+
" return p;\n"
2156+
"}\n");
2157+
ASSERT_EQUALS("[test.cpp:5:9]: (error) Memory is allocated but not initialized: *p [uninitdata]\n", errout_str());
2158+
2159+
checkUninitVar("int* f() {\n"
2160+
" int* p = (int*)malloc(4);\n"
2161+
" if (p == nullptr)\n"
2162+
" return nullptr;\n"
2163+
" if (*p) {}\n"
2164+
" return p;\n"
2165+
"}\n");
2166+
ASSERT_EQUALS("[test.cpp:5:9]: (error) Memory is allocated but not initialized: *p [uninitdata]\n", errout_str());
21492167
}
21502168

21512169
// class / struct..

0 commit comments

Comments
 (0)