From a6257c5edc260076044ed4ec6ff9a276aed29076 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Tue, 6 Aug 2024 22:18:58 +0200 Subject: [PATCH 1/2] Fix #12941 Dynamic cast wrong logic --- lib/programmemory.cpp | 2 +- test/testcondition.cpp | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 458183e6a81..2c266c49999 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1572,7 +1572,7 @@ namespace { return unknown(); } else if (expr->str() == "(" && expr->isCast()) { - if (Token::simpleMatch(expr->previous(), ">") && expr->linkAt(-1)) + if (expr->astOperand2() && expr->astOperand1()->str() != "dynamic_cast") return execute(expr->astOperand2()); return execute(expr->astOperand1()); } diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 2cedb63758e..45ec8d5fb24 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -6084,11 +6084,25 @@ class TestCondition : public TestFixture { errout_str()); } - void knownConditionCast() { // #9976 - check("void f(int i) {\n" + void knownConditionCast() { + check("void f(int i) {\n" // #9976 " if (i < 0 || (unsigned)i > 5) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("struct B {\n" // #12941 + " virtual void f();\n" + "};\n" + "struct One : public B {};\n" + "struct Two : public B {};\n" + "void g(const B& b) {\n" + " const Two* two = nullptr;\n" + " const One* one = dynamic_cast(&b);\n" + " if (one == nullptr)\n" + " two = dynamic_cast(&b);\n" + " if (two) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); } void knownConditionIncrementLoop() { // #9808 From 9d4ccb3652378f026b3e1ca55ce493efda9be553 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Tue, 6 Aug 2024 22:27:51 +0200 Subject: [PATCH 2/2] Fix --- lib/programmemory.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/programmemory.cpp b/lib/programmemory.cpp index 2c266c49999..1e7ba3323d6 100644 --- a/lib/programmemory.cpp +++ b/lib/programmemory.cpp @@ -1572,8 +1572,11 @@ namespace { return unknown(); } else if (expr->str() == "(" && expr->isCast()) { - if (expr->astOperand2() && expr->astOperand1()->str() != "dynamic_cast") - return execute(expr->astOperand2()); + if (expr->astOperand2()) { + if (expr->astOperand1()->str() != "dynamic_cast") + return execute(expr->astOperand2()); + return unknown(); + } return execute(expr->astOperand1()); } if (expr->exprId() > 0 && pm->hasValue(expr->exprId())) {