From 2908593cf68497d280224d697e9af746aa88c5be Mon Sep 17 00:00:00 2001 From: Kamil Dudka Date: Mon, 18 Feb 2019 09:35:07 +0100 Subject: [PATCH] checkautovariables: eliminate false positives on assignment of &ptr->item (#1667) Even if `ptr` is a local variable, the object `ptr->item` might be not. So taking address of `ptr->item` is definitely not unsafe in general. This commit fixes false positives triggered by commit 1.85-249-gf42648fe2 on the following code of sssd: https://github.com/SSSD/sssd/blob/d409df33/src/sbus/request/sbus_request.c#L359 --- lib/checkautovariables.cpp | 2 +- test/testautovariables.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index b0b12c247..4b435d73f 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -179,7 +179,7 @@ static bool isAddressOfLocalVariableRecursive(const Token *expr) const Token *op = expr->astOperand1(); bool deref = false; while (Token::Match(op, ".|[")) { - if (op->str() == "[") + if (op->str() == "[" || op->originalName() == "->") deref = true; op = op->astOperand1(); } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index fc5ca62da..da9543288 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -1315,6 +1315,16 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + + check("struct s { void *p; };\n" + "extern struct s* f(void);\n" + "void g(void **q)\n" + "{\n" + " struct s *r = f();\n" + " *q = &r->p;\n" + "}\n"); + + ASSERT_EQUALS("", errout.str()); } void testconstructor() { // Ticket #5478 - crash while checking a constructor