Skip to content

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

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

chrchr-github wants to merge 5 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.

@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 new commits only add a severity check to CheckStl::negativeIndexError. Removing the ret->condition && !warning filter from ValueFlow::findValue still leaks warnings into default runs. Three of these were already reported in C1/C2: negativeMemoryAllocationSize and negativeArraySize still appear with default settings, and only negativeContainerIndex is fixed. There is also a new case. The shiftNegative path in CheckOther::checkNegativeBitwiseShift reports at warning severity, and it now fires with default settings (no --enable) when the only negative value comes from a condition such as if (k < 0) {}. Base does not report it. That check, and the buffer-overrun ones, need a !value->condition || isEnabled(warning) gate.

Comments

# Comment By Where Status
1 Removing condition filter makes negativeMemoryAllocationSize/negativeArraySize/negativeContainerIndex appear without --enable=warning AI review general ⚠️ Open
2 findValue condition filter was the only severity gate for several getValueLE/GE callers AI review lib/valueflow.cpp:7613 ⚠️ Open
3 shiftNegative (warning) now reported with default settings for condition values AI review lib/valueflow.cpp:7614 🆕 New

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

Testing

I built cppcheck and testrunner in pr/ and cppcheck in base/. The full ./testrunner passes: 5419 tests, 0 failures, including the new cases in TestOther and TestStl.

New finding: shiftNegative with default settings

scratch/t3.c:

int f6(int k) {
    if (k < 0) {}
    return 2 >> k;
}

Command: cppcheck -q --template='{line}:{severity}:{id}:{message}' t3.c

  • base: no output
  • pr: 3:warning:shiftNegative:Shifting by a negative value is undefined behaviour

Recheck of the C1/C2 example

Run on the same file and command with default settings:

  • pr still reports 8:error:negativeArraySize and 3:warning:negativeMemoryAllocationSize.
  • negativeContainerIndex is gone, and shiftNegativeLHS appears with --enable=portability as intended.

Other callers that I checked

With --enable=warning, output of base and pr is identical. The other callers of getValueGE/getValueLE that I tried (shiftTooManyBits, integerOverflowCond) show no difference at default settings, because they already call mSettings.isEnabled(value, false).


Automated review of 928aaa9 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))
return nullptr;

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

Removing the condition filter here also affects the RHS branch of CheckOther::checkNegativeBitwiseShift (isNegative() → getValueLE(-1)). That branch reports shiftNegative at warning severity and never checks whether warning is enabled.

int f6(int k) {
    if (k < 0) {}
    return 2 >> k;
}

cppcheck -q t3.c (no --enable):

  • base: no output
  • pr: 3:warning:shiftNegative:Shifting by a negative value is undefined behaviour

The RHS path needs the same !value->condition || mSettings.severity.isEnabled(Severity::warning) gate that negativeIndexError got in checkstl.cpp.

@chrchr-github

Copy link
Copy Markdown
Collaborator Author

The severity handling of negativeMemoryAllocationSize and negativeArraySize seems dubious already, see https://trac.cppcheck.net/ticket/15048#comment:11
I'd rather not touch it in this PR.

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