Преглед на файлове

Allow bytes field to be longer than 16000 bytes (#5924)

* Allow bytes field to be longer than 16000 bytes

* Remove empty line
Paul Yang преди 6 години
родител
ревизия
ba42cb53d9
променени са 3 файла, в които са добавени 31 реда и са изтрити 6 реда
  1. 7 3
      php/ext/google/protobuf/upb.c
  2. 17 0
      php/tests/encode_decode_test.php
  3. 7 3
      ruby/ext/google/protobuf_c/upb.c

+ 7 - 3
php/ext/google/protobuf/upb.c

@@ -16533,9 +16533,14 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str,
   UPB_UNUSED(handler_data);
   UPB_UNUSED(handler_data);
   UPB_UNUSED(handle);
   UPB_UNUSED(handle);
 
 
+  print_data(p, "\"", 1);
+
   while (remaining > 2) {
   while (remaining > 2) {
-    /* TODO(haberman): handle encoded lengths > sizeof(data) */
-    UPB_ASSERT((limit - to) >= 4);
+    if (limit - to < 4) {
+      bytes = to - data;
+      putstring(p, data, bytes);
+      to = data;
+    }
 
 
     to[0] = base64[from[0] >> 2];
     to[0] = base64[from[0] >> 2];
     to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
     to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
@@ -16567,7 +16572,6 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str,
   }
   }
 
 
   bytes = to - data;
   bytes = to - data;
-  print_data(p, "\"", 1);
   putstring(p, data, bytes);
   putstring(p, data, bytes);
   print_data(p, "\"", 1);
   print_data(p, "\"", 1);
   return len;
   return len;

+ 17 - 0
php/tests/encode_decode_test.php

@@ -167,6 +167,23 @@ class EncodeDecodeTest extends TestBase
         $this->assertSame("\"YQ==\"", $m->serializeToJsonString());
         $this->assertSame("\"YQ==\"", $m->serializeToJsonString());
     }
     }
 
 
+    public function generateRandomString($length = 10) {
+        $randomString = str_repeat("+", $length);
+        for ($i = 0; $i < $length; $i++) {
+            $randomString[$i] = rand(0, 255);
+        }
+        return $randomString;
+    }
+
+    public function testEncodeTopLevelLongBytesValue()
+    {
+        $m = new BytesValue();
+        $data = $this->generateRandomString(12007);
+        $m->setValue($data);
+        $expected = "\"" . base64_encode($data) . "\"";
+        $this->assertSame(strlen($expected), strlen($m->serializeToJsonString()));
+    }
+
     public function testEncode()
     public function testEncode()
     {
     {
         $from = new TestMessage();
         $from = new TestMessage();

+ 7 - 3
ruby/ext/google/protobuf_c/upb.c

@@ -16533,9 +16533,14 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str,
   UPB_UNUSED(handler_data);
   UPB_UNUSED(handler_data);
   UPB_UNUSED(handle);
   UPB_UNUSED(handle);
 
 
+  print_data(p, "\"", 1);
+
   while (remaining > 2) {
   while (remaining > 2) {
-    /* TODO(haberman): handle encoded lengths > sizeof(data) */
-    UPB_ASSERT((limit - to) >= 4);
+    if (limit - to < 4) {
+      bytes = to - data;
+      putstring(p, data, bytes);
+      to = data;
+    }
 
 
     to[0] = base64[from[0] >> 2];
     to[0] = base64[from[0] >> 2];
     to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
     to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
@@ -16567,7 +16572,6 @@ static size_t putbytes(void *closure, const void *handler_data, const char *str,
   }
   }
 
 
   bytes = to - data;
   bytes = to - data;
-  print_data(p, "\"", 1);
   putstring(p, data, bytes);
   putstring(p, data, bytes);
   print_data(p, "\"", 1);
   print_data(p, "\"", 1);
   return len;
   return len;