Skip to content

Commit ae03b14

Browse files
committed
do not fold sizeof to the base class size when a member size is unknown
1 parent b0d514f commit ae03b14

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8906,7 +8906,8 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
89068906
return v;
89078907
});
89088908
}
8909-
if (accuracy == Accuracy::ExactOrZero && (overflow || (total == 0 && !result.success)))
8909+
// the member sizes are not known exactly, the base class sizes must not be returned alone
8910+
if (accuracy == Accuracy::ExactOrZero && (overflow || !result.success))
89108911
return 0;
89118912
total = std::max(size_t{1}, total);
89128913
size_t align = getAlignOf(*this, settings, accuracy, sizeOf);

‎test/testbufferoverrun.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5089,6 +5089,15 @@ class TestBufferOverrun : public TestFixture {
50895089
"}\n", settings0);
50905090
ASSERT_EQUALS("", errout_str());
50915091

5092+
check("struct B { int i; };\n"
5093+
"struct Inner { char m[0x100000000][0x100000000]; };\n"
5094+
"struct D : B { Inner in; };\n"
5095+
"void f() {\n"
5096+
" D d;\n"
5097+
" memset(&d, 0, 10);\n"
5098+
"}\n", settings0);
5099+
ASSERT_EQUALS("", errout_str());
5100+
50925101
// an array after a bitfield must not be counted as part of the bitfield
50935102
check("struct S { int a : 8; char c[0x2000000000000000]; };\n"
50945103
"void f() {\n"

‎test/testvalueflow.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,6 +2032,23 @@ class TestValueFlow : public TestFixture {
20322032
ASSERT_EQUALS(1U, values.size());
20332033
ASSERT(values.back().isImpossible());
20342034

2035+
// the overflow of a member whose own size overflows must not be lost either
2036+
code = "struct B { int i; };\n"
2037+
"struct Inner { char m[0x100000000][0x100000000]; };\n"
2038+
"struct D : B { Inner in; };\n"
2039+
"x = sizeof(D);\n";
2040+
values = tokenValues(code, "( D");
2041+
ASSERT_EQUALS(1U, values.size());
2042+
ASSERT(values.back().isImpossible());
2043+
2044+
code = "struct B { int i; };\n"
2045+
"struct Inner { int m[0x4000000000000001]; };\n"
2046+
"struct D : B { Inner in[2]; };\n"
2047+
"x = sizeof(D);\n";
2048+
values = tokenValues(code, "( D");
2049+
ASSERT_EQUALS(1U, values.size());
2050+
ASSERT(values.back().isImpossible());
2051+
20352052
// the number of bits of an array after a bitfield does not fit in a size_t,
20362053
// the array must not be counted as part of the bitfield
20372054
code = "struct S { int a : 8; char c[0x2000000000000000]; };\n"

0 commit comments

Comments
 (0)