Merge branch 'master' of https://code.google.com/p/nanopb
[apps/low-level-can-service.git] / pb_decode.c
index 3992ab8..fd23488 100644 (file)
@@ -75,7 +75,7 @@ static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
 {
     uint64_t temp;
     bool status = pb_decode_varint(stream, &temp);
-    *dest = temp;
+    *dest = (uint32_t)temp;
     return status;
 }
 
@@ -448,6 +448,7 @@ static void endian_copy(void *dest, void *src, size_t destsize, size_t srcsize)
 #ifdef __BIG_ENDIAN__
     memcpy(dest, (char*)src + (srcsize - destsize), destsize);
 #else
+    UNUSED(srcsize);
     memcpy(dest, src, destsize);
 #endif
 }
@@ -480,6 +481,7 @@ bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, v
     }
     return status;
 #else
+    UNUSED(field);
     return pb_read(stream, (uint8_t*)dest, 4);
 #endif
 }
@@ -496,6 +498,7 @@ bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, v
     }
     return status;
 #else
+    UNUSED(field);
     return pb_read(stream, (uint8_t*)dest, 8);
 #endif
 }
@@ -509,7 +512,8 @@ bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, voi
         return false;
     x->size = temp;
     
-    if (x->size > field->data_size)
+    /* Check length, noting the space taken by the size_t header. */
+    if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
         return false;
     
     return pb_read(stream, x->bytes, x->size);
@@ -522,7 +526,8 @@ bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, vo
     if (!pb_decode_varint32(stream, &size))
         return false;
     
-    if (size > field->data_size - 1)
+    /* Check length, noting the null terminator */
+    if (size + 1 > field->data_size)
         return false;
     
     status = pb_read(stream, (uint8_t*)dest, size);