Przeglądaj źródła

Decoding unknown field should succeed.

Bo Yang 8 lat temu
rodzic
commit
60d4f8af48

+ 5 - 0
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;
             }

+ 4 - 0
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)

+ 6 - 0
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);
+    }
 }

+ 2 - 1
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 {