Skip to content

Commit 51dc9b1

Browse files
authored
Fix #15225 (FN arrayIndexOutOfBounds: sizeof(arr) unknown when the array dimension contains sizeof of a struct, variable or dereference) (#9037)
1 parent e1f82c1 commit 51dc9b1

8 files changed

Lines changed: 169 additions & 4 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,13 @@ bool Variable::isStlStringViewType() const
27272727
return getFlag(fIsStlType) && valueType() && valueType()->container && valueType()->container->stdStringLike && valueType()->container->view;
27282728
}
27292729

2730+
bool Variable::isDecayedArrayArgument() const
2731+
{
2732+
if (!isArray() || !isArgument() || isReference())
2733+
return false;
2734+
return !(isStlType() && getTypeName() == "std::array") || Token::simpleMatch(nameToken()->next(), "[");
2735+
}
2736+
27302737
std::string Variable::getTypeName() const
27312738
{
27322739
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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,35 @@ 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->linkAt(-1);
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+
if (Token::Match(dimension.tok->previous(), "[<,]"))
440+
continue;
441+
const ValueFlow::Value* const v = dimension.tok->getKnownValue(ValueFlow::Value::ValueType::INT);
442+
if (v && v->intvalue > 0) {
443+
dimension.known = true;
444+
dimension.num = v->intvalue;
445+
}
446+
}
447+
}
448+
425449
static void valueFlowNumber(TokenList &tokenlist, const Settings& settings)
426450
{
427451
for (Token *tok = tokenlist.front(); tok;) {
452+
if (tok->str() == "]" && !Token::simpleMatch(tok->next(), "["))
453+
valueFlowArrayDimensions(tok);
428454
tok = ValueFlow::valueFlowSetConstantValue(tok, settings);
429455
}
430456

‎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: 26 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,31 @@ 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+
check("struct S { char c[4]; };\n"
2069+
"void f() {\n"
2070+
" std::array<char, 3 + sizeof(S)> a[2];\n"
2071+
" char buf[sizeof(a)];\n"
2072+
" buf[5] = 0;\n"
2073+
"}\n");
2074+
ASSERT_EQUALS("", errout_str());
2075+
}
2076+
20512077
void array_index_multidim() {
20522078
check("void f()\n"
20532079
"{\n"

‎test/testsizeof.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,12 @@ class TestSizeof : public TestFixture {
339339
"}\n"
340340
"int g(std::string a[2]) {\n"
341341
" return sizeof(a);\n"
342+
"}\n"
343+
"int h(std::array<int, 2> a[2]) {\n"
344+
" return sizeof(a);\n"
342345
"}\n");
343-
ASSERT_EQUALS("[test.cpp:5:12]: (warning) Using 'sizeof' on array given as function argument returns size of a pointer. [sizeofwithsilentarraypointer]\n",
346+
ASSERT_EQUALS("[test.cpp:5:12]: (warning) Using 'sizeof' on array given as function argument returns size of a pointer. [sizeofwithsilentarraypointer]\n"
347+
"[test.cpp:8:12]: (warning) Using 'sizeof' on array given as function argument returns size of a pointer. [sizeofwithsilentarraypointer]\n",
344348
errout_str());
345349
}
346350

‎test/testvalueflow.cpp‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,98 @@ class TestValueFlow : public TestFixture {
15561556
ASSERT_EQUALS(1U, values.size());
15571557
ASSERT_EQUALS(2, values.back().intvalue);
15581558

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

0 commit comments

Comments
 (0)