Skip to content

Commit 61b9327

Browse files
committed
fixes "issue read X bytes more data than requested (YYYY read, ZZZZ max) - excess data will be lost in"
this problem described at https://s.afterlogic.com/forum/forum_posts.asp?TID=7995&PN=1&TPN=1
1 parent 8bcffb0 commit 61b9327

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

lib/MailSo/Base/StreamWrappers/SubStreams.php

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static function CreateStream($aSubStreams)
7373
$sHashName = \md5(\microtime(true).\rand(1000, 9999));
7474

7575
self::$aStreams[$sHashName] = $aSubStreams;
76-
76+
7777
\MailSo\Base\Loader::IncStatistic('CreateStream/SubStreams');
7878

7979
return \fopen(self::STREAM_NAME.'://'.$sHashName, 'rb');
@@ -89,7 +89,7 @@ protected function &getPart()
8989
{
9090
return $this->aSubStreams[$this->iIndex];
9191
}
92-
92+
9393
return $nNull;
9494
}
9595

@@ -111,7 +111,7 @@ public function stream_open($sPath)
111111
{
112112
$sHashName = $aPath['host'];
113113
$this->sHash = $sHashName;
114-
114+
115115
if (isset(self::$aStreams[$sHashName]) &&
116116
\is_array(self::$aStreams[$sHashName]) &&
117117
0 < \count(self::$aStreams[$sHashName]))
@@ -138,7 +138,7 @@ public function stream_read($iCount)
138138
{
139139
$sReturn = '';
140140
$mCurrentPart = null;
141-
141+
142142
if ($iCount > 0)
143143
{
144144
if ($iCount < \strlen($this->sBuffer))
@@ -170,7 +170,7 @@ public function stream_read($iCount)
170170
{
171171
return false;
172172
}
173-
173+
174174
$sReturn .= $sReadResult;
175175

176176
$iLen = \strlen($sReturn);
@@ -192,8 +192,21 @@ public function stream_read($iCount)
192192
}
193193
else if (\is_string($mCurrentPart))
194194
{
195-
$sReturn .= $mCurrentPart;
196-
$this->iIndex++;
195+
if (\strlen($mCurrentPart) > $iCount)
196+
{
197+
/** $mCurrentPart will not fit in the remaining
198+
* $iCount bytes of $sReturn. Break out of the
199+
* loop without copying it: we will be called
200+
* again with a larger $iCount that it should
201+
* fit into.
202+
*/
203+
break;
204+
}
205+
else
206+
{
207+
$sReturn .= $mCurrentPart;
208+
$this->iIndex++;
209+
}
197210
}
198211
}
199212
}
@@ -258,7 +271,7 @@ public function stream_seek()
258271
{
259272
return false;
260273
}
261-
274+
262275
static public function setGlobalCounter($value)
263276
{
264277
$GLOBALS['counter'] = $value;
@@ -271,11 +284,11 @@ static public function getGlobalCounter($defValue = 0)
271284
self::setGlobalCounter($defValue);
272285
}
273286
return $GLOBALS['counter'];
274-
}
275-
287+
}
288+
276289
static public function incGlobalCounter()
277290
{
278291
self::setGlobalCounter(self::getGlobalCounter() + 1);
279292
}
280-
293+
281294
}

0 commit comments

Comments
 (0)