From 6eeee743d2a0da737db15eb1a94dfd42ad0edea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 14 Mar 2019 06:41:11 +0100 Subject: [PATCH] Auto variables: Minor cleanup --- lib/checkautovariables.cpp | 10 ++++++---- test/testautovariables.cpp | 6 ++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index cf8c60a3c..92be9ae02 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -173,13 +173,15 @@ static bool isAddressOfLocalVariableRecursive(const Token *expr) return false; if (Token::Match(expr, "+|-")) return isAddressOfLocalVariableRecursive(expr->astOperand1()) || isAddressOfLocalVariableRecursive(expr->astOperand2()); - if (expr->str() == "(" && !expr->astOperand2()) - return isAddressOfLocalVariableRecursive(expr->astOperand1()); - if (expr->str() == "&" && !expr->astOperand2()) { + if (expr->isCast()) + return isAddressOfLocalVariableRecursive(expr->astOperand2() ? expr->astOperand2() : expr->astOperand1()); + if (expr->isUnaryOp("&")) { const Token *op = expr->astOperand1(); bool deref = false; while (Token::Match(op, ".|[")) { - if (op->str() == "[" || op->originalName() == "->") + if (op->originalName() == "->") + return false; + if (op->str() == "[") deref = true; op = op->astOperand1(); } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 07933bc7d..b4a8931b1 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -388,6 +388,12 @@ private: "}"); ASSERT_EQUALS("[test.cpp:6]: (error) Address of local auto-variable assigned to a function parameter.\n", errout.str()); + check("void f(char **out) {\n" + " struct S *p = glob;\n" + " *out = &p->data;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // #4998 check("void f(s8**out) {\n" " s8 *p;\n" // <- p is pointer => no error