Skip to content

Commit 6b9e7b0

Browse files
committed
Check read size in ReadableStream::downloadNumBytes()
Negative reads should throw before they cause an fread() failure. Zero-length reads can return an empty string immediately.
1 parent 6ab8c72 commit 6b9e7b0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/GridFS/ReadableStream.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MongoDB\GridFS;
44

5-
use MongoDB\Driver\Exception\Exception;
5+
use MongoDB\Exception\InvalidArgumentException;
66
use MongoDB\GridFS\Exception\CorruptFileException;
77
use stdClass;
88

@@ -68,10 +68,19 @@ public function close()
6868
* if data is not available to be read.
6969
*
7070
* @param integer $numBytes Number of bytes to read
71-
* @return string
71+
* @return string
72+
* @throws InvalidArgumentException if $numBytes is negative
7273
*/
7374
public function downloadNumBytes($numBytes)
7475
{
76+
if ($numBytes < 0) {
77+
throw new InvalidArgumentException(sprintf('$numBytes must be >= zero; given: %d', $numBytes));
78+
}
79+
80+
if ($numBytes == 0) {
81+
return '';
82+
}
83+
7584
if ($this->bufferFresh) {
7685
rewind($this->buffer);
7786
$this->bufferFresh = false;

0 commit comments

Comments
 (0)