From c1993846586124ef2219fbaba598ba4c0c704d7e Mon Sep 17 00:00:00 2001 From: Tom Arbesser-Rastburg Date: Tue, 21 Oct 2025 11:26:31 +1100 Subject: [PATCH] fix(php): Correct uint32 type mapping to use UInt32 instead of Int32 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes incorrect type mapping in the PHP extension where uint32 fields were handled as signed int32. This prevented valid unsigned values above 2^31 - 1 (e.g. 2147483648–4294967295) from being represented correctly. This updates the mapping to UInt32. --- php/ext/google/protobuf/convert.c | 2 +- php/tests/GeneratedClassTest.php | 37 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/php/ext/google/protobuf/convert.c b/php/ext/google/protobuf/convert.c index a0b5d23faa9d6..87c1a61385f4e 100644 --- a/php/ext/google/protobuf/convert.c +++ b/php/ext/google/protobuf/convert.c @@ -148,7 +148,7 @@ upb_CType pbphp_dtype_to_type(upb_FieldType type) { CASE(Enum, Enum); CASE(Int32, Int32); CASE(Int64, Int64); - CASE(UInt32, Int32); + CASE(UInt32, UInt32); CASE(UInt64, UInt64); CASE(SInt32, Int32); CASE(SInt64, Int64); diff --git a/php/tests/GeneratedClassTest.php b/php/tests/GeneratedClassTest.php index 787f76f4bb1ab..b24e8dec48e17 100644 --- a/php/tests/GeneratedClassTest.php +++ b/php/tests/GeneratedClassTest.php @@ -269,6 +269,43 @@ public function testUint32Field() $this->assertSame(MIN_INT32, $m->getOptionalUint32()); } + public function testUint32ArrayConstructor() + { + $m1 = new TestMessage(['optional_uint32' => 2147483648]); + $this->assertSame(MIN_INT32, $m1->getOptionalUint32()); + + $m2 = new TestMessage(['optional_uint32' => MAX_UINT32]); + $this->assertSame(-1, $m2->getOptionalUint32()); + } + + public function testRepeatedUint32Boundaries() + { + $m = new TestMessage(); + $rf = $m->getRepeatedUint32(); + + $rf[] = 0; + $rf[] = MAX_INT32; + $rf[] = 2147483648; + $rf[] = MAX_UINT32; + + $this->assertSame(0, $rf[0]); + $this->assertSame(MAX_INT32, $rf[1]); + $this->assertSame(MIN_INT32, $rf[2]); + $this->assertSame(-1, $rf[3]); + } + + public function testMapUint32Uint32Boundaries() + { + $m = new TestMessage(); + $map = $m->getMapUint32Uint32(); + + $map[1] = 0; + $map[MAX_UINT32] = MAX_UINT32; + + $this->assertSame(0, $map[1]); + $this->assertSame(-1, $map[MAX_UINT32]); + } + ######################################################### # Test int64 field. #########################################################