From 1b86615a69f389b4621c83406f20b651e8d7d8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 12 Dec 2013 11:44:07 +0100 Subject: [PATCH] Uninitialized variables: Fixed false positive for 'p=malloc; if (p) {}' --- lib/checkuninitvar.cpp | 4 ++-- test/testuninitvar.cpp | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 126a0ae5c..eced88f57 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1703,7 +1703,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all } else if (Token::Match(start->previous(), "if|while|for")) { // control-flow statement reading the variable "by value" - return true; + return !alloc; } } } @@ -1766,7 +1766,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, bool all bool unknown = false; if (pointer && CheckNullPointer::isPointerDeRef(vartok, unknown)) { - // pointer is allocated + // pointer is allocated - dereferencing it is ok. if (alloc) return false; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 67ba8cbf2..6eac6c5ec 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3272,6 +3272,12 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkUninitVar2("void f() {\n" + " char *s = malloc(100);\n" + " if (s != NULL) { }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // function parameter (treat it as initialized until malloc is used) checkUninitVar2("int f(int *p) {\n" " if (*p == 1) {}\n" // no error