Skip to content

Partial fix for #15048: shiftNegative for conditional value - #8884

Open
chrchr-github wants to merge 4 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15048_II
Open

chrchr-github wants to merge 4 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_15048_II

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

Comment thread lib/checkother.cpp
// a portability issue
const char* id = isLHS ? "shiftNegativeLHS" : "shiftNegative";
const std::string msg = isLHS ? "Shifting a negative value is technically undefined behaviour" : "Shifting by a negative value is undefined behaviour";
const Severity severity = isLHS ? Severity::portability : (v && v->errorSeverity() && !v->conditional ? Severity::error : Severity::warning);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is an AI review. Take it with a grain of salt and feel free to reject it by resolving the comment.

getErrorMessages() calls this with v == nullptr, so --errorlist now reports shiftNegative as severity="warning" instead of severity="error". I checked this with a build of the PR. Tools that use the error list (GUI, docs, severity mappings) would see the downgrade even though a known negative shift is still an error. Maybe treat a missing value as the non-conditional case:

Suggested change
const Severity severity = isLHS ? Severity::portability : (v && v->errorSeverity() && !v->conditional ? Severity::error : Severity::warning);
const Severity severity = isLHS ? Severity::portability : (!v || (v->errorSeverity() && !v->conditional) ? Severity::error : Severity::warning);

Apart from that, the new severity matches how zerodiv/zerodivcond handle the same patterns. I compared with int d = 0; if (b) return i; return i / d;.

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.

2 participants