@@ -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);
0 commit comments