Skip to content

Commit 5ac7eea

Browse files
committed
Binary: avoid overwriting parameter variable in writeUnsignedVarInt/Long
this causes misleading outputs in backtraces.
1 parent 5433b39 commit 5ac7eea

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Diff for: src/Binary.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -432,16 +432,16 @@ public static function writeVarInt(int $v) : string{
432432
*/
433433
public static function writeUnsignedVarInt(int $value) : string{
434434
$buf = "";
435-
$value &= 0xffffffff;
435+
$remaining = $value & 0xffffffff;
436436
for($i = 0; $i < 5; ++$i){
437-
if(($value >> 7) !== 0){
438-
$buf .= chr($value | 0x80);
437+
if(($remaining >> 7) !== 0){
438+
$buf .= chr($remaining | 0x80);
439439
}else{
440-
$buf .= chr($value & 0x7f);
440+
$buf .= chr($remaining & 0x7f);
441441
return $buf;
442442
}
443443

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

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

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

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

0 commit comments

Comments
 (0)