Skip to content

Commit 5870344

Browse files
committed
Fix #15225 (FN arrayIndexOutOfBounds: sizeof(arr) unknown when the array dimension contains sizeof of a struct, variable or dereference)
A dimension such as 10 + sizeof(struct S) was only marked known by SymbolDatabase::setArrayDimensionsUsingValueFlow(), which runs after all ValueFlow passes. Every reader inside ValueFlow saw it as unknown, so sizeof(arr), sizeof(arr) / sizeof(arr[0]) and sizeof of a struct that contains such an array got no value. valueFlowNumber() now sets the dimension when the token walk reaches the end of the array declaration, where constant folding has already given the dimension token a known value. Declarations precede uses, so the existing sizeof code then works unchanged, including for chained declarations such as char b[sizeof(a)]. The pass after ValueFlow stays for dimensions that only become known later. With more dimensions known, sizeof of an array function parameter would get the size of the array more often. It is a pointer, so sizeof now gives the pointer size and sizeof(p) / sizeof(p[0]) is no longer evaluated as the element count. The condition moves to Variable::isDecayedArrayArgument(), shared with CheckSizeof.
1 parent e1b5791 commit 5870344

7 files changed

Lines changed: 145 additions & 3 deletions

File tree

‎lib/checksizeof.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void CheckSizeofImpl::checkSizeofForArrayParameter()
8787
}
8888

8989
const Variable *var = varTok->variable();
90-
if (var && var->isArray() && var->isArgument() && !var->isReference() && !(var->isStlType() && var->getTypeName() == "std::array"))
90+
if (var && var->isDecayedArrayArgument())
9191
sizeofForArrayParameterError(tok);
9292
}
9393
}

‎lib/symboldatabase.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,11 @@ bool Variable::isStlStringViewType() const
27182718
return getFlag(fIsStlType) && valueType() && valueType()->container && valueType()->container->stdStringLike && valueType()->container->view;
27192719
}
27202720

