From 2214ef5359c092a82da25838e0b3fe037e6962ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 23 Nov 2018 20:23:20 +0100 Subject: [PATCH] Refactor isVLAIndex --- lib/checkbufferoverrun.cpp | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 1ee4089ce..87a076c17 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1081,30 +1081,22 @@ void CheckBufferOverrun::checkScope_inner(const Token *tok, const ArrayInfo &arr // Negative size in array declarations //--------------------------------------------------------------------------- -static bool isVLAIndex(const Token *index) +static bool isVLAIndex(const Token *tok) { - std::stack tokens; - tokens.push(index); - while (!tokens.empty()) { - const Token *tok = tokens.top(); - tokens.pop(); - if (!tok) - continue; - if (tok->varId() != 0U) + if (!tok) + return false; + if (tok->varId() != 0U) + return true; + if (tok->str() == "?") { + // this is a VLA index if both expressions around the ":" is VLA index + if (tok->astOperand2() && + tok->astOperand2()->str() == ":" && + isVLAIndex(tok->astOperand2()->astOperand1()) && + isVLAIndex(tok->astOperand2()->astOperand2())) return true; - if (tok->str() == "?") { - // this is a VLA index if both expressions around the ":" is VLA index - if (tok->astOperand2() && - tok->astOperand2()->str() == ":" && - isVLAIndex(tok->astOperand2()->astOperand1()) && - isVLAIndex(tok->astOperand2()->astOperand2())) - return true; - continue; - } - tokens.push(tok->astOperand1()); - tokens.push(tok->astOperand2()); + return false; } - return false; + return isVLAIndex(tok->astOperand1()) || isVLAIndex(tok->astOperand2()); } void CheckBufferOverrun::negativeArraySize()