Skip to content

Fix #14660 Handle reverse conditions in isOppositeCond function#8408

Open
francois-berder wants to merge 3 commits intodanmar:mainfrom
francois-berder:reverse-cond
Open

Fix #14660 Handle reverse conditions in isOppositeCond function#8408
francois-berder wants to merge 3 commits intodanmar:mainfrom
francois-berder:reverse-cond

Conversation

@francois-berder
Copy link
Copy Markdown
Contributor

No description provided.

Signed-off-by: Francois Berder <fberder@outlook.fr>
lib/astutils.cpp Outdated
static bool isZeroBoundCond(const Token * const cond, bool reverse)
{
if (cond == nullptr)
if (cond == nullptr || cond->astOperand1() == nullptr || cond->astOperand2() == nullptr)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (cond == nullptr || cond->astOperand1() == nullptr || cond->astOperand2() == nullptr)
if (cond == nullptr || !cond->isBinaryOp())

lib/astutils.cpp Outdated
return !isZero;
if (cond->str() == ">")

if ((reverse && !cond->astOperand1()->hasKnownIntValue()) || (!reverse && !cond->astOperand2()->hasKnownIntValue()))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm an alternative that would lead to less "hard coding" would be:

    std::string cmp = cond->str();
    if (reverse && cmp[0] == '>')
        cmp[0] = '<';
    else if (reverse && cmp[0] == '<')
        cmp[0] = '>';
    const Token* const op1 = reverse ? cond->astOperand1() : cond->astOperand2();
    const Token* const op2 = reverse ? cond->astOperand2() : cond->astOperand1();

    ....

@firewave
Copy link
Copy Markdown
Collaborator

firewave commented Apr 9, 2026

Please file a ticket for this.

@danmar
Copy link
Copy Markdown
Owner

danmar commented Apr 9, 2026

Please file a ticket for this.

not sure if you have a trac account. let us know if you want us to file a ticket for you..

@francois-berder
Copy link
Copy Markdown
Contributor Author

Please file a ticket for this.

not sure if you have a trac account. let us know if you want us to file a ticket for you..

Indeed, I do not have a trac account and I cannot create one myself so please file a ticket for me.

@chrchr-github chrchr-github changed the title Handle reverse conditions in isOppositeCond function Fix #14660 Handle reverse conditions in isOppositeCond function Apr 9, 2026
@chrchr-github
Copy link
Copy Markdown
Collaborator

Indeed, I do not have a trac account and I cannot create one myself so please file a ticket for me.

See https://trac.cppcheck.net/ticket/14660

@francois-berder
Copy link
Copy Markdown
Contributor Author

Let me know if you want to squash the commits and include a reference to the ticket number in the commit message.

@sonarqubecloud
Copy link
Copy Markdown

@chrchr-github
Copy link
Copy Markdown
Collaborator

Let me know if you want to squash the commits and include a reference to the ticket number in the commit message.

We always squash, and the PR title becomes the commit message, so it's all good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants