Skip to content

Commit c28b4ad

Browse files
committed
do not sum the member sizes in the overflow check for unions
1 parent ae03b14 commit c28b4ad

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8869,8 +8869,10 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88698869
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88708870
}
88718871
n *= dim;
8872-
size_t padding = (a - (total % a)) % a;
8873-
if (total + padding > maxSize || n > maxSize - total - padding) {
8872+
// the size of a union is max(total, n), so only n has to fit there
8873+
const bool isUnion = typeScope->type == ScopeType::eUnion;
8874+
const size_t padding = isUnion ? 0 : (a - (total % a)) % a;
8875+
if (isUnion ? (n > maxSize) : (total + padding > maxSize || n > maxSize - total - padding)) {
88748876
overflow = true;
88758877
return accuracy == Accuracy::ExactOrZero ? 0 : total;
88768878
}
@@ -8887,7 +8889,7 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88878889
currentBitfieldAlloc = 0;
88888890
currentBitCount = 0;
88898891
}
8890-
return typeScope->type == ScopeType::eUnion ? std::max(total, n) : total + padding + n;
8892+
return isUnion ? std::max(total, n) : total + padding + n;
88918893
};
88928894
Result result = accumulateStructMembers(typeScope, accHelper, accuracy, settings);
88938895
overflow = overflow || result.overflow;

‎test/testbufferoverrun.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5106,6 +5106,14 @@ class TestBufferOverrun : public TestFixture {
51065106
"}\n", settings0);
51075107
ASSERT_EQUALS("", errout_str());
51085108

5109+
// the size of a union is the size of its largest member, a union that fits keeps its size
5110+
check("union U { char a[0x6000000000000000]; char b[0x6000000000000000]; };\n"
5111+
"void f() {\n"
5112+
" U u;\n"
5113+
" memset(&u, 0, 0x7000000000000000);\n"
5114+
"}\n", settings0);
5115+
ASSERT_EQUALS("[test.cpp:4:12]: (error) Buffer is accessed out of bounds: &u [bufferAccessOutOfBounds]\n", errout_str());
5116+
51095117
// sizeof of an array variable must not wrap around
51105118
check("int f() {\n"
51115119
" int a[0x4000000000000001];\n"

‎test/testvalueflow.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,20 @@ class TestValueFlow : public TestFixture {
20572057
ASSERT_EQUALS(1U, values.size());
20582058
ASSERT_EQUALS(0x2000000000000004LL, values.back().intvalue);
20592059

2060+
// the size of a union is the size of its largest member, the members must not be summed
2061+
code = "union U { char a[0x6000000000000000]; char b[0x6000000000000000]; };\n"
2062+
"x = sizeof(U);\n";
2063+
values = tokenValues(code, "( U");
2064+
ASSERT_EQUALS(1U, values.size());
2065+
ASSERT_EQUALS(0x6000000000000000LL, values.back().intvalue);
2066+
2067+
// a union member whose own size does not fit is still an overflow
2068+
code = "union U { char a[0x100000000][0x100000000]; int i; };\n"
2069+
"x = sizeof(U);\n";
2070+
values = tokenValues(code, "( U");
2071+
ASSERT_EQUALS(1U, values.size());
2072+
ASSERT(values.back().isImpossible());
2073+
20602074
// the size in bytes of an array variable does not fit in a MathLib::bigint
20612075
code = "void f() {\n"
20622076
" int a[0x4000000000000001];\n"

0 commit comments

Comments
 (0)