Skip to content

Commit ea68cc0

Browse files
authored
Merge pull request #26366 from protocolbuffers/php-cherrypick-33.x
Backport php fixes to 33.x
2 parents d24be29 + 60e93d2 commit ea68cc0

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

php/src/Google/Protobuf/Internal/CodedInputStream.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,8 @@ public function readTag()
271271
public function readRaw($size, &$buffer)
272272
{
273273
$current_buffer_size = 0;
274-
if ($this->bufferSize() < $size) {
274+
// size (varint) read from the wire could be negative.
275+
if ($size < 0 || $this->bufferSize() < $size) {
275276
return false;
276277
}
277278

@@ -337,7 +338,7 @@ public function incrementRecursionDepthAndPushLimit(
337338
$byte_limit, &$old_limit, &$recursion_budget)
338339
{
339340
$old_limit = $this->pushLimit($byte_limit);
340-
$recursion_limit = --$this->recursion_limit;
341+
$recursion_budget = --$this->recursion_budget;
341342
}
342343

343344
public function decrementRecursionDepthAndPopLimit($byte_limit)

php/tests/EncodeDecodeTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,38 @@ public function testDecodeNegativeInt32()
603603
$this->assertEquals(-1, $m->getOptionalInt32());
604604
}
605605

606+
public function testInvalidVarintLength() {
607+
$this->expectException(Exception::class);
608+
609+
$m = new TestMessage();
610+
$m->mergeFromString(hex2bin("0afaffffff0f"));
611+
}
612+
613+
private function makeRecursiveMessage($depth) {
614+
$m = new TestMessage();
615+
$m->setOptionalInt32(1);
616+
if ($depth == 0) {
617+
return $m;
618+
}
619+
$m->setRecursive($this->makeRecursiveMessage($depth - 1));
620+
return $m;
621+
}
622+
623+
public function testRecursiveMessage() {
624+
$payload = $this->makeRecursiveMessage(99)->serializeToString();
625+
626+
$m = new TestMessage();
627+
$m->mergeFromString($payload);
628+
}
629+
630+
public function testOverlyRecursiveMessage() {
631+
$this->expectException(Exception::class);
632+
$payload = $this->makeRecursiveMessage(101)->serializeToString();
633+
634+
$m = new TestMessage();
635+
$m->mergeFromString($payload);
636+
}
637+
606638
public function testRandomFieldOrder()
607639
{
608640
$m = new TestRandomFieldOrder();

0 commit comments

Comments
 (0)