From af50a4a33ade59cf1a52664b149b182148f04e80 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Wed, 21 Oct 2015 23:54:14 +0300 Subject: [PATCH] Throw in Buffer when there is not enough data to unpack --- SourceQuery/Buffer.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/SourceQuery/Buffer.php b/SourceQuery/Buffer.php index f7d2051..854140b 100644 --- a/SourceQuery/Buffer.php +++ b/SourceQuery/Buffer.php @@ -12,10 +12,14 @@ namespace xPaw\SourceQuery; + use xPaw\SourceQuery\Exception\InvalidPacketException; + /** * Class Buffer * * @package xPaw\SourceQuery + * + * @uses xPaw\SourceQuery\Exception\InvalidPacketException */ class Buffer { @@ -121,6 +125,11 @@ */ public function GetShort( ) { + if( $this->Remaining( ) < 2 ) + { + throw new InvalidPacketException( 'Not enough data to unpack a short.', InvalidPacketException::BUFFER_EMPTY ); + } + $Data = UnPack( 'v', $this->Get( 2 ) ); return $Data[ 1 ]; @@ -133,6 +142,11 @@ */ public function GetLong( ) { + if( $this->Remaining( ) < 4 ) + { + throw new InvalidPacketException( 'Not enough data to unpack a long.', InvalidPacketException::BUFFER_EMPTY ); + } + $Data = UnPack( 'l', $this->Get( 4 ) ); return $Data[ 1 ]; @@ -145,6 +159,11 @@ */ public function GetFloat( ) { + if( $this->Remaining( ) < 4 ) + { + throw new InvalidPacketException( 'Not enough data to unpack a float.', InvalidPacketException::BUFFER_EMPTY ); + } + $Data = UnPack( 'f', $this->Get( 4 ) ); return $Data[ 1 ]; @@ -157,6 +176,11 @@ */ public function GetUnsignedLong( ) { + if( $this->Remaining( ) < 4 ) + { + throw new InvalidPacketException( 'Not enough data to unpack an usigned long.', InvalidPacketException::BUFFER_EMPTY ); + } + $Data = UnPack( 'V', $this->Get( 4 ) ); return $Data[ 1 ];