From 4cccc710ef0f2650fd0d95ab971c36377490e2f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 24 May 2021 20:55:04 +0200 Subject: [PATCH] Uninitialized variables; Fixed FP when bitmask is used on address --- lib/checkuninitvar.cpp | 6 ++++-- test/testuninitvar.cpp | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index de2f2bff4..d2a3ce580 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1120,7 +1120,9 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, const int arrayDim = (vartok->variable() && vartok->variable()->isArray()) ? vartok->variable()->dimensions().size() : 1; int deref = 0; derefValue = valueExpr; - while (Token::Match(derefValue->astParent(), "+|-|*|[|.") || (derefValue->astParent() && derefValue->astParent()->isCast())) { + while (Token::Match(derefValue->astParent(), "+|-|*|[|.") || + (derefValue->astParent() && derefValue->astParent()->isCast()) || + (deref < arrayDim && Token::Match(derefValue->astParent(), "&") && derefValue->astParent()->isBinaryOp())) { const Token * const derefValueParent = derefValue->astParent(); if (derefValueParent->str() == "*") { if (derefValueParent->isUnaryOp("*")) @@ -1133,7 +1135,7 @@ const Token* CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, else break; } else if (Token::Match(derefValueParent, "[+-]")) { - if (derefValueParent->valueType() && derefValueParent->valueType()->pointer == 0) + if (deref >= arrayDim) break; } else if (derefValueParent->str() == ".") ++deref; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 1e8dc6042..414ba2457 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1619,6 +1619,12 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + checkUninitVar("void foo() {\n" + " char buf[1024];\n" + " char *b = (char *) (((uintptr_t) buf + 63) & ~(uintptr_t) 63);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // Passing array to function checkUninitVar("void f(int i);\n" "void foo()\n"