aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBo Yang <teboring@google.com>2017-01-10 11:00:03 -0800
committerBo Yang <teboring@google.com>2017-01-10 11:00:23 -0800
commit60d4f8af483e0234ee5b7f7544bcd578a34412ad (patch)
tree0e1ff689e6f425d69b3af689cf3e5b4458b6c75e
parente259b515a5baae2d7d7623af62ea87079a5e91a0 (diff)
downloadprotobuf-60d4f8af483e0234ee5b7f7544bcd578a34412ad.tar.gz
protobuf-60d4f8af483e0234ee5b7f7544bcd578a34412ad.tar.bz2
protobuf-60d4f8af483e0234ee5b7f7544bcd578a34412ad.zip
Decoding unknown field should succeed.
-rw-r--r--php/src/Google/Protobuf/Internal/Message.php5
-rw-r--r--php/src/Google/Protobuf/descriptor.php4
-rw-r--r--php/tests/encode_decode_test.php6
-rw-r--r--php/tests/proto/test.proto3
4 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 7d1e51e8..f6be8500 100644
--- a/php/src/Google/Protobuf/Internal/Message.php
+++ b/php/src/Google/Protobuf/Internal/Message.php
@@ -400,6 +400,11 @@ class Message
$number = GPBWire::getTagFieldNumber($tag);
$field = $this->desc->getFieldByNumber($number);
+ // Check whether we retrieved a known field
+ if ($field === NULL) {
+ continue;
+ }
+
if (!$this->parseFieldFromStream($tag, $input, $field)) {
return false;
}
diff --git a/php/src/Google/Protobuf/descriptor.php b/php/src/Google/Protobuf/descriptor.php
index ef6b9dcf..9e56ef27 100644
--- a/php/src/Google/Protobuf/descriptor.php
+++ b/php/src/Google/Protobuf/descriptor.php
@@ -155,7 +155,11 @@ class Descriptor
public function getFieldByNumber($number)
{
+ if (!isset($this->field[$number])) {
+ return NULL;
+ } else {
return $this->field[$number];
+ }
}
public function setClass($klass)
diff --git a/php/tests/encode_decode_test.php b/php/tests/encode_decode_test.php
index 7bb75336..992f1631 100644
--- a/php/tests/encode_decode_test.php
+++ b/php/tests/encode_decode_test.php
@@ -167,4 +167,10 @@ class EncodeDecodeTest extends TestBase
$this->assertEquals($original, $msg->getOptionalInt64());
}
}
+
+ public function testDecodeFieldNonExist() {
+ $data = hex2bin('c80501');
+ $m = new TestMessage();
+ $m->decode($data);
+ }
}
diff --git a/php/tests/proto/test.proto b/php/tests/proto/test.proto
index fc283a05..594aee4d 100644
--- a/php/tests/proto/test.proto
+++ b/php/tests/proto/test.proto
@@ -92,7 +92,8 @@ message TestMessage {
int32 a = 1;
}
- // NestedMessage nested_message = 90;
+ // Reserved for non-existing field test.
+ // int32 non_exist = 89;
}
enum TestEnum {