From 236d0eb178d0fa2a11aa5cff1cf2eae808f94cc3 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Wed, 28 Sep 2011 21:46:09 +0300 Subject: [PATCH] Fix #2813 (False negative: Uninitialized variable not found for realloc) http://sourceforge.net/apps/trac/cppcheck/ticket/2813 Patch provided by: marekzmyslowski --- lib/checkuninitvar.cpp | 3 ++- test/testuninitvar.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 0b0a0eec4..c11c082b2 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -651,7 +651,8 @@ private: return tok.next()->link(); // deallocate pointer - if (Token::Match(&tok, "free|kfree|fclose ( %var% )")) + if (Token::Match(&tok, "free|kfree|fclose ( %var% )") || + Token::Match(&tok, "realloc ( %var%")) { dealloc_pointer(checks, tok.tokAt(2)); return tok.tokAt(3); diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index fa9e23388..f788bac59 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -85,6 +85,18 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); + checkUninitVar("void foo() {\n" + " int *p;\n" + " realloc(p,10);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: p\n", errout.str()); + + checkUninitVar("void foo() {\n" + " int *p = NULL;\n" + " realloc(p,10);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // dereferencing uninitialized pointer.. checkUninitVar("static void foo()\n" "{\n"