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