Skip to content

Commit

Permalink
Fix #12941 Dynamic cast wrong logic (#6666)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Aug 7, 2024
1 parent 7ece4f6 commit bc4579b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/programmemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,8 +1572,11 @@ namespace {

return unknown();
} else if (expr->str() == "(" && expr->isCast()) {
if (Token::simpleMatch(expr->previous(), ">") && expr->linkAt(-1))
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())) {
Expand Down
18 changes: 16 additions & 2 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const One*>(&b);\n"
" if (one == nullptr)\n"
" two = dynamic_cast<const Two*>(&b);\n"
" if (two) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void knownConditionIncrementLoop() { // #9808
Expand Down

0 comments on commit bc4579b

Please sign in to comment.