Skip to content

Commit 1621b9e

Browse files
Nussu06metsw24-max
authored andcommitted
keep the sizeof overflow when base classes are added
1 parent d9eb5c7 commit 1621b9e

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
@@ -8712,6 +8712,7 @@ namespace {
87128712
{
87138713
size_t total;
87148714
bool success;
8715+
bool overflow;
87158716
};
87168717
}
87178718

@@ -8726,10 +8727,10 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
87268727
const MathLib::bigint bits = var.nameToken() ? var.nameToken()->bits() : -1;
87278728
if (const ValueType* vt = var.valueType()) {
87288729
if (vt->type == ValueType::Type::RECORD && vt->typeScope == scope)
8729-
return {0, false};
8730+
return {0, false, false};
87308731
MathLib::bigint dim = 0;
87318732
if (!var.dimensionsProduct(dim))
8732-
return {0, false};
8733+
return {0, false, true};
87338734
if (var.nameToken()->scope() != scope && var.nameToken()->scope()->definedType) { // anonymous union
87348735
const auto ret = anonScopes.insert(var.nameToken()->scope());
87358736
if (ret.second)
@@ -8743,9 +8744,9 @@ static Result accumulateStructMembers(const Scope* scope, F f, ValueType::Accura
87438744
total = f(total, *vt, dim, bits);
87448745
}
87458746
if (accuracy == ValueType::Accuracy::ExactOrZero && total == 0 && bits == -1)
8746-
return {0, false};
8747+
return {0, false, false};
87478748
}
8748-
return {total, true};
8749+
return {total, true, false};
87498750
}
87508751

87518752

@@ -8826,6 +8827,9 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88268827
if (type == ValueType::Type::RECORD && typeScope) {
88278828
size_t currentBitCount = 0;
88288829
size_t currentBitfieldAlloc = 0;
8830+
// the size must fit in a MathLib::bigint
8831+
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
8832+
bool overflow = false;
88298833
auto accHelper = [&](size_t total, const ValueType& vt2, size_t dim, MathLib::bigint nBits) -> size_t {
88308834
const size_t charBit = settings.platform.char_bit;
88318835
size_t n = vt2.getSizeOf(settings, accuracy, SizeOf::Pointer, ++maxRecursion);
@@ -8856,14 +8860,16 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88568860
currentBitCount += nBits;
88578861
return ret;
88588862
}
8859-
// the size must fit in a MathLib::bigint
8860-
const size_t maxSize = std::numeric_limits<MathLib::bigint>::max();
8861-
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim)))
8863+
if (MathLib::mulOverflows(static_cast<MathLib::bigint>(n), static_cast<MathLib::bigint>(dim))) {
8864+
overflow = true;
88628865
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8866+
}
88638867
n *= dim;
88648868
size_t padding = (a - (total % a)) % a;
8865-
if (total + padding > maxSize || n > maxSize - total - padding)
8869+
if (total + padding > maxSize || n > maxSize - total - padding) {
8870+
overflow = true;
88668871
return accuracy == Accuracy::ExactOrZero ? 0 : total;
8872+
}
88678873
if (currentBitCount > 0) {
88688874
bool fitsInBitfield = currentBitCount + (n * charBit) <= currentBitfieldAlloc * charBit;
88698875
bool isAligned = currentBitCount % (charBit * a) == 0;
@@ -8878,17 +8884,23 @@ size_t ValueType::getSizeOf( const Settings& settings, Accuracy accuracy, SizeOf
88788884
return typeScope->type == ScopeType::eUnion ? std::max(total, n) : total + padding + n;
88798885
};
88808886
Result result = accumulateStructMembers(typeScope, accHelper, accuracy, settings);
8887+
overflow = overflow || result.overflow;
88818888
size_t total = result.total;
88828889
if (currentBitCount > 0)
88838890
total += currentBitfieldAlloc;
88848891
if (const ::Type* dt = typeScope->definedType) {
88858892
total = std::accumulate(dt->derivedFrom.begin(), dt->derivedFrom.end(), total, [&](size_t v, const ::Type::BaseInfo& bi) {
8886-
if (bi.type && bi.type->classScope)
8887-
v += accumulateStructMembers(bi.type->classScope, accHelper, accuracy, settings).total;
8893+
if (bi.type && bi.type->classScope) {
8894+
const Result baseResult = accumulateStructMembers(bi.type->classScope, accHelper, accuracy, settings);
8895+
if (baseResult.overflow || v > maxSize || baseResult.total > maxSize - v)
8896+
overflow = true;
8897+
else
8898+
v += baseResult.total;
8899+
}
88888900
return v;
88898901
});
88908902
}
8891-
if (accuracy == Accuracy::ExactOrZero && total == 0 && !result.success)
8903+
if (accuracy == Accuracy::ExactOrZero && (overflow || (total == 0 && !result.success)))
88928904
return 0;
88938905
total = std::max(size_t{1}, total);
88948906
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
@@ -5071,6 +5071,23 @@ class TestBufferOverrun : public TestFixture {
50715071
" memset(a, 0, 10);\n"
50725072
"}\n", settings0);
50735073
ASSERT_EQUALS("", errout_str());
5074+
5075+
// the size of a derived class must not wrap around
5076+
check("struct B { int i; };\n"
5077+
"struct D : B { char m[0x100000000][0x100000000]; };\n"
5078+
"void f() {\n"
5079+
" D d;\n"
5080+
" memset(&d, 0, 10);\n"
5081+
"}\n", settings0);
5082+
ASSERT_EQUALS("", errout_str());
5083+
5084+
check("struct B { int i; };\n"
5085+
"struct D : B { char b[0x6000000000000000]; char c[0x6000000000000000]; };\n"
5086+
"int f() {\n"
5087+
" char buf[2] = {0};\n"
5088+
" return buf[sizeof(D)];\n"
5089+
"}\n", settings0);
5090+
ASSERT_EQUALS("", errout_str());
50745091
}
50755092

50765093
// extracttests.enable

‎test/testvalueflow.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,44 @@ class TestValueFlow : public TestFixture {
19931993
values = tokenValues(code, "( S");
19941994
ASSERT_EQUALS(1U, values.size());
19951995
ASSERT(values.back().isImpossible());
1996+
1997+
// the overflow must not be lost when base classes are added
1998+
code = "struct B { int i; };\n"
1999+
"struct D : B { int m[0x4000000000000001]; };\n"
2000+
"x = sizeof(D);\n";
2001+
values = tokenValues(code, "( D");
2002+
ASSERT_EQUALS(1U, values.size());
2003+
ASSERT(values.back().isImpossible());
2004+
2005+
code = "struct B { int i; };\n"
2006+
"struct D : B { char m[0x100000000][0x100000000]; };\n"
2007+
"x = sizeof(D);\n";
2008+
values = tokenValues(code, "( D");
2009+
ASSERT_EQUALS(1U, values.size());
2010+
ASSERT(values.back().isImpossible());
2011+
2012+
code = "struct B { int i; };\n"
2013+
"struct D : B { char b[0x6000000000000000]; char c[0x6000000000000000]; };\n"
2014+
"x = sizeof(D);\n";
2015+
values = tokenValues(code, "( D");
2016+
ASSERT_EQUALS(1U, values.size());
2017+
ASSERT(values.back().isImpossible());
2018+
2019+
code = "struct B { char m[0x100000000][0x100000000]; };\n"
2020+
"struct D : B { int i; };\n"
2021+
"x = sizeof(D);\n";
2022+
values = tokenValues(code, "( D");
2023+
ASSERT_EQUALS(1U, values.size());
2024+
ASSERT(values.back().isImpossible());
2025+
2026+
// the sum of the base class sizes does not fit in a MathLib::bigint
2027+
code = "struct B1 { char m[0x6000000000000000]; };\n"
2028+
"struct B2 { char m[0x6000000000000000]; };\n"
2029+
"struct D : B1, B2 { char c; };\n"
2030+
"x = sizeof(D);\n";
2031+
values = tokenValues(code, "( D");
2032+
ASSERT_EQUALS(1U, values.size());
2033+
ASSERT(values.back().isImpossible());
19962034
}
19972035

19982036
void valueFlowComma()

0 commit comments

Comments
 (0)