Check array max size when encoding.
authorPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 20 Oct 2013 18:42:00 +0000 (21:42 +0300)
committerPetteri Aimonen <jpa@git.mail.kapsi.fi>
Sun, 20 Oct 2013 18:42:00 +0000 (21:42 +0300)
Update issue 90
Status: FixedInGit

pb_encode.c
tests/encode_unittests/encode_unittests.c

index d6ba7e3..4aced3c 100644 (file)
@@ -94,6 +94,9 @@ static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
     
     if (count == 0)
         return true;
+        
+    if (count > field->array_size)
+        PB_RETURN_ERROR(stream, "array max size exceeded");
     
     /* We always pack arrays if the datatype allows it. */
     if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
index 32a37bf..14bc62e 100644 (file)
@@ -223,6 +223,20 @@ int main()
         TEST(!pb_encode(&s, FloatArray_fields, &msg))
     }
     
+    {
+        uint8_t buffer[50];
+        pb_ostream_t s;
+        FloatArray msg = {1, {99.0f}};
+        
+        COMMENT("Test array size limit in pb_encode")
+        
+        s = pb_ostream_from_buffer(buffer, sizeof(buffer));
+        TEST((msg.data_count = 10) && pb_encode(&s, FloatArray_fields, &msg))
+        
+        s = pb_ostream_from_buffer(buffer, sizeof(buffer));
+        TEST((msg.data_count = 11) && !pb_encode(&s, FloatArray_fields, &msg))
+    }
+    
     {
         uint8_t buffer[10];
         pb_ostream_t s;