#define checkreturn __attribute__((warn_unused_result))
#endif
-#define NANOPB_INTERNALS
#include "pb.h"
#include "pb_decode.h"
* This is an optimization for the varint decoding. */
static bool checkreturn pb_readbyte(pb_istream_t *stream, uint8_t *buf)
{
- if (!stream->bytes_left)
+ if (stream->bytes_left == 0)
PB_RETURN_ERROR(stream, "end-of-stream");
#ifndef PB_BUFFER_ONLY
if (!pb_readbyte(stream, &byte))
return false;
- if (!(byte & 0x80))
+ if ((byte & 0x80) == 0)
{
/* Quick case, 1 byte value */
result = byte;
{
return true;
}
- pb_field_next(iter);
+ (void)pb_field_next(iter);
} while (iter->field_index != start);
return false;
if (!pb_make_string_substream(stream, &substream))
return false;
- while (substream.bytes_left && *size < iter->pos->array_size)
+ while (substream.bytes_left > 0 && *size < iter->pos->array_size)
{
void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
if (!func(&substream, iter->pos, pItem))
#ifdef PB_ENABLE_MALLOC
/* Allocate storage for the field and store the pointer at iter->pData.
- * array_size is the number of entries to reserve in an array. */
+ * array_size is the number of entries to reserve in an array.
+ * Zero size is not allowed, use pb_free() for releasing.
+ */
static bool checkreturn allocate_field(pb_istream_t *stream, void *pData, size_t data_size, size_t array_size)
{
void *ptr = *(void**)pData;
- size_t size = array_size * data_size;
+
+ /* Check for multiplication overflows.
+ * This code avoids the costly division if the sizes are small enough.
+ * Multiplication is safe as long as only half of bits are set
+ * in either multiplicand.
+ */
+ const size_t check_limit = (size_t)1 << (sizeof(size_t) * 4);
+ if (data_size >= check_limit || array_size >= check_limit)
+ {
+ const size_t size_max = (size_t)-1;
+ if (size_max / array_size < data_size)
+ {
+ PB_RETURN_ERROR(stream, "size too large");
+ }
+ }
/* Allocate new or expand previous allocation */
/* Note: on failure the old pointer will remain in the structure,
* the message must be freed by caller also on error return. */
- ptr = realloc(ptr, size);
+ ptr = pb_realloc(ptr, array_size * data_size);
if (ptr == NULL)
PB_RETURN_ERROR(stream, "realloc failed");
/* Clear a newly allocated item in case it contains a pointer, or is a submessage. */
static void initialize_pointer_field(void *pItem, pb_field_iterator_t *iter)
{
- if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING)
- {
- *(char**)pItem = NULL;
- }
- else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
+ if (PB_LTYPE(iter->pos->type) == PB_LTYPE_STRING ||
+ PB_LTYPE(iter->pos->type) == PB_LTYPE_BYTES)
{
- memset(pItem, 0, iter->pos->data_size);
+ *(void**)pItem = NULL;
}
else if (PB_LTYPE(iter->pos->type) == PB_LTYPE_SUBMESSAGE)
{
{
case PB_HTYPE_REQUIRED:
case PB_HTYPE_OPTIONAL:
- if (PB_LTYPE(type) == PB_LTYPE_STRING)
+ if (PB_LTYPE(type) == PB_LTYPE_STRING ||
+ PB_LTYPE(type) == PB_LTYPE_BYTES)
{
return func(stream, iter->pos, iter->pData);
}
{
const pb_field_t *field = (const pb_field_t*)extension->type->arg;
pb_field_iterator_t iter;
- bool dummy;
if (field->tag != tag)
return true;
iter.required_field_index = 0;
iter.dest_struct = extension->dest;
iter.pData = extension->dest;
- iter.pSize = &dummy;
+ iter.pSize = &extension->found;
return decode_field(stream, wire_type, &iter);
}
pb_extension_t *extension = *(pb_extension_t* const *)iter->pData;
size_t pos = stream->bytes_left;
- while (extension && pos == stream->bytes_left)
+ while (extension != NULL && pos == stream->bytes_left)
{
bool status;
if (extension->type->decode)
do {
if (PB_LTYPE(iter->pos->type) == PB_LTYPE_EXTENSION)
return true;
- pb_field_next(iter);
+ (void)pb_field_next(iter);
} while (iter->field_index != start);
return false;
bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
{
- uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0}; /* Used to check for required fields */
+ uint8_t fields_seen[(PB_MAX_REQUIRED_FIELDS + 7) / 8] = {0, 0, 0, 0, 0, 0, 0, 0};
uint32_t extension_range_start = 0;
pb_field_iterator_t iter;
} while (pb_field_next(&iter));
/* Fixup if last field was also required. */
- if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag)
+ if (PB_HTYPE(last_type) == PB_HTYPE_REQUIRED && iter.pos->tag != 0)
req_field_count++;
/* Check the whole bytes */
bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
{
+ bool status;
pb_message_set_to_defaults(fields, dest_struct);
- return pb_decode_noinit(stream, fields, dest_struct);
+ status = pb_decode_noinit(stream, fields, dest_struct);
+
+#ifdef PB_ENABLE_MALLOC
+ if (!status)
+ pb_release(fields, dest_struct);
+#endif
+
+ return status;
}
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
{
- if (PB_LTYPE(type) == PB_LTYPE_STRING &&
- PB_HTYPE(type) == PB_HTYPE_REPEATED)
+ if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
+ (PB_LTYPE(type) == PB_LTYPE_STRING ||
+ PB_LTYPE(type) == PB_LTYPE_BYTES))
{
- /* Release entries in repeated string array */
+ /* Release entries in repeated string or bytes array */
void **pItem = *(void***)iter.pData;
size_t count = *(size_t*)iter.pSize;
while (count--)
{
- free(*pItem);
+ pb_free(*pItem);
*pItem++ = NULL;
}
}
- else if (PB_LTYPE(type) == PB_LTYPE_BYTES)
- {
- /* Release entries in repeated bytes array */
- pb_bytes_ptr_t *pItem = *(pb_bytes_ptr_t**)iter.pData;
- size_t count = (pItem ? 1 : 0);
-
- if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
- {
- count = *(size_t*)iter.pSize;
- }
-
- while (count--)
- {
- free(pItem->bytes);
- pItem->bytes = NULL;
- pItem++;
- }
- }
else if (PB_LTYPE(type) == PB_LTYPE_SUBMESSAGE)
{
/* Release fields in submessages */
}
/* Release main item */
- free(*(void**)iter.pData);
+ pb_free(*(void**)iter.pData);
*(void**)iter.pData = NULL;
}
} while (pb_field_next(&iter));
#endif
}
-bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint64_t value;
if (!pb_decode_varint(stream, &value))
return true;
}
-bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_uvarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint64_t value;
if (!pb_decode_varint(stream, &value))
return true;
}
-bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
int64_t value;
if (!pb_decode_svarint(stream, &value))
return true;
}
-bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
UNUSED(field);
return pb_decode_fixed32(stream, dest);
}
-bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
UNUSED(field);
return pb_decode_fixed64(stream, dest);
}
-bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint32_t size;
- size_t alloc_size;
+ pb_bytes_array_t *bdest;
if (!pb_decode_varint32(stream, &size))
return false;
- /* Space for the size_t header */
- alloc_size = size + offsetof(pb_bytes_array_t, bytes);
-
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
#ifndef PB_ENABLE_MALLOC
PB_RETURN_ERROR(stream, "no malloc support");
#else
- pb_bytes_ptr_t *bdest = (pb_bytes_ptr_t*)dest;
- if (!allocate_field(stream, &bdest->bytes, alloc_size, 1))
+ if (!allocate_field(stream, dest, PB_BYTES_ARRAY_T_ALLOCSIZE(size), 1))
return false;
-
- bdest->size = size;
- return pb_read(stream, bdest->bytes, size);
+ bdest = *(pb_bytes_array_t**)dest;
#endif
}
else
{
- pb_bytes_array_t* bdest = (pb_bytes_array_t*)dest;
- if (alloc_size > field->data_size)
+ if (PB_BYTES_ARRAY_T_ALLOCSIZE(size) > field->data_size)
PB_RETURN_ERROR(stream, "bytes overflow");
- bdest->size = size;
- return pb_read(stream, bdest->bytes, size);
+ bdest = (pb_bytes_array_t*)dest;
}
+
+ bdest->size = size;
+ return pb_read(stream, bdest->bytes, size);
}
-bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
uint32_t size;
size_t alloc_size;
return status;
}
-bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
+static bool checkreturn pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
bool status;
pb_istream_t substream;