Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/checkstl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,8 @@ void CheckStlImpl::negativeIndexError(const Token *tok, const ValueFlow::Value &
else
errmsg << "Array index " << index.intvalue << " is out of bounds.";
const auto severity = index.errorSeverity() && index.isKnown() ? Severity::error : Severity::warning;
if (!mSettings.severity.isEnabled(severity))
return;
const auto certainty = index.isInconclusive() ? Certainty::inconclusive : Certainty::normal;
reportError(std::move(errorPath), severity, "negativeContainerIndex", errmsg.str(), CWE786, certainty);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

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.

if (ret->condition && !settings.severity.isEnabled(Severity::warning))
return nullptr;
}
return ret;
}
Expand Down
7 changes: 7 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10603,6 +10603,13 @@ class TestOther : public TestFixture {
" return f<-1>(2)\n"
"}\n");
ASSERT_EQUALS("", errout_str());

Settings settings = settingsBuilder(settings0).severity(Severity::portability).build();
check("int f(int k, int& s) {\n" // #7215
" if (k >= -128 && k <= 127)\n"
" s = (k << 8);\n"
"}\n", dinit(CheckOptions, $.settings = &settings));
ASSERT_EQUALS("[test.cpp:2:11] -> [test.cpp:3:16]: (portability) Shifting a negative value is technically undefined behaviour [shiftNegativeLHS]\n", errout_str());
}

void checkNegativeShiftErrorPath() {
Expand Down
30 changes: 24 additions & 6 deletions test/teststl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3176,13 +3176,31 @@ class TestStl : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout_str());

Settings s = settings;
s.daca = true;
{
Settings s = settings;
s.daca = true;

check("void f() {\n"
" const char a[][5] = { \"1\", \"true\", \"on\", \"yes\" };\n"
"}\n", s);
ASSERT_EQUALS("", errout_str());
check("void f() {\n"
" const char a[][5] = { \"1\", \"true\", \"on\", \"yes\" };\n"
"}\n", s);
ASSERT_EQUALS("", errout_str());
}

{
Settings s = settings;
const char code[] = "char f(const std::string& s, bool b) {\n"
" int i = -1;\n"
" if (b)\n"
" i = 0;\n"
" return s[i];\n"
"}\n";
check(code, s);
ASSERT_EQUALS("[test.cpp:5:12]: (warning) Array index -1 is out of bounds. [negativeContainerIndex]\n", errout_str());

s.severity.disable(Severity::warning);
check(code, s);
ASSERT_EQUALS("", errout_str());
}
}

void negativeIndexMultiline() {
Expand Down
Loading