Skip to content

Commit

Permalink
Binary: avoid overwriting parameter variable in writeUnsignedVarInt/Long
Browse files Browse the repository at this point in the history
this causes misleading outputs in backtraces.
  • Loading branch information
dktapps committed Jan 12, 2022
1 parent 5433b39 commit 5ac7eea
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,16 @@ public static function writeVarInt(int $v) : string{
*/
public static function writeUnsignedVarInt(int $value) : string{
$buf = "";
$value &= 0xffffffff;
$remaining = $value & 0xffffffff;
for($i = 0; $i < 5; ++$i){
if(($value >> 7) !== 0){
$buf .= chr($value | 0x80);
if(($remaining >> 7) !== 0){
$buf .= chr($remaining | 0x80);
}else{
$buf .= chr($value & 0x7f);
$buf .= chr($remaining & 0x7f);
return $buf;
}

$value = (($value >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
$remaining = (($remaining >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
}

throw new InvalidArgumentException("Value too large to be encoded as a VarInt");
Expand Down Expand Up @@ -496,15 +496,16 @@ public static function writeVarLong(int $v) : string{
*/
public static function writeUnsignedVarLong(int $value) : string{
$buf = "";
$remaining = $value;
for($i = 0; $i < 10; ++$i){
if(($value >> 7) !== 0){
$buf .= chr($value | 0x80); //Let chr() take the last byte of this, it's faster than adding another & 0x7f.
if(($remaining >> 7) !== 0){
$buf .= chr($remaining | 0x80); //Let chr() take the last byte of this, it's faster than adding another & 0x7f.
}else{
$buf .= chr($value & 0x7f);
$buf .= chr($remaining & 0x7f);
return $buf;
}

$value = (($value >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
$remaining = (($remaining >> 7) & (PHP_INT_MAX >> 6)); //PHP really needs a logical right-shift operator
}

throw new InvalidArgumentException("Value too large to be encoded as a VarLong");
Expand Down

0 comments on commit 5ac7eea

Please sign in to comment.