From 5a91d6fe5e3fbe667eed9b27ed4d8293efb0cf56 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 9 May 2024 14:39:58 -0700 Subject: [PATCH] [php] Added a unit test that bad UTF-8 is rejected in the parser. PiperOrigin-RevId: 632274113 --- php/src/Google/Protobuf/Internal/Message.php | 3 ++- php/tests/EncodeDecodeTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index 21d9c980e0..31e2f29d30 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -394,7 +394,8 @@ class Message } break; case GPBType::STRING: - // TODO: Add utf-8 check. + // We don't check UTF-8 here; that will be validated by the + // setter later. if (!GPBWire::readString($input, $value)) { throw new GPBDecodeException( "Unexpected EOF inside string field."); diff --git a/php/tests/EncodeDecodeTest.php b/php/tests/EncodeDecodeTest.php index 276528d26e..abf95cca00 100644 --- a/php/tests/EncodeDecodeTest.php +++ b/php/tests/EncodeDecodeTest.php @@ -683,6 +683,21 @@ class EncodeDecodeTest extends TestBase $m->mergeFromString(hex2bin('7201')); } + public function testDecodeInvalidStringDataBadUtf8() + { + $this->expectException(Exception::class); + + $m = new TestMessage(); + $m->mergeFromString(hex2bin('720180')); + } + + public function testDecodeValidStringData() + { + $m = new TestMessage(); + $m->mergeFromString(hex2bin('720161')); + $this->assertSame('a', $m->getOptionalString()); + } + public function testDecodeInvalidBytesLengthMiss() { $this->expectException(Exception::class);