summaryrefslogtreecommitdiff
path: root/arraylist.c
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2016-05-23 02:10:58 +0000
committerEric Haszlakiewicz <erh+git@nimenees.com>2016-05-23 02:10:58 +0000
commit996be858439e8207dadc76e0b45344edfd787861 (patch)
treef2ba11b261e6314f78d611d9455fa48e967165b0 /arraylist.c
parent9a2915ce6603bfcd3de99f3111fe56cc2b4f02bd (diff)
downloadjson-c-996be858439e8207dadc76e0b45344edfd787861.tar.gz
Fix a few places that needed adjustment for the size_t changes, including updating the range checks to use a calculated SIZE_T_MAX.
Diffstat (limited to 'arraylist.c')
-rw-r--r--arraylist.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/arraylist.c b/arraylist.c
index a02266e..0d01069 100644
--- a/arraylist.c
+++ b/arraylist.c
@@ -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;