From 8a32612e03292946d05cd969128a262ce7b2aa03 Mon Sep 17 00:00:00 2001 From: Michael Calcinai Date: Thu, 22 Oct 2015 15:11:52 +1300 Subject: [PATCH] Fixed initial buffer overflow/underrun handline --- src/Protocol/RFC6455/Frame.php | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Protocol/RFC6455/Frame.php b/src/Protocol/RFC6455/Frame.php index 5032dab..47dcd90 100644 --- a/src/Protocol/RFC6455/Frame.php +++ b/src/Protocol/RFC6455/Frame.php @@ -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){