aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Haberman <haberman@google.com>2024-05-09 14:39:58 -0700
committerCopybara-Service <copybara-worker@google.com>2024-05-09 14:42:05 -0700
commit5a91d6fe5e3fbe667eed9b27ed4d8293efb0cf56 (patch)
treec4527cba2f8d828f5eb96b48a9394944965e50a0
parentb51dc1b438485e6b147c6b874d1859f1e9ea7da3 (diff)
downloadprotobuf-5a91d6fe5e3fbe667eed9b27ed4d8293efb0cf56.tar.gz
[php] Added a unit test that bad UTF-8 is rejected in the parser.
PiperOrigin-RevId: 632274113
-rw-r--r--php/src/Google/Protobuf/Internal/Message.php3
-rw-r--r--php/tests/EncodeDecodeTest.php15
2 files changed, 17 insertions, 1 deletions
diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php
index 21d9c980e..31e2f29d3 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 276528d26..abf95cca0 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);