@@ -560,12 +560,18 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
560560 if (!bufTok->valueType ())
561561 return ValueFlow::Value (-1 );
562562
563+ MathLib::bigint index = 0 ;
563564 if (bufTok->isUnaryOp (" &" )) {
564565 bufTok = bufTok->astOperand1 ();
565566 if (Token::simpleMatch (bufTok, " [" )) {
566- const Token* index = bufTok->astOperand2 ();
567- if (!(index && index->hasKnownIntValue () && index->getKnownIntValue () == 0 ))
568- return ValueFlow::Value (-1 );
567+ if (const Token* indexTok = bufTok->astOperand2 ()) {
568+ if (indexTok->hasKnownIntValue ())
569+ index = indexTok->getKnownIntValue ();
570+ else if (const ValueFlow::Value* maxValue = indexTok->getMaxValue (false ))
571+ index = maxValue->intvalue ;
572+ else
573+ return ValueFlow::Value (-1 );
574+ }
569575 bufTok = bufTok->astOperand1 ();
570576 }
571577 }
@@ -600,10 +606,10 @@ ValueFlow::Value CheckBufferOverrunImpl::getBufferSize(const Token *bufTok, cons
600606 v.valueType = ValueFlow::Value::ValueType::BUFFER_SIZE ;
601607
602608 if (var->isPointerArray ())
603- v.intvalue = dim * mSettings .platform .sizeof_pointer ;
609+ v.intvalue = ( dim - index) * mSettings .platform .sizeof_pointer ;
604610 else {
605611 const size_t typeSize = bufTok->valueType ()->getSizeOf (mSettings , ValueType::Accuracy::ExactOrZero, ValueType::SizeOf::Pointee);
606- v.intvalue = dim * typeSize;
612+ v.intvalue = ( dim - index) * typeSize;
607613 }
608614
609615 return v;
0 commit comments