@@ -635,33 +635,23 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
635635 if (!var->dimensionsProduct (dim))
636636 return ValueFlow::Value (-1 );
637637
638+ MathLib::bigint typeSize;
639+ if (var->isPointerArray ())
640+ typeSize = mSettings .platform .sizeof_pointer ;
641+ else
642+ typeSize = bufTok->valueType ()->getSizeOf (mSettings , ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
643+ if (MathLib::mulOverflows (dim - index, typeSize))
644+ return ValueFlow::Value (-1 );
645+
638646 ValueFlow::Value v;
639647 v.setKnown ();
640648 v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE ;
641-
642- if (var->isPointerArray ())
643- v.intvalue = (dim - index) * mSettings .platform .sizeof_pointer ;
644- else {
645- const size_t typeSize = bufTok->valueType ()->getSizeOf (mSettings , ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
646- v.intvalue = (dim - index) * typeSize;
647- }
649+ v.intvalue = (dim - index) * typeSize;
648650
649651 return v;
650652}
651653// ---------------------------------------------------------------------------
652654
653- // Return true if a * b does not fit in a MathLib::bigint
654- static bool mulOverflows (MathLib::bigint a, MathLib::bigint b)
655- {
656- const MathLib::bigint maxValue = std::numeric_limits<MathLib::bigint>::max ();
657- const MathLib::bigint minValue = std::numeric_limits<MathLib::bigint>::min ();
658- if (a > 0 )
659- return b > 0 ? a > maxValue / b : b < minValue / a;
660- if (a < 0 )
661- return b > 0 ? a < minValue / b : (b < 0 && a < maxValue / b);
662- return false ;
663- }
664-
665655static 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)
666656{
667657 const Token * const arg = (minsize.arg > 0 && minsize.arg - 1 < args.size ()) ? args[minsize.arg - 1 ] : nullptr ;
@@ -686,7 +676,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
686676 break ;
687677 MathLib::bigint myMinsize = argVal->intvalue ;
688678 const int baseSize = tokenizer->sizeOfType (minsize.baseType );
689- const bool overflow = baseSize != 0 && mulOverflows (myMinsize, baseSize);
679+ const bool overflow = baseSize != 0 && MathLib:: mulOverflows (myMinsize, baseSize);
690680 if (baseSize != 0 && !overflow)
691681 myMinsize *= baseSize;
692682 const bool ok = !overflow && myMinsize <= bufferSize.intvalue ;
@@ -704,7 +694,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
704694 break ;
705695 case Library::ArgumentChecks::MinSize::Type::MUL :
706696 if (arg && arg2 && arg->hasKnownIntValue () && arg2->hasKnownIntValue ()) {
707- if (mulOverflows (arg->getKnownIntValue (), arg2->getKnownIntValue ()))
697+ if (MathLib:: mulOverflows (arg->getKnownIntValue (), arg2->getKnownIntValue ()))
708698 return false ;
709699 return (arg->getKnownIntValue () * arg2->getKnownIntValue ()) <= bufferSize.intvalue ;
710700 }
@@ -713,7 +703,7 @@ static bool checkBufferSize(const Token *ftok, const Library::ArgumentChecks::Mi
713703 MathLib::bigint myMinsize = minsize.value ;
714704 const int baseSize = tokenizer->sizeOfType (minsize.baseType );
715705 if (baseSize != 0 ) {
716- if (mulOverflows (myMinsize, baseSize))
706+ if (MathLib:: mulOverflows (myMinsize, baseSize))
717707 return false ;
718708 myMinsize *= baseSize;
719709 }
0 commit comments