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