Skip to content

Fix #7215 shiftNegativeLHS msg only printed with --enable=all - #9040

Open
chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_7215
Open

chrchr-github wants to merge 2 commits into
cppcheck-opensource:mainfrom
chrchr-github:chr_7215

Conversation

@chrchr-github

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The PR does fix #7215. Before it, shiftNegativeLHS was dropped for conditional values when only portability was enabled, and now it is reported. But it removes the ret->condition && !warning filter from ValueFlow::findValue, and that filter was the only severity gate for several other checks that call getValueLE/getValueGE. As a result, with default settings (no --enable) cppcheck now reports negativeMemoryAllocationSize and negativeContainerIndex at warning severity even though warning is not enabled. It also reports negativeArraySize as an error for a value that only comes from a condition such as if (n < 0) {}. Neither appeared before. The filter should move into the checks that need it, as CheckBufferOverrun::negativeIndexError already does, instead of being removed for all callers. A narrower fix would be to have the shiftNegativeLHS path, which reports at portability severity, ask for conditional values on its own.

Comments

# Comment By Where Status
1 Removing the condition filter makes warning-severity messages appear when warning is not enabled AI review lib/valueflow.cpp:7613 🆕 New

@chrchr-github, please fix, resolve or reply to the open and new comments: #1.

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/. The full ./testrunner passes: 5419 tests, 0 failures, and this includes the new case in TestOther.

/work/scratch/t1.cpp:

void f1(int n) {
    if (n < 0) {}
    int *p = new int[n];
    delete[] p;
}
void f2(int n) {
    if (n < 0) {}
    char a[n];
    a[0] = 0;
}
#include <array>
int f3(int i) {
    std::array<int,10> a{};
    if (i < 0) {}
    return a[i];
}

Command: cppcheck -q --template='{line}:{severity}:{id}:{message}' t1.cpp (default settings, warning not enabled):

base:

4:error:uninitdata:Memory is allocated but not initialized: p

pr:

8:error:negativeArraySize:Declaration of array 'a' with negative size is undefined behaviour
3:warning:negativeMemoryAllocationSize:Memory allocation size is negative.
15:warning:negativeContainerIndex:Either the condition 'i<0' is redundant, otherwise there is negative array index -1.
4:error:uninitdata:Memory is allocated but not initialized: p

--enable=portability gives the same result: these warnings appear in pr but not in base, and shiftNegativeLHS now appears as intended. With --enable=warning the output of base and pr is the same.


Automated review of c510f58 by Claude. The pull request was built and tested locally.

Comment thread lib/valueflow.cpp
@@ -7612,8 +7612,6 @@ const ValueFlow::Value* ValueFlow::findValue(const std::list<ValueFlow::Value>&
if (ret) {
if (ret->isInconclusive() && !settings.certainty.isEnabled(Certainty::inconclusive))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

The ret->condition && !warning check removed here was the only severity gate for several callers of getValueLE/getValueGE/findValue. These include CheckBufferOverrun::negativeArraySize, negativeMemoryAllocationSize and CheckStl::negativeIndex (negativeContainerIndex). Without it, default settings now produce new output:

void f1(int n) { if (n < 0) {} int *p = new int[n]; delete[] p; }
void f2(int n) { if (n < 0) {} char a[n]; a[0] = 0; }
#include <array>
int f3(int i) { std::array<int,10> a{}; if (i < 0) {} return a[i]; }

cppcheck t1.cpp (no --enable) now reports warning:negativeMemoryAllocationSize, warning:negativeContainerIndex and error:negativeArraySize (an error that rests only on a condition value). Base reports none of them. Either keep the filter and let checkNegativeBitwiseShift request conditional values explicitly, or add a !value->condition || isEnabled(warning) check to each of these callers, as CheckBufferOverrun::negativeIndexError does.

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