Skip to content

Commit f84e133

Browse files
committed
keep the sizeof overflow when base classes are added
1 parent 1906bfa commit f84e133

3 files changed

Lines changed: 78 additions & 11 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8695,6 +8695,7 @@ namespace {
86958695
{
86968696
size_t total;
86978697
bool success;
8698+
bool overflow;
86988699
};
86998700
}
87008701

@@ -8709,10 +8710,10 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
87098710
const MathLib::bigint bits = var.nameToken() ? var.nameToken()->bits() : -1;
87108711
if (const ValueType* vt = var.valueType()) {
87118712
if (vt->type == ValueType::Type::RECORD && vt->typeScope == scope)
8712-
return {0, false};
8713+
return {0, false, false};
87138714
MathLib::bigint dim = 0;
87148715
if (!var.dimensionsProduct(dim))
8715-
return {0, false};
8716+
return {0, false, true};
87168717
if (var.nameToken()->scope() != scope && var.nameToken()->scope()->definedType) { // anonymous union
87178718
const auto ret = anonScopes.insert(var.nameToken()->scope());
87188719
if (ret.second)
@@ -8726,9 +8727,9 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
87268727
total = f(total, *vt, dim, bits);
87278728
}
87288729
if (accuracy == ValueType::Accuracy::ExactOrZero && total == 0 && bits == -1)
8729-
return {0, false};
8730+
return {0, false, false};
87308731
}
8731-
return {total, true};
8732+
return {total, true, false};
87328733
}
87338734

87348735

@@ -8809,6 +8810,9 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88098810
if (type == ValueType::Type::RECORD && typeScope) {
88108811
size_t currentBitCount = 0;
88118812
size_t currentBitfieldAlloc = 0;
8813+
// the size must fit in a MathLib::bigint
8814+
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
8815+
bool overflow = false;
88128816
auto accHelper = [&](size_t total, const ValueType& vt2, size_t dim, MathLib::bigint nBits) -> size_t {
88138817
const size_t charBit = settings.platform.char_bit;
88148818
size_t n = vt2.getSizeOf(settings, accuracy, SizeOf::Pointer, ++maxRecursion);
@@ -8839,14 +8843,16 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88398843
currentBitCount += nBits;
88408844
return ret;
88418845
}
8842-
// the size must fit in a MathLib::bigint
8843-
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
8844-
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim)))
8846+
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim))) {
8847+
overflow = true;
88458848
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8849+
}
88468850
n *= dim;
88478851
size_t padding = (a - (total % a)) % a;
8848-
if (total + padding > maxSize || n > maxSize - total - padding)
8852+
if (total + padding > maxSize || n > maxSize - total - padding) {
8853+
overflow = true;
88498854
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8855+
}
88508856
if (currentBitCount > 0) {
88518857
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
88528858
bool isAligned = currentBitCount % (charBit * a) == 0;
@@ -8861,17 +8867,23 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88618867
return typeScope->type == ScopeType::eUnion ? std::max(total, n) : total + padding + n;
88628868
};
88638869
Result result = accumulateStructMembers(typeScope, accHelper, accuracy, settings);
8870+
overflow = overflow || result.overflow;
88648871
size_t total = result.total;
88658872
if (currentBitCount > 0)
88668873
total += currentBitfieldAlloc;
88678874
if (const ::Type* dt = typeScope->definedType) {
88688875
total = std::accumulate(dt->derivedFrom.begin(), dt->derivedFrom.end(), total, [&](size_t v, const ::Type::BaseInfo& bi) {
8869-
if (bi.type && bi.type->classScope)
8870-
v += accumulateStructMembers(bi.type->classScope, accHelper, accuracy, settings).total;
8876+
if (bi.type && bi.type->classScope) {
8877+
const Result baseResult = accumulateStructMembers(bi.type->classScope, accHelper, accuracy, settings);
8878+
if (baseResult.overflow || v > maxSize || baseResult.total > maxSize - v)
8879+
overflow = true;
8880+
else
8881+
v += baseResult.total;
8882+
}
88718883
return v;
88728884
});
88738885
}
8874-
if (accuracy == Accuracy::ExactOrZero && total == 0 && !result.success)
8886+
if (accuracy == Accuracy::ExactOrZero && (overflow || (total == 0 && !result.success)))
88758887
return 0;
88768888
total = std::max(size_t{1}, total);
88778889
size_t align = getAlignOf(*this, settings, accuracy, sizeOf);

