Add a integer overflow check when resizing arrays

This commit is contained in:
Pierre Pronchery 2015-06-23 03:00:13 +02:00
parent b1246abd04
commit 8cd73f4219

View File

@ -104,6 +104,7 @@ int array_set(Array * array, size_t pos, void * value)
uint32_t p = pos + 1;
uint64_t offset;
uint64_t curpos;
size_t size;
void * q;
/* check for overflows */
@ -113,6 +114,9 @@ int array_set(Array * array, size_t pos, void * value)
if(array->count < p)
{
/* grow the array */
size = offset + array->size;
if(size != offset + array->size)
return -error_set_code(-ERANGE, "%s", strerror(ERANGE));
if((q = realloc(array->value, offset + array->size)) == NULL)
return -error_set_code(-errno, "%s", strerror(errno));
array->value = q;