Skip to content

Commit

Permalink
A better patch for the problem described at https://s.afterlogic.com/…
Browse files Browse the repository at this point in the history
…forum/forum_posts.asp?TID=7995&PN=1&TPN=1

This updated patch addresses the same problem, but does so more elegantly by moving the existing code that handles buffering of data larger than $iCount so that it applies to the "if (\is_string($mCurrentPart))" case too.

This will make the code correctly handle a case where $mCurrentPart is > 8192 bytes to start with, which neither the original code nor the first version of the patch handled.
  • Loading branch information
Robert L Mathews committed Apr 7, 2021
1 parent 61b9327 commit 5d0f897
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions lib/MailSo/Base/StreamWrappers/SubStreams.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function stream_read($iCount)
{
$sReturn = '';
$mCurrentPart = null;

if ($iCount > 0)
{
if ($iCount < \strlen($this->sBuffer))
Expand Down Expand Up @@ -170,20 +170,8 @@ public function stream_read($iCount)
{
return false;
}

$sReturn .= $sReadResult;

$iLen = \strlen($sReturn);
if ($iCount < $iLen)
{
$this->sBuffer = \substr($sReturn, $iCount);
$sReturn = \substr($sReturn, 0, $iCount);
$iCount = 0;
}
else
{
$iCount -= $iLen;
}
}
else
{
Expand All @@ -192,21 +180,20 @@ public function stream_read($iCount)
}
else if (\is_string($mCurrentPart))
{
if (\strlen($mCurrentPart) > $iCount)
{
/** $mCurrentPart will not fit in the remaining
* $iCount bytes of $sReturn. Break out of the
* loop without copying it: we will be called
* again with a larger $iCount that it should
* fit into.
*/
break;
}
else
{
$sReturn .= $mCurrentPart;
$this->iIndex++;
}
$sReturn .= $mCurrentPart;
$this->iIndex++;
}

$iLen = \strlen($sReturn);
if ($iCount < $iLen)
{
$this->sBuffer = \substr($sReturn, $iCount);
$sReturn = \substr($sReturn, 0, $iCount);
$iCount = 0;
}
else
{
$iCount -= $iLen;
}
}
}
Expand Down

0 comments on commit 5d0f897

Please sign in to comment.