Skip to content

Commit db13836

Browse files
Pure PHP: cleanly ignore bits in the 10-byte varints with bit above LSB set.
Still error out if the MSB in that is set (implying it would be an 11+ byte varint). This is not really functional change; today this flow is just silently 'weird' where readVarint64() can return a float64 since php silently promotes the type when it doesn't fit. PiperOrigin-RevId: 899065019
1 parent b7e554a commit db13836

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

php/tests/PhpImplementationTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,20 @@ public function testReadVarint64()
379379
$input = new CodedInputStream(hex2bin(''));
380380
$this->assertFalse($input->readVarint64($var));
381381

382-
// The largest varint is 10 bytes long.
382+
// varints which are >10 bytes long aren't allowed
383383
$input = new CodedInputStream(hex2bin('8080808080808080808001'));
384384
$this->assertFalse($input->readVarint64($var));
385385

386+
// 10-byte varint with high bits set in the 10th byte.
387+
// Bits above bit 0 in the 10th byte are discarded.
388+
$input = new CodedInputStream(hex2bin('87808080808080808002'));
389+
$this->assertTrue($input->readVarint64($var));
390+
if (PHP_INT_SIZE == 4) {
391+
$this->assertSame('7', $var);
392+
} else {
393+
$this->assertSame(7, $var);
394+
}
395+
386396
// Corrupted varint.
387397
$input = new CodedInputStream(hex2bin('808080'));
388398
$this->assertFalse($input->readVarint64($var));

0 commit comments

Comments
 (0)