Skip to content

Commit 2aee9b3

Browse files
Fix sizeof for multiple array members (f'up to #12479) (#6081)
1 parent 2464a9d commit 2aee9b3

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/valueflow.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,11 +1116,10 @@ static size_t accumulateStructMembers(const Scope* scope, F f)
11161116
if (const ValueType* vt = var.valueType()) {
11171117
if (vt->type == ValueType::Type::RECORD && vt->typeScope == scope)
11181118
return 0;
1119-
total = f(total, *vt);
11201119
const MathLib::bigint dim = std::accumulate(var.dimensions().cbegin(), var.dimensions().cend(), 1LL, [](MathLib::bigint i1, const Dimension& dim) {
11211120
return i1 * dim.num;
11221121
});
1123-
total *= dim;
1122+
total = f(total, *vt, dim);
11241123
}
11251124
if (total == 0)
11261125
return 0;
@@ -1149,7 +1148,7 @@ static size_t getAlignOf(const ValueType& vt, const Settings& settings)
11491148
return align == 0 ? 0 : bitCeil(align);
11501149
}
11511150
if (vt.type == ValueType::Type::RECORD && vt.typeScope) {
1152-
return accumulateStructMembers(vt.typeScope, [&](size_t max, const ValueType& vt2) {
1151+
return accumulateStructMembers(vt.typeScope, [&](size_t max, const ValueType& vt2, size_t /*dim*/) {
11531152
size_t a = getAlignOf(vt2, settings);
11541153
return std::max(max, a);
11551154
});
@@ -1187,11 +1186,12 @@ size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings)
11871186
if (vt.type == ValueType::Type::LONGDOUBLE)
11881187
return settings.platform.sizeof_long_double;
11891188
if (vt.type == ValueType::Type::RECORD && vt.typeScope) {
1190-
size_t total = accumulateStructMembers(vt.typeScope, [&](size_t total, const ValueType& vt2) -> size_t {
1189+
size_t total = accumulateStructMembers(vt.typeScope, [&](size_t total, const ValueType& vt2, size_t dim) -> size_t {
11911190
size_t n = ValueFlow::getSizeOf(vt2, settings);
11921191
size_t a = getAlignOf(vt2, settings);
11931192
if (n == 0 || a == 0)
11941193
return 0;
1194+
n *= dim;
11951195
size_t padding = (a - (total % a)) % a;
11961196
return total + padding + n;
11971197
});

test/testvalueflow.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,29 @@ class TestValueFlow : public TestFixture {
14661466
values = tokenValues(code, "=");
14671467
ASSERT_EQUALS(1U, values.size());
14681468
ASSERT_EQUALS(4LL * 2 * 10, values.back().intvalue);
1469+
1470+
code = "struct S {\n"
1471+
" int a[10];\n"
1472+
" int b[20];\n"
1473+
" int c[30];\n"
1474+
"};\n"
1475+
"void f() {\n"
1476+
" x = sizeof(S);\n"
1477+
"}\n";
1478+
values = tokenValues(code, "=");
1479+
ASSERT_EQUALS(1U, values.size());
1480+
ASSERT_EQUALS(4LL * (10 + 20 + 30), values.back().intvalue);
1481+
1482+
code = "struct S {\n"
1483+
" char c;\n"
1484+
" int a[4];\n"
1485+
"};\n"
1486+
"void f() {\n"
1487+
" x = sizeof(S);\n"
1488+
"}\n";
1489+
values = tokenValues(code, "=");
1490+
ASSERT_EQUALS(1U, values.size());
1491+
ASSERT_EQUALS(4LL * 5, values.back().intvalue);
14691492
}
14701493

14711494
void valueFlowComma()

0 commit comments

Comments
 (0)