Skip to content

Commit

Permalink
Fixed initial buffer overflow/underrun handline
Browse files Browse the repository at this point in the history
  • Loading branch information
calcinai committed Oct 22, 2015
1 parent 39d60de commit 8a32612
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/Protocol/RFC6455/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,30 +138,35 @@ public function appendBuffer(&$buffer){
$this->decode($buffer);
}

$this->appendPayload($buffer);
return $this->appendPayload($buffer);
}

public function appendPayload($buffer){

$payload_length = strlen($this->payload);
$outstanding = $this->frame_length - $payload_length;
$available_length = $payload_length + strlen($buffer);

if($available_length < $outstanding){
//Underrun
throw new IncompletePayloadException($this);
}

$buffer_to_append = substr($buffer, 0, $outstanding);

if($this->isMasked()){
self::mask($buffer, $this->masking_key);
self::mask($buffer_to_append, $this->masking_key);
}

$this->payload .= $buffer;
$this->payload .= $buffer_to_append;

$payload_length = strlen($this->payload);
if($payload_length < $this->frame_length){
//Underrun
throw new IncompletePayloadException($this);
} elseif($payload_length > $this->frame_length){
//Overflow
$this->payload = substr($this->payload, 0, $this->frame_length);
return substr($this->payload, $this->frame_length);
} else{
//No underrun/overflow
return null;
if($available_length > $outstanding){
//Overflow - get what's past what's outstanding
return substr($buffer, $outstanding);
}

return null;

}

public function decode(&$buffer){
Expand Down

0 comments on commit 8a32612

Please sign in to comment.