[php] Added a unit test that bad UTF-8 is rejected in the parser.

PiperOrigin-RevId: 632274113
pull/16811/head
Joshua Haberman 2024-05-09 14:39:58 -07:00 committed by Copybara-Service
parent b51dc1b438
commit 5a91d6fe5e
2 changed files with 17 additions and 1 deletions

View File

@ -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.");

View File

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