Skip to content

Commit

Permalink
Ensure we have enough bytes when reading packets
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Aug 29, 2020
1 parent fb94af6 commit c311afc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ MongoDB for XP Framework ChangeLog

## ?.?.? / ????-??-??

## 0.5.3 / 2020-08-29

* Fixed reading to ensure we have enough bytes when reading packets
(@thekid)

## 0.5.2 / 2020-08-08

* Fixed error #22 (InvalidBSON) when using `NULL` values - @thekid
Expand Down
18 changes: 16 additions & 2 deletions src/main/php/com/mongodb/Protocol.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ public function msg($flags, $kind, $sections) {
throw Error::newInstance($result['body']);
}

/**
* Reads a given number of bytes.
*
* @param int $bytes
* @return string
*/
private function read($bytes) {
$b= '';
do {
$b.= $this->conn->readBinary($bytes);
} while (strlen($b) < $bytes && !$this->conn->eof());
return $b;
}

public function send($operation, $body) {
$this->id > 2147483647 ? $this->id= 1 : $this->id++;

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

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

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

switch ($header['opCode']) {
Expand Down

0 comments on commit c311afc

Please sign in to comment.