Skip to content

Commit c311afc

Browse files
committed
Ensure we have enough bytes when reading packets
1 parent fb94af6 commit c311afc

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ MongoDB for XP Framework ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
## 0.5.3 / 2020-08-29
7+
8+
* Fixed reading to ensure we have enough bytes when reading packets
9+
(@thekid)
10+
611
## 0.5.2 / 2020-08-08
712

813
* Fixed error #22 (InvalidBSON) when using `NULL` values - @thekid

src/main/php/com/mongodb/Protocol.class.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,20 @@ public function msg($flags, $kind, $sections) {
155155
throw Error::newInstance($result['body']);
156156
}
157157

158+
/**
159+
* Reads a given number of bytes.
160+
*
161+
* @param int $bytes
162+
* @return string
163+
*/
164+
private function read($bytes) {
165+
$b= '';
166+
do {
167+
$b.= $this->conn->readBinary($bytes);
168+
} while (strlen($b) < $bytes && !$this->conn->eof());
169+
return $b;
170+
}
171+
158172
public function send($operation, $body) {
159173
$this->id > 2147483647 ? $this->id= 1 : $this->id++;
160174

@@ -163,10 +177,10 @@ public function send($operation, $body) {
163177
// \util\cmd\Console::writeLine('>>> ', strlen($payload), ': ', $this->hex($payload));
164178
$this->conn->write($payload);
165179

166-
$header= unpack('VmessageLength/VrequestID/VresponseTo/VopCode', $this->conn->readBinary(16));
180+
$header= unpack('VmessageLength/VrequestID/VresponseTo/VopCode', $this->read(16));
167181
// \util\cmd\Console::writeLine('<<< ', $header);
168182

169-
$response= $this->conn->readBinary($header['messageLength'] - 16);
183+
$response= $this->read($header['messageLength'] - 16);
170184
// \util\cmd\Console::writeLine('<<< ', strlen($response), ': ', $this->hex($response));
171185

172186
switch ($header['opCode']) {

0 commit comments

Comments
 (0)