Skip to content

Commit 998ffe3

Browse files
Another fix for #12393 Stack overflow in accumulateStructMembers() (cppcheck-opensource#5934)
1 parent 63ac630 commit 998ffe3

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ static size_t bitCeil(size_t x)
11391139

11401140
static size_t getAlignOf(const ValueType& vt, const Settings& settings)
11411141
{
1142-
if (vt.pointer || vt.isPrimitive()) {
1142+
if (vt.pointer || vt.reference != Reference::None || vt.isPrimitive()) {
11431143
auto align = ValueFlow::getSizeOf(vt, settings);
11441144
return align == 0 ? 0 : bitCeil(align);
11451145
}
@@ -1161,7 +1161,7 @@ static nonneg int getSizeOfType(const Token *typeTok, const Settings &settings)
11611161

11621162
size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings)
11631163
{
1164-
if (vt.pointer)
1164+
if (vt.pointer || vt.reference != Reference::None)
11651165
return settings.platform.sizeof_pointer;
11661166
if (vt.type == ValueType::Type::BOOL || vt.type == ValueType::Type::CHAR)
11671167
return 1;

test/testvalueflow.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,16 @@ class TestValueFlow : public TestFixture {
14481448
values = tokenValues(code, "( X )");
14491449
ASSERT_EQUALS(1U, values.size());
14501450
ASSERT_EQUALS(12, values.back().intvalue);
1451+
1452+
code = "struct T;\n"
1453+
"struct S { T& r; };\n"
1454+
"struct T { S s{ *this }; };\n"
1455+
"void f() {\n"
1456+
" sizeof(T) == sizeof(void*);\n"
1457+
"}\n";
1458+
values = tokenValues(code, "==");
1459+
ASSERT_EQUALS(1U, values.size());
1460+
ASSERT_EQUALS(1LL, values.back().intvalue);
14511461
}
14521462

14531463
void valueFlowComma()

0 commit comments

Comments
 (0)