From 2c155a7a780ceeb75cf59996c3c0866e9339b675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 16 May 2021 11:58:51 +0200 Subject: [PATCH] Uninitialized variables; use AST --- lib/checkuninitvar.cpp | 6 +++--- test/testbufferoverrun.cpp | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 119f8110d..4317030a6 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1109,14 +1109,14 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al } // is there something like: ; "*((&var ..expr.. =" => the variable is assigned - if (vartok->previous()->str() == "&" && !vartok->previous()->astOperand2()) + if (vartok->astParent()->isUnaryOp("&")) return false; // bailout to avoid fp for 'int x = 2 + x();' where 'x()' is a unseen preprocessor macro (seen in linux) if (!pointer && vartok->next() && vartok->next()->str() == "(") return false; - if (alloc != NO_ALLOC && vartok->previous()->str() == "*") { + if (alloc != NO_ALLOC && vartok->astParent()->isUnaryOp("*")) { // TestUninitVar::isVariableUsageDeref() const Token *parent = vartok->previous()->astParent(); if (parent && parent->str() == "=" && parent->astOperand1() == vartok->previous()) @@ -1126,7 +1126,7 @@ bool CheckUninitVar::isVariableUsage(const Token *vartok, bool pointer, Alloc al return true; } - if (vartok->previous()->str() != "&" || !Token::Match(vartok->tokAt(-2), "[(,=?:]")) + if (!vartok->astParent()->isUnaryOp("&") || !Token::Match(vartok->tokAt(-2), "[(,=?:]")) return alloc == NO_ALLOC; } diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 3c90fd0f5..b59ddddac 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -1756,9 +1756,9 @@ private: ASSERT_EQUALS("", errout.str()); } - void array_index_bounds() { - // #10275 - check("int a[10];\n" + void array_index_bounds() { + // #10275 + check("int a[10];\n" "void f(int i) {\n" " if (i >= 0 && i < 10) {}\n" " a[i] = 1;\n" @@ -1766,7 +1766,7 @@ private: ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'i<10' is redundant or the array 'a[10]' is accessed at index 10, which is out of bounds.\n" "[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'i>=0' is redundant or the array 'a[10]' is accessed at index -1, which is out of bounds.\n", errout.str()); - } + } void array_index_calculation() { // #1193 - false negative: array out of bounds in loop when there is calculation