@@ -639,33 +639,23 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
639639 if (!var->dimensionsProduct (dim))
640640 return ValueFlow::Value (-1 );
641641
642+ MathLib::bigint typeSize;
643+ if (var->isPointerArray ())
644+ typeSize = mSettings .platform .sizeof_pointer ;
645+ else
646+ typeSize = bufTok->valueType ()->getSizeOf (mSettings , ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
647+ if (MathLib::mulOverflows (dim - index, typeSize))
648+ return ValueFlow::Value (-1 );
649+
642650 ValueFlow::Value v;
643651 v.setKnown ();
644652 v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE ;
645-
646- if (var->isPointerArray ())
647- v.intvalue = (dim - index) * mSettings .platform .sizeof_pointer ;
648- else {
649- const size_t typeSize = bufTok->valueType ()->getSizeOf (mSettings , ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
650- v.intvalue = (dim - index) * typeSize;
651- }
653+ v.intvalue = (dim - index) * typeSize;
652654
653655 return v;
654656}
655657// ---------------------------------------------------------------------------
656658
657- // Return true if a * b does not fit in a MathLib::bigint
658- static bool mulOverflows (MathLib::bigint a, MathLib::bigint b)
659- {
660- const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max ();
661- const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min ();
662- if (a > 0 )
663- return b > 0 ? a > maxValue / b : b < minValue / a;
664- if (a < 0 )
665- return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
666- return false ;
667- }
668-
669659static bool checkBufferSize (const Token *ftok, const Library::ArgumentChecks::MinSize &minsize, const std::vector<const Token *> &args, ValueFlow::Value& bufferSize, const Settings &settings, const Tokenizer* tokenizer)
670660{
671661 const Token * const arg = (minsize.arg > 0 && minsize.arg - 1 < args.size ()) ? args[minsize.arg - 1 ] : nullptr ;
@@ -690,7 +680,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
690680 break ;
691681 MathLib::bigint myMinsize = argVal->intvalue ;
692682 const int baseSize = tokenizer->sizeOfType (minsize.baseType );
693- const bool overflow = baseSize != 0 && mulOverflows (myMinsize, baseSize);
683+ const bool overflow = baseSize != 0 && MathLib:: mulOverflows (myMinsize, baseSize);
694684 if (baseSize != 0 && !overflow)
695685 myMinsize *= baseSize;
696686 const bool ok = !overflow && myMinsize <= bufferSize.intvalue ;
@@ -708,7 +698,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
708698 break ;
709699 case Library::ArgumentChecks::MinSize::Type::MUL :
710700 if (arg && arg2 && arg->hasKnownIntValue () && arg2->hasKnownIntValue ()) {
711- if (mulOverflows (arg->getKnownIntValue (), arg2->getKnownIntValue ()))
701+ if (MathLib:: mulOverflows (arg->getKnownIntValue (), arg2->getKnownIntValue ()))
712702 return false ;
713703 return (arg->getKnownIntValue () * arg2->getKnownIntValue ()) <= bufferSize.intvalue ;
714704 }
@@ -717,7 +707,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
717707 MathLib::bigint myMinsize = minsize.value ;
718708 const int baseSize = tokenizer->sizeOfType (minsize.baseType );
719709 if (baseSize != 0 ) {
720- if (mulOverflows (myMinsize, baseSize))
710+ if (MathLib:: mulOverflows (myMinsize, baseSize))
721711 return false ;
722712 myMinsize *= baseSize;
723713 }
0 commit comments