Fixed #10275 (ValueFlow: condition 'x>=0 && x < 10')

This commit is contained in:
Daniel Marjamäki 2021-05-16 10:34:22 +02:00
parent 2c10e0747a
commit e73057eb44
2 changed files with 17 additions and 0 deletions

View File

@ -2063,6 +2063,10 @@ static void mergeAdjacent(std::list<ValueFlow::Value>& values)
if (x->valueKind != y->valueKind)
continue;
if (x->bound != y->bound) {
if (y->bound != ValueFlow::Value::Bound::Point && isAdjacent(*x, *y)) {
adjValues.clear();
break;
}
// No adjacent points for floating points
if (x->valueType == ValueFlow::Value::ValueType::FLOAT)
continue;

View File

@ -135,6 +135,7 @@ private:
TEST_CASE(array_index_multidim);
TEST_CASE(array_index_switch_in_for);
TEST_CASE(array_index_for_in_for); // FP: #2634
TEST_CASE(array_index_bounds);
TEST_CASE(array_index_calculation);
TEST_CASE(array_index_negative1);
TEST_CASE(array_index_negative2); // ticket #3063
@ -1755,6 +1756,18 @@ private:
ASSERT_EQUALS("", errout.str());
}
void array_index_bounds() {
// #10275
check("int a[10];\n"
"void f(int i) {\n"
" if (i >= 0 && i < 10) {}\n"
" a[i] = 1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'i<10' is redundant or the array 'a[10]' is accessed at index 10, which is out of bounds.\n"
"[test.cpp:3] -> [test.cpp:4]: (warning) Either the condition 'i>=0' is redundant or the array 'a[10]' is accessed at index -1, which is out of bounds.\n",
errout.str());
}
void array_index_calculation() {
// #1193 - false negative: array out of bounds in loop when there is calculation
check("void f()\n"