Skip to content

Commit 3e6fbc6

Browse files
committed
Fixed uninitialized variable FP after realloc if it fails. Related with ticket: cppcheck-opensource#5240
1 parent 825174d commit 3e6fbc6

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

lib/checkuninitvar.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,8 @@ class UninitVar : public ExecutionPath {
614614
if (Token::Match(&tok, "free|kfree|fclose ( %var% )") ||
615615
Token::Match(&tok, "realloc ( %var%")) {
616616
dealloc_pointer(checks, tok.tokAt(2));
617+
if (tok.str() == "realloc")
618+
ExecutionPath::bailOutVar(checks, tok.tokAt(2)->varId());
617619
return tok.tokAt(3);
618620
}
619621

test/testuninitvar.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ class TestUninitVar : public TestFixture {
106106
"}");
107107
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str());
108108

109+
checkUninitVar("void foo() {\n" // #5240
110+
" char *p = malloc(100);\n"
111+
" char *tmp = realloc(p,1000);\n"
112+
" if (!tmp) free(p);\n"
113+
"}");
114+
ASSERT_EQUALS("", errout.str());
115+
109116
checkUninitVar("void foo() {\n"
110117
" int *p = NULL;\n"
111118
" realloc(p,10);\n"

0 commit comments

Comments
 (0)