Skip to content

Commit 370d71b

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 530e59f commit 370d71b

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
@@ -165,6 +165,7 @@ class TestBufferOverrun : public TestFixture {
165165
TEST_CASE(array_index_75);
166166
TEST_CASE(array_index_76);
167167
TEST_CASE(array_index_77);
168+
TEST_CASE(array_index_78); // #15225
168169
TEST_CASE(array_index_multidim);
169170
TEST_CASE(array_index_switch_in_for);
170171
TEST_CASE(array_index_for_in_for); // FP: #2634
@@ -2031,6 +2032,23 @@ class TestBufferOverrun : public TestFixture {
20312032
errout_str());
20322033
}
20332034

2035+
void array_index_78()
2036+
{
2037+
check("struct S { char c[4]; };\n" // #15225
2038+
"void f() {\n"
2039+
" char a[10 + sizeof(struct S)];\n"
2040+
" a[sizeof(a)] = 0;\n"
2041+
"}\n");
2042+
ASSERT_EQUALS("[test.cpp:4:6]: (error) Array 'a[14]' accessed at index 14, which is out of bounds. [arrayIndexOutOfBounds]\n",
2043+
errout_str());
2044+
2045+
check("void f(char a[10]) {\n"
2046+
" a[sizeof(a)] = 0;\n"
2047+
" a[sizeof(a) / sizeof(a[0])] = 0;\n"
2048+
"}\n");
2049+
ASSERT_EQUALS("", errout_str());
2050+
}
2051+
20342052
void array_index_multidim() {
20352053
check("void f()\n"
20362054
"{\n"

‎test/testvalueflow.cpp‎

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

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

0 commit comments

Comments
 (0)