From 85723f860525f30ee57a487f1a5048877389abfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 23 May 2021 11:45:32 +0200 Subject: [PATCH] Uninitialized variables; Different heuristic needed for const pointer function parameter --- lib/checkuninitvar.cpp | 10 ++-------- test/testuninitvar.cpp | 7 ++++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index e752baf05..6cdb4c111 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1148,6 +1148,8 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, return nullptr; // FIXME handle address of!! + if (valueExpr->astParent()->isUnaryOp("&")) + return nullptr; if (derefValue && derefValue->astParent() && derefValue->astParent()->isUnaryOp("&")) return nullptr; @@ -1190,14 +1192,6 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, const int use = isFunctionParUsage(derefValue, false, NO_ALLOC, indirect); return (use>0) ? derefValue : nullptr; } - if (valueExpr->astParent()->isUnaryOp("&")) { - const Token *parent = valueExpr->astParent(); - if (Token::Match(parent->astParent(), "[(,]") && (parent->astParent()->str() == "," || astIsRhs(parent))) { - const int use = isFunctionParUsage(valueExpr, pointer, alloc, indirect); - return (use>0) ? valueExpr : nullptr; - } - return nullptr; - } // Assignments; // * Is this LHS in assignment diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 77cdc0214..7fd5ad450 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -3051,12 +3051,13 @@ private: "}"); ASSERT_EQUALS("", errout.str()); - checkUninitVar("void a(const char *c);\n" // const address => error + checkUninitVar("void a(const char *c);\n" // const address => data is not changed "void b() {\n" " char c;\n" - " a(&c);\n" + " a(&c);\n" // <- no warning + " c++;\n" // <- uninitialized variable "}"); - ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: c\n", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: c\n", "", errout.str()); // pointer variable checkUninitVar("void a(char c);\n" // value => error