‎test/testbufferoverrun.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5017,6 +5017,23 @@ class TestBufferOverrun : public TestFixture {
50175017
" memset(a, 0, 10);\n"
50185018
"}\n", settings0);
50195019
ASSERT_EQUALS("", errout_str());
5020+
5021+
// the size of a derived class must not wrap around
5022+
check("struct B { int i; };\n"
5023+
"struct D : B { char m[0x100000000][0x100000000]; };\n"
5024+
"void f() {\n"
5025+
" D d;\n"
5026+
" memset(&d, 0, 10);\n"
5027+
"}\n", settings0);
5028+
ASSERT_EQUALS("", errout_str());
5029+
5030+
check("struct B { int i; };\n"
5031+
"struct D : B { char b[0x6000000000000000]; char c[0x6000000000000000]; };\n"
5032+
"int f() {\n"
5033+
" char buf[2] = {0};\n"
5034+
" return buf[sizeof(D)];\n"
5035+
"}\n", settings0);
5036+
ASSERT_EQUALS("", errout_str());
50205037
}
50215038

50225039
// extracttests.enable

‎test/testvalueflow.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,44 @@ class TestValueFlow : public TestFixture {
18781878
values = tokenValues(code, "( S");
18791879
ASSERT_EQUALS(1U, values.size());
18801880
ASSERT(values.back().isImpossible());
1881+
1882+
// the overflow must not be lost when base classes are added
1883+
code = "struct B { int i; };\n"
1884+
"struct D : B { int m[0x4000000000000001]; };\n"
1885+
"x = sizeof(D);\n";
1886+
values = tokenValues(code, "( D");
1887+
ASSERT_EQUALS(1U, values.size());
1888+
ASSERT(values.back().isImpossible());
1889+
1890+
code = "struct B { int i; };\n"
1891+
"struct D : B { char m[0x100000000][0x100000000]; };\n"
1892+
"x = sizeof(D);\n";
1893+
values = tokenValues(code, "( D");
1894+
ASSERT_EQUALS(1U, values.size());
1895+
ASSERT(values.back().isImpossible());
1896+
1897+
code = "struct B { int i; };\n"
1898+
"struct D : B { char b[0x6000000000000000]; char c[0x6000000000000000]; };\n"
1899+
"x = sizeof(D);\n";
1900+
values = tokenValues(code, "( D");
1901+
ASSERT_EQUALS(1U, values.size());
1902+
ASSERT(values.back().isImpossible());
1903+
1904+
code = "struct B { char m[0x100000000][0x100000000]; };\n"
1905+
"struct D : B { int i; };\n"
1906+
"x = sizeof(D);\n";
1907+
values = tokenValues(code, "( D");
1908+
ASSERT_EQUALS(1U, values.size());
1909+
ASSERT(values.back().isImpossible());
1910+
1911+
// the sum of the base class sizes does not fit in a MathLib::bigint
1912+
code = "struct B1 { char m[0x6000000000000000]; };\n"
1913+
"struct B2 { char m[0x6000000000000000]; };\n"
1914+
"struct D : B1, B2 { char c; };\n"
1915+
"x = sizeof(D);\n";
1916+
values = tokenValues(code, "( D");
1917+
ASSERT_EQUALS(1U, values.size());
1918+
ASSERT(values.back().isImpossible());
18811919
}
18821920

18831921
void valueFlowComma()

0 commit comments

Comments
 (0)