Bugfixes for dynamic allocation

This commit is contained in:
Petteri Aimonen
2014-02-25 19:58:11 +02:00
parent 011a30af9c
commit 48ac461372

View File

@@ -359,6 +359,10 @@ static bool pb_field_next(pb_field_iterator_t *iter)
{ {
prev_size *= iter->pos->array_size; prev_size *= iter->pos->array_size;
} }
else if (PB_ATYPE(iter->pos->type) == PB_ATYPE_POINTER)
{
prev_size = sizeof(void*);
}
if (iter->pos->tag == 0) if (iter->pos->tag == 0)
return false; /* Only happens with empty message types */ return false; /* Only happens with empty message types */
@@ -512,9 +516,17 @@ static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
{ {
case PB_HTYPE_REQUIRED: case PB_HTYPE_REQUIRED:
case PB_HTYPE_OPTIONAL: case PB_HTYPE_OPTIONAL:
if (PB_LTYPE(type) == PB_LTYPE_STRING)
{
return func(stream, iter->pos, iter->pData);
}
else
{
if (!allocate_field(stream, iter, 1)) if (!allocate_field(stream, iter, 1))
return false; return false;
return func(stream, iter->pos, iter->pData);
return func(stream, iter->pos, *(void**)iter->pData);
}
case PB_HTYPE_REPEATED: case PB_HTYPE_REPEATED:
if (wire_type == PB_WT_STRING if (wire_type == PB_WT_STRING
@@ -545,6 +557,7 @@ static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
status = false; status = false;
break; break;
} }
}
/* Decode the array entry */ /* Decode the array entry */
pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size); pItem = (uint8_t*)iter->pData + iter->pos->data_size * (*size);
@@ -555,7 +568,6 @@ static bool checkreturn decode_pointer_field(pb_istream_t *stream, pb_wire_type_
} }
(*size)++; (*size)++;
} }
}
pb_close_string_substream(stream, &substream); pb_close_string_substream(stream, &substream);
return status; return status;
@@ -1036,8 +1048,22 @@ bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, vo
return false; return false;
/* Check length, noting the null terminator */ /* Check length, noting the null terminator */
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
#ifndef PB_ENABLE_MALLOC
PB_RETURN_ERROR(stream, "no malloc support");
#else
*(void**)dest = realloc(*(void**)dest, size + 1);
if (*(void**)dest == NULL)
PB_RETURN_ERROR(stream, "out of memory");
dest = *(void**)dest;
#endif
}
else
{
if (size + 1 > field->data_size) if (size + 1 > field->data_size)
PB_RETURN_ERROR(stream, "string overflow"); PB_RETURN_ERROR(stream, "string overflow");
}
status = pb_read(stream, (uint8_t*)dest, size); status = pb_read(stream, (uint8_t*)dest, size);
*((uint8_t*)dest + size) = 0; *((uint8_t*)dest + size) = 0;