diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 998d45bc3..41021d62e 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1165,6 +1165,8 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, return nullptr; if (alloc == CTOR_CALL && derefValue && Token::simpleMatch(derefValue->astParent(), "(") && astIsLhs(derefValue)) return nullptr; + if (Token::simpleMatch(valueExpr->astParent(), "return")) + return nullptr; } // Passing variable to function.. diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index baceede00..ecc5c0e87 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -4127,7 +4127,7 @@ private: "void f() {\n" " struct t_udf_file *newf;\n" " newf = malloc(sizeof(*newf));\n" - " if (!newf) return 0;\n" + " if (!newf) {};\n" "}"); ASSERT_EQUALS("", errout.str()); @@ -4149,6 +4149,12 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("int* f() {\n" + " int *p = (int*)malloc(40);\n" + " return p;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // function parameter (treat it as initialized until malloc is used) checkUninitVar("int f(int *p) {\n" " if (*p == 1) {}\n" // no error