From 08c0c32105f2cd05096390e362ff84797b8e88f4 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 17 Nov 2023 19:38:31 -0600 Subject: [PATCH] Fix stack overflow --- lib/astutils.cpp | 9 +++++++++ lib/astutils.h | 1 + lib/programmemory.cpp | 2 +- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 439a9cfe818..90a38283cdb 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -149,6 +149,15 @@ bool astHasVar(const Token * tok, nonneg int varid) return astHasVar(tok->astOperand1(), varid) || astHasVar(tok->astOperand2(), varid); } +bool astHasExpr(const Token * tok, nonneg int exprid) +{ + if (!tok) + return false; + if (tok->exprId() == exprid) + return true; + return astHasExpr(tok->astOperand1(), exprid) || astHasExpr(tok->astOperand2(), exprid); +} + static bool astIsCharWithSign(const Token *tok, ValueType::Sign sign) { if (!tok) diff --git a/lib/astutils.h b/lib/astutils.h index 60d4c4cf500..c1e94c1bda0 100644 --- a/lib/astutils.h +++ b/lib/astutils.h @@ -138,6 +138,7 @@ nonneg int astCount(const Token* tok, const char* op, int depth = 100); bool astHasToken(const Token* root, const Token * tok); +bool astHasExpr(const Token * tok, nonneg int exprid); bool astHasVar(const Token * tok, nonneg int varid); bool astIsPrimitive(const Token* tok); diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index f1fc037bf00..07b53f3cd1b 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1313,7 +1313,7 @@ namespace { const Token* tok = p.first.tok; const ValueFlow::Value& value = p.second; - if (tok->str() == expr->str()) { + if (tok->str() == expr->str() && !astHasExpr(tok, expr->exprId())) { // TODO: Handle when it is greater if (n != astCount(tok, expr->str().c_str())) continue;