2721+
bool Variable::isDecayedArrayArgument() const
2722+
{
2723+
return isArray() && isArgument() && !isReference() && !(isStlType() && getTypeName() == "std::array");
2724+
}
2725+
27212726
std::string Variable::getTypeName() const
27222727
{
27232728
std::string ret;

‎lib/symboldatabase.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,12 @@ class CPPCHECKLIB Variable {
447447
return getFlag(fIsArray) && !getFlag(fIsPointer);
448448
}
449449

450+
/**
451+
* Is variable an array function parameter, which is a pointer.
452+
* @return true if array parameter that decays to a pointer, false if not
453+
*/
454+
bool isDecayedArrayArgument() const;
455+
450456
/**
451457
* Is pointer variable.
452458
* @return true if pointer, false otherwise

‎lib/valueflow.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,33 @@ void ValueFlow::combineValueProperties(const ValueFlow::Value &value1, const Val
422422
result.path = value1.path;
423423
}
424424

425+
// Set the dimensions that are known when the array declaration ends at tok, so sizeof of the array is known
426+
static void valueFlowArrayDimensions(const Token* tok)
427+
{
428+
const Token* start = tok->link();
429+
while (Token::simpleMatch(start->previous(), "]"))
430+
start = start->previous()->link();
431+
const Token* const nameTok = start->previous();
432+
const Variable* const var = nameTok ? nameTok->variable() : nullptr;
433+
if (!var || var->nameToken() != nameTok || !var->isArray())
434+
return;
435+
for (const Dimension& constDimension : var->dimensions()) {
436+
auto& dimension = const_cast<Dimension&>(constDimension);
437+
if (dimension.known || dimension.num != 0 || !dimension.tok)
438+
continue;
439+
const ValueFlow::Value* const v = dimension.tok->getKnownValue(ValueFlow::Value::ValueType::INT);
440+
if (v && v->intvalue > 0) {
441+
dimension.known = true;
442+
dimension.num = v->intvalue;
443+
}
444+
}
445+
}
446+
425447
static void valueFlowNumber(TokenList &tokenlist, const Settings& settings)
426448
{
427449
for (Token *tok = tokenlist.front(); tok;) {
450+
if (tok->str() == "]" && !Token::simpleMatch(tok->next(), "["))
451+
valueFlowArrayDimensions(tok);
428452
tok = ValueFlow::valueFlowSetConstantValue(tok, settings);
429453
}
430454

‎lib/vf_common.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ namespace ValueFlow
234234
if (varid1 &&
235235
sz1->variable() &&
236236
sz1->variable()->isArray() &&
237+
!sz1->variable()->isDecayedArrayArgument() &&
237238
!sz1->variable()->dimensions().empty() &&
238239
sz1->variable()->dimensionKnown(0) &&
239240
Token::Match(sz2->astOperand2(), "*|[") && Token::Match(sz2->astOperand2()->astOperand1(), "%varid%", varid1)) {
@@ -245,8 +246,11 @@ namespace ValueFlow
245246
} else if (Token::Match(tok2, "%var% )")) {
246247
if (const Variable *var = tok2->variable()) {
247248
// find the size of the type
249+
const bool isPointer = var->isDecayedArrayArgument();
248250
size_t size = 0;
249-
if (var->isEnumType()) {
251+
if (isPointer) {
252+
size = settings.platform.sizeof_pointer;
253+
} else if (var->isEnumType()) {
250254
size = settings.platform.sizeof_int;
251255
if (var->type()->classScope && var->type()->classScope->enumType)
252256
size = getSizeOfType(var->type()->classScope->enumType, settings);
@@ -262,7 +266,7 @@ namespace ValueFlow
262266
}
263267
// find the number of elements
264268
size_t count = 1;
265-
for (size_t i = 0; i < var->dimensions().size(); ++i) {
269+
for (size_t i = 0; !isPointer && i < var->dimensions().size(); ++i) {
266270
if (var->dimensionKnown(i))
267271
count *= var->dimension(i);
268272
else

‎test/testbufferoverrun.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class TestBufferOverrun : public TestFixture {
166166
TEST_CASE(array_index_76);
167167
TEST_CASE(array_index_77);
168168
TEST_CASE(array_index_78);
169+
TEST_CASE(array_index_79); // #15225
169170
TEST_CASE(array_index_multidim);
170171
TEST_CASE(array_index_switch_in_for);
171172
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -2048,6 +2049,23 @@ class TestBufferOverrun : public TestFixture {
20482049
ASSERT_EQUALS("", errout_str());
20492050
}
20502051

2052+
void array_index_79()
2053+
{
2054+
check("struct S { char c[4]; };\n" // #15225
2055+
"void f() {\n"
2056+
" char a[10 + sizeof(struct S)];\n"
2057+
" a[sizeof(a)] = 0;\n"
2058+
"}\n");
2059+
ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'a[14]' accessed at index 14, which is out of bounds. [arrayIndexOutOfBounds]\n",
2060+
errout_str());
2061+
2062+
check("void f(char a[10]) {\n"
2063+
" a[sizeof(a)] = 0;\n"
2064+
" a[sizeof(a) / sizeof(a[0])] = 0;\n"
2065+
"}\n");
2066+
ASSERT_EQUALS("", errout_str());
2067+
}
2068+
20512069
void array_index_multidim() {
20522070
check("void f()\n"
20532071
"{\n"

‎test/testvalueflow.cpp‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,91 @@ class TestValueFlow : public TestFixture {
15471547
ASSERT_EQUALS(1U, values.size());
15481548
ASSERT_EQUALS(2, values.back().intvalue);
15491549

1550+
code = "struct S { char c[4]; };\n"
1551+
"void f() {\n"
1552+
" char a[10 + sizeof(struct S)];\n"
1553+
" x = sizeof(a);\n"
1554+
"}\n";
1555+
values = tokenValues(code, "( a )");
1556+
ASSERT_EQUALS(1U, values.size());
1557+
ASSERT_EQUALS(14, values.back().intvalue);
1558+
1559+
code = "void f() {\n"
1560+
" short s;\n"
1561+
" char a[10 + sizeof(s)];\n"
1562+
" x = sizeof(a);\n"
1563+
"}\n";
1564+
values = tokenValues(code, "( a )");
1565+
ASSERT_EQUALS(1U, values.size());
1566+
ASSERT_EQUALS(12, values.back().intvalue);
1567+
1568+
code = "struct S { char c[4]; };\n"
1569+
"struct S *p;\n"
1570+
"void f() {\n"
1571+
" char a[10 + sizeof(*p)];\n"
1572+
" x = sizeof(a);\n"
1573+
"}\n";
1574+
values = tokenValues(code, "( a )");
1575+
ASSERT_EQUALS(1U, values.size());
1576+
ASSERT_EQUALS(14, values.back().intvalue);
1577+
1578+
code = "struct S { char c[4]; };\n"
1579+
"void f() {\n"
1580+
" int a[2 + sizeof(struct S)];\n"
1581+
" x = sizeof(a) / sizeof(a[0]);\n"
1582+
"}\n";
1583+
values = tokenValues(code, "/");
1584+
ASSERT_EQUALS(1U, values.size());
1585+
ASSERT_EQUALS(6, values.back().intvalue);
1586+
1587+
code = "struct S { char c[4]; };\n"
1588+
"struct T { char m[10 + sizeof(struct S)]; };\n"
1589+
"void f() {\n"
1590+
" x = sizeof(struct T);\n"
1591+
"}\n";
1592+
values = tokenValues(code, "( struct T )");
1593+
ASSERT_EQUALS(1U, values.size());
1594+
ASSERT_EQUALS(14, values.back().intvalue);
1595+
1596+
code = "struct S { char c[4]; };\n"
1597+
"void f() {\n"
1598+
" char a[10 + sizeof(struct S)];\n"
1599+
" char b[sizeof(a)][2];\n"
1600+
" x = sizeof(b);\n"
1601+
"}\n";
1602+
values = tokenValues(code, "( b )");
1603+
ASSERT_EQUALS(1U, values.size());
1604+
ASSERT_EQUALS(28, values.back().intvalue);
1605+
1606+
code = "void f(char a[10]) {\n" // array parameter is a pointer
1607+
" x = sizeof(a);\n"
1608+
"}\n";
1609+
values = tokenValues(code, "( a )");
1610+
ASSERT_EQUALS(1U, values.size());
1611+
ASSERT_EQUALS(settings.platform.sizeof_pointer, values.back().intvalue);
1612+
1613+
code = "struct S { char c[4]; };\n"
1614+
"void f(char a[10 + sizeof(struct S)]) {\n"
1615+
" x = sizeof(a);\n"
1616+
"}\n";
1617+
values = tokenValues(code, "( a )");
1618+
ASSERT_EQUALS(1U, values.size());
1619+
ASSERT_EQUALS(settings.platform.sizeof_pointer, values.back().intvalue);
1620+
1621+
code = "void f(char (&a)[10]) {\n"
1622+
" x = sizeof(a);\n"
1623+
"}\n";
1624+
values = tokenValues(code, "( a )");
1625+
ASSERT_EQUALS(1U, values.size());
1626+
ASSERT_EQUALS(10, values.back().intvalue);
1627+
1628+
code = "void f(std::array<char, 10> a) {\n"
1629+
" x = sizeof(a);\n"
1630+
"}\n";
1631+
values = tokenValues(code, "( a )");
1632+
ASSERT_EQUALS(1U, values.size());
1633+
ASSERT_EQUALS(10, values.back().intvalue);
1634+
15501635
#define CHECK(A, B, C, D) \
15511636
do { \
15521637
code = "enum " A " E " B " { E0, E1 };\n" \

0 commit comments

Comments
 (0)