From 5d0f897f2edc1cc560156f13dc7de8cee4f31a12 Mon Sep 17 00:00:00 2001 From: Robert L Mathews Date: Wed, 7 Apr 2021 11:22:51 -0700 Subject: [PATCH] A better patch for the problem described at https://s.afterlogic.com/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. --- lib/MailSo/Base/StreamWrappers/SubStreams.php | 45 +++++++------------ 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/lib/MailSo/Base/StreamWrappers/SubStreams.php b/lib/MailSo/Base/StreamWrappers/SubStreams.php index ccbf7d4..0172f94 100755 --- a/lib/MailSo/Base/StreamWrappers/SubStreams.php +++ b/lib/MailSo/Base/StreamWrappers/SubStreams.php @@ -138,7 +138,7 @@ public function stream_read($iCount) { $sReturn = ''; $mCurrentPart = null; - + if ($iCount > 0) { if ($iCount < \strlen($this->sBuffer)) @@ -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 { @@ -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; } } }