Fix a few places that needed adjustment for the size_t changes, including updating the range checks to use a calculated SIZE_T_MAX.

This commit is contained in:
Eric Haszlakiewicz
2016-05-23 02:10:58 +00:00
parent 9a2915ce66
commit 996be85843
8 changed files with 142 additions and 15 deletions

View File

@@ -22,6 +22,14 @@
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#if SIZEOF_SIZE_T == SIZEOF_INT
#define SIZE_T_MAX UINT_MAX
#elif SIZEOF_SIZE_T == SIZEOF_LONG
#define SIZE_T_MAX ULONG_MAX
#else
#error Unable to determine size of size_t
#endif
#include "arraylist.h"
struct array_list*
@@ -64,8 +72,8 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
size_t new_size;
if(max < arr->size) return 0;
/* Avoid undefined behaviour on int32 overflow */
if( arr->size >= INT_MAX / 2 )
/* Avoid undefined behaviour on size_t overflow */
if( arr->size >= SIZE_T_MAX / 2 )
new_size = max;
else
{
@@ -73,8 +81,8 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
if (new_size < max)
new_size = max;
}
if((size_t)new_size > (~((size_t)0)) / sizeof(void*)) return -1;
if(!(t = realloc(arr->array, ((size_t)new_size)*sizeof(void*)))) return -1;
if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
arr->array = (void**)t;
(void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
arr->size = new_size;
@@ -84,7 +92,7 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
int
array_list_put_idx(struct array_list *arr, size_t idx, void *data)
{
if( idx < 0 || idx > INT_MAX - 1 ) return -1;
if (idx > SIZE_T_MAX - 1 ) return -1;
if(array_list_expand_internal(arr, idx+1)) return -1;
if(arr->array[idx]) arr->free_fn(arr->array[idx]);
arr->array[idx] = data;
@@ -118,9 +126,9 @@ array_list_length(struct array_list *arr)
}
int
array_list_del_idx( struct array_list *arr, int idx, int count )
array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
{
int i, stop;
size_t i, stop;
stop = idx + count;
if ( idx >= arr->length || stop > arr->length ) return -1;