Code Review
/
apps
/
agl-service-can-low-level.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
Rename poorly named identifier to avoid name conflicts.
[apps/agl-service-can-low-level.git]
/
pb_decode.c
diff --git
a/pb_decode.c
b/pb_decode.c
index
40da1aa
..
5d21102
100644
(file)
--- a/
pb_decode.c
+++ b/
pb_decode.c
@@
-352,7
+352,7
@@
static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t
{
/* Packed array */
bool status = true;
{
/* Packed array */
bool status = true;
-
size_t *size = (
size_t*)iter->pSize;
+
pb_size_t *size = (pb_
size_t*)iter->pSize;
pb_istream_t substream;
if (!pb_make_string_substream(stream, &substream))
return false;
pb_istream_t substream;
if (!pb_make_string_substream(stream, &substream))
return false;
@@
-377,7
+377,7
@@
static bool checkreturn decode_static_field(pb_istream_t *stream, pb_wire_type_t
else
{
/* Repeated field */
else
{
/* Repeated field */
-
size_t *size = (
size_t*)iter->pSize;
+
pb_size_t *size = (pb_
size_t*)iter->pSize;
void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
if (*size >= iter->pos->array_size)
PB_RETURN_ERROR(stream, "array overflow");
void *pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
if (*size >= iter->pos->array_size)
PB_RETURN_ERROR(stream, "array overflow");
@@
-444,8
+444,8
@@
static void initialize_pointer_field(void *pItem, pb_field_iter_t *iter)
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
{
#ifndef PB_ENABLE_MALLOC
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_t wire_type, pb_field_iter_t *iter)
{
#ifndef PB_ENABLE_MALLOC
- UNUSED(wire_type);
- UNUSED(iter);
+
PB_
UNUSED(wire_type);
+
PB_
UNUSED(iter);
PB_RETURN_ERROR(stream, "no malloc support");
#else
pb_type_t type;
PB_RETURN_ERROR(stream, "no malloc support");
#else
pb_type_t type;
@@
-478,7
+478,7
@@
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
{
/* Packed array, multiple items come in at once. */
bool status = true;
{
/* Packed array, multiple items come in at once. */
bool status = true;
-
size_t *size = (
size_t*)iter->pSize;
+
pb_size_t *size = (pb_
size_t*)iter->pSize;
size_t allocated_size = *size;
void *pItem;
pb_istream_t substream;
size_t allocated_size = *size;
void *pItem;
pb_istream_t substream;
@@
-488,7
+488,7
@@
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
while (substream.bytes_left)
{
while (substream.bytes_left)
{
- if (*size + 1 > allocated_size)
+ if (
(size_t)
*size + 1 > allocated_size)
{
/* Allocate more storage. This tries to guess the
* number of remaining entries. Round the division
{
/* Allocate more storage. This tries to guess the
* number of remaining entries. Round the division
@@
-510,6
+510,16
@@
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
status = false;
break;
}
status = false;
break;
}
+
+ if (*size == PB_SIZE_MAX)
+ {
+#ifndef PB_NO_ERRMSG
+ stream->errmsg = "too many array entries";
+#endif
+ status = false;
+ break;
+ }
+
(*size)++;
}
pb_close_string_substream(stream, &substream);
(*size)++;
}
pb_close_string_substream(stream, &substream);
@@
-519,9
+529,12
@@
static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
else
{
/* Normal repeated field, i.e. only one item at a time. */
else
{
/* Normal repeated field, i.e. only one item at a time. */
-
size_t *size = (
size_t*)iter->pSize;
+
pb_size_t *size = (pb_
size_t*)iter->pSize;
void *pItem;
void *pItem;
+ if (*size == PB_SIZE_MAX)
+ PB_RETURN_ERROR(stream, "too many array entries");
+
(*size)++;
if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
return false;
(*size)++;
if (!allocate_field(stream, iter->pData, iter->pos->data_size, *size))
return false;
@@
-616,7
+629,7
@@
static bool checkreturn default_extension_decoder(pb_istream_t *stream,
/* Fake a field iterator for the extension field.
* It is not actually safe to advance this iterator, but decode_field
* will not even try to. */
/* Fake a field iterator for the extension field.
* It is not actually safe to advance this iterator, but decode_field
* will not even try to. */
- pb_field_iter_begin(&iter, field, extension->dest);
+
(void)
pb_field_iter_begin(&iter, field, extension->dest);
iter.pData = extension->dest;
iter.pSize = &extension->found;
iter.pData = extension->dest;
iter.pSize = &extension->found;
@@
-668,16
+681,14
@@
static bool checkreturn find_extension_field(pb_field_iter_t *iter)
static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
{
pb_field_iter_t iter;
static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_struct)
{
pb_field_iter_t iter;
- pb_field_iter_begin(&iter, fields, dest_struct);
+
+ if (!pb_field_iter_begin(&iter, fields, dest_struct))
+ return; /* Empty message type */
do
{
pb_type_t type;
type = iter.pos->type;
do
{
pb_type_t type;
type = iter.pos->type;
-
- /* Avoid crash on empty message types (zero fields) */
- if (iter.pos->tag == 0)
- continue;
if (PB_ATYPE(type) == PB_ATYPE_STATIC)
{
if (PB_ATYPE(type) == PB_ATYPE_STATIC)
{
@@
-690,7
+701,7
@@
static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str
else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
/* Set array count to 0, no need to initialize contents. */
else if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
/* Set array count to 0, no need to initialize contents. */
- *(size_t*)iter.pSize = 0;
+ *(
pb_
size_t*)iter.pSize = 0;
continue;
}
continue;
}
@@
-718,7
+729,7
@@
static void pb_message_set_to_defaults(const pb_field_t fields[], void *dest_str
/* Initialize array count to 0. */
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
/* Initialize array count to 0. */
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
- *(size_t*)iter.pSize = 0;
+ *(
pb_
size_t*)iter.pSize = 0;
}
}
else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
}
}
else if (PB_ATYPE(type) == PB_ATYPE_CALLBACK)
@@
-738,7
+749,9
@@
bool checkreturn pb_decode_noinit(pb_istream_t *stream, const pb_field_t fields[
uint32_t extension_range_start = 0;
pb_field_iter_t iter;
uint32_t extension_range_start = 0;
pb_field_iter_t iter;
- pb_field_iter_begin(&iter, fields, dest_struct);
+ /* Return value ignored, as empty message types will be correctly handled by
+ * pb_field_iter_find() anyway. */
+ (void)pb_field_iter_begin(&iter, fields, dest_struct);
while (stream->bytes_left)
{
while (stream->bytes_left)
{
@@
-859,17
+872,15
@@
bool pb_decode_delimited(pb_istream_t *stream, const pb_field_t fields[], void *
void pb_release(const pb_field_t fields[], void *dest_struct)
{
pb_field_iter_t iter;
void pb_release(const pb_field_t fields[], void *dest_struct)
{
pb_field_iter_t iter;
- pb_field_iter_begin(&iter, fields, dest_struct);
+
+ if (!pb_field_iter_begin(&iter, fields, dest_struct))
+ return; /* Empty message type */
do
{
pb_type_t type;
type = iter.pos->type;
do
{
pb_type_t type;
type = iter.pos->type;
- /* Avoid crash on empty message types (zero fields) */
- if (iter.pos->tag == 0)
- continue;
-
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
{
if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
if (PB_ATYPE(type) == PB_ATYPE_POINTER)
{
if (PB_HTYPE(type) == PB_HTYPE_REPEATED &&
@@
-878,7
+889,7
@@
void pb_release(const pb_field_t fields[], void *dest_struct)
{
/* Release entries in repeated string or bytes array */
void **pItem = *(void***)iter.pData;
{
/* Release entries in repeated string or bytes array */
void **pItem = *(void***)iter.pData;
-
size_t count = *(
size_t*)iter.pSize;
+
pb_size_t count = *(pb_
size_t*)iter.pSize;
while (count--)
{
pb_free(*pItem);
while (count--)
{
pb_free(*pItem);
@@
-889,11
+900,11
@@
void pb_release(const pb_field_t fields[], void *dest_struct)
{
/* Release fields in submessages */
void *pItem = *(void**)iter.pData;
{
/* Release fields in submessages */
void *pItem = *(void**)iter.pData;
- size_t count = (pItem ? 1 : 0);
+
pb_
size_t count = (pItem ? 1 : 0);
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
if (PB_HTYPE(type) == PB_HTYPE_REPEATED)
{
- count = *(size_t*)iter.pSize;
+ count = *(
pb_
size_t*)iter.pSize;
}
while (count--)
}
while (count--)
@@
-1021,13
+1032,13
@@
static bool checkreturn pb_dec_svarint(pb_istream_t *stream, const pb_field_t *f
static 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);
+
PB_
UNUSED(field);
return pb_decode_fixed32(stream, dest);
}
static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
return pb_decode_fixed32(stream, dest);
}
static bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
{
- UNUSED(field);
+
PB_
UNUSED(field);
return pb_decode_fixed64(stream, dest);
}
return pb_decode_fixed64(stream, dest);
}
@@
-1056,7
+1067,12
@@
static bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *fie
bdest = (pb_bytes_array_t*)dest;
}
bdest = (pb_bytes_array_t*)dest;
}
- bdest->size = size;
+ if (size > PB_SIZE_MAX)
+ {
+ PB_RETURN_ERROR(stream, "bytes overflow");
+ }
+
+ bdest->size = (pb_size_t)size;
return pb_read(stream, bdest->bytes, size);
}
return pb_read(stream, bdest->bytes, size);
}