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);