Skip to content

Commit 996be85

Browse files
committed
Fix a few places that needed adjustment for the size_t changes, including updating the range checks to use a calculated SIZE_T_MAX.
1 parent 9a2915c commit 996be85

File tree

8 files changed

+142
-15
lines changed

8 files changed

+142
-15
lines changed

arraylist.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@
2222
# include <strings.h>
2323
#endif /* HAVE_STRINGS_H */
2424

25+
#if SIZEOF_SIZE_T == SIZEOF_INT
26+
#define SIZE_T_MAX UINT_MAX
27+
#elif SIZEOF_SIZE_T == SIZEOF_LONG
28+
#define SIZE_T_MAX ULONG_MAX
29+
#else
30+
#error Unable to determine size of size_t
31+
#endif
32+
2533
#include "arraylist.h"
2634

2735
struct array_list*
@@ -64,17 +72,17 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
6472
size_t new_size;
6573

6674
if(max < arr->size) return 0;
67-
/* Avoid undefined behaviour on int32 overflow */
68-
if( arr->size >= INT_MAX / 2 )
75+
/* Avoid undefined behaviour on size_t overflow */
76+
if( arr->size >= SIZE_T_MAX / 2 )
6977
new_size = max;
7078
else
7179
{
7280
new_size = arr->size << 1;
7381
if (new_size < max)
7482
new_size = max;
7583
}
76-
if((size_t)new_size > (~((size_t)0)) / sizeof(void*)) return -1;
77-
if(!(t = realloc(arr->array, ((size_t)new_size)*sizeof(void*)))) return -1;
84+
if (new_size > (~((size_t)0)) / sizeof(void*)) return -1;
85+
if (!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1;
7886
arr->array = (void**)t;
7987
(void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*));
8088
arr->size = new_size;
@@ -84,7 +92,7 @@ static int array_list_expand_internal(struct array_list *arr, size_t max)
8492
int
8593
array_list_put_idx(struct array_list *arr, size_t idx, void *data)
8694
{
87-
if( idx < 0 || idx > INT_MAX - 1 ) return -1;
95+
if (idx > SIZE_T_MAX - 1 ) return -1;
8896
if(array_list_expand_internal(arr, idx+1)) return -1;
8997
if(arr->array[idx]) arr->free_fn(arr->array[idx]);
9098
arr->array[idx] = data;
@@ -118,9 +126,9 @@ array_list_length(struct array_list *arr)
118126
}
119127

120128
int
121-
array_list_del_idx( struct array_list *arr, int idx, int count )
129+
array_list_del_idx( struct array_list *arr, size_t idx, size_t count )
122130
{
123-
int i, stop;
131+
size_t i, stop;
124132

125133
stop = idx + count;
126134
if ( idx >= arr->length || stop > arr->length ) return -1;

arraylist.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern void* array_list_bsearch(const void **key,
5454
int (*sort_fn)(const void *, const void *));
5555

5656
extern int
57-
array_list_del_idx(struct array_list *arr, int i, int count);
57+
array_list_del_idx(struct array_list *arr, size_t idx, size_t count);
5858

5959
#ifdef __cplusplus
6060
}

autoconf-archive/README.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ Autoconf Archive fetched from:
33

44
http://gnu.mirror.iweb.com/autoconf-archive/autoconf-archive-2015.09.25.tar.xz
55

6-
Grabbed the minimum files needed for the AX_APPEND_COMPILE_FLAGS macro.
6+
Grabbed the minimum files needed for the AX_APPEND_COMPILE_FLAGS and
7+
AX_COMPILE_CHECK_SIZEOF macros.
78

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# ===========================================================================
2+
# http://www.gnu.org/software/autoconf-archive/ax_compile_check_sizeof.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_COMPILE_CHECK_SIZEOF(TYPE [, HEADERS [, EXTRA_SIZES...]])
8+
#
9+
# DESCRIPTION
10+
#
11+
# This macro checks for the size of TYPE using compile checks, not run
12+
# checks. You can supply extra HEADERS to look into. the check will cycle
13+
# through 1 2 4 8 16 and any EXTRA_SIZES the user supplies. If a match is
14+
# found, it will #define SIZEOF_`TYPE' to that value. Otherwise it will
15+
# emit a configure time error indicating the size of the type could not be
16+
# determined.
17+
#
18+
# The trick is that C will not allow duplicate case labels. While this is
19+
# valid C code:
20+
#
21+
# switch (0) case 0: case 1:;
22+
#
23+
# The following is not:
24+
#
25+
# switch (0) case 0: case 0:;
26+
#
27+
# Thus, the AC_TRY_COMPILE will fail if the currently tried size does not
28+
# match.
29+
#
30+
# Here is an example skeleton configure.in script, demonstrating the
31+
# macro's usage:
32+
#
33+
# AC_PROG_CC
34+
# AC_CHECK_HEADERS(stddef.h unistd.h)
35+
# AC_TYPE_SIZE_T
36+
# AC_CHECK_TYPE(ssize_t, int)
37+
#
38+
# headers='#ifdef HAVE_STDDEF_H
39+
# #include <stddef.h>
40+
# #endif
41+
# #ifdef HAVE_UNISTD_H
42+
# #include <unistd.h>
43+
# #endif
44+
# '
45+
#
46+
# AX_COMPILE_CHECK_SIZEOF(char)
47+
# AX_COMPILE_CHECK_SIZEOF(short)
48+
# AX_COMPILE_CHECK_SIZEOF(int)
49+
# AX_COMPILE_CHECK_SIZEOF(long)
50+
# AX_COMPILE_CHECK_SIZEOF(unsigned char *)
51+
# AX_COMPILE_CHECK_SIZEOF(void *)
52+
# AX_COMPILE_CHECK_SIZEOF(size_t, $headers)
53+
# AX_COMPILE_CHECK_SIZEOF(ssize_t, $headers)
54+
# AX_COMPILE_CHECK_SIZEOF(ptrdiff_t, $headers)
55+
# AX_COMPILE_CHECK_SIZEOF(off_t, $headers)
56+
#
57+
# LICENSE
58+
#
59+
# Copyright (c) 2008 Kaveh Ghazi <[email protected]>
60+
#
61+
# This program is free software: you can redistribute it and/or modify it
62+
# under the terms of the GNU General Public License as published by the
63+
# Free Software Foundation, either version 3 of the License, or (at your
64+
# option) any later version.
65+
#
66+
# This program is distributed in the hope that it will be useful, but
67+
# WITHOUT ANY WARRANTY; without even the implied warranty of
68+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
69+
# Public License for more details.
70+
#
71+
# You should have received a copy of the GNU General Public License along
72+
# with this program. If not, see <http://www.gnu.org/licenses/>.
73+
#
74+
# As a special exception, the respective Autoconf Macro's copyright owner
75+
# gives unlimited permission to copy, distribute and modify the configure
76+
# scripts that are the output of Autoconf when processing the Macro. You
77+
# need not follow the terms of the GNU General Public License when using
78+
# or distributing such scripts, even though portions of the text of the
79+
# Macro appear in them. The GNU General Public License (GPL) does govern
80+
# all other use of the material that constitutes the Autoconf Macro.
81+
#
82+
# This special exception to the GPL applies to versions of the Autoconf
83+
# Macro released by the Autoconf Archive. When you make and distribute a
84+
# modified version of the Autoconf Macro, you may extend this special
85+
# exception to the GPL to apply to your modified version as well.
86+
87+
#serial 5
88+
89+
AU_ALIAS([AC_COMPILE_CHECK_SIZEOF], [AX_COMPILE_CHECK_SIZEOF])
90+
AC_DEFUN([AX_COMPILE_CHECK_SIZEOF],
91+
[changequote(<<, >>)dnl
92+
dnl The name to #define.
93+
define(<<AC_TYPE_NAME>>, translit(sizeof_$1, [a-z *], [A-Z_P]))dnl
94+
dnl The cache variable name.
95+
define(<<AC_CV_NAME>>, translit(ac_cv_sizeof_$1, [ *], [_p]))dnl
96+
changequote([, ])dnl
97+
AC_MSG_CHECKING(size of $1)
98+
AC_CACHE_VAL(AC_CV_NAME,
99+
[for ac_size in 4 8 1 2 16 $3 ; do # List sizes in rough order of prevalence.
100+
AC_TRY_COMPILE([#include "confdefs.h"
101+
#include <sys/types.h>
102+
$2
103+
], [switch (0) case 0: case (sizeof ($1) == $ac_size):;], AC_CV_NAME=$ac_size)
104+
if test x$AC_CV_NAME != x ; then break; fi
105+
done
106+
])
107+
if test x$AC_CV_NAME = x ; then
108+
AC_MSG_ERROR([cannot determine a size for $1])
109+
fi
110+
AC_MSG_RESULT($AC_CV_NAME)
111+
AC_DEFINE_UNQUOTED(AC_TYPE_NAME, $AC_CV_NAME, [The number of bytes in type $1])
112+
undefine([AC_TYPE_NAME])dnl
113+
undefine([AC_CV_NAME])dnl
114+
])

configure.ac

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ AX_APPEND_COMPILE_FLAGS([-Wall -Werror -Wno-error=deprecated-declarations])
107107
AX_APPEND_COMPILE_FLAGS([-Wextra -Wwrite-string -Wno-unused-parameter])
108108
AX_APPEND_COMPILE_FLAGS([-D_GNU_SOURCE -D_REENTRANT])
109109

110+
AX_COMPILE_CHECK_SIZEOF(int)
111+
AX_COMPILE_CHECK_SIZEOF(long)
112+
AX_COMPILE_CHECK_SIZEOF(size_t, [#include "json_inttypes.h"])
113+
110114
AC_CONFIG_FILES([
111115
Makefile
112116
json-c.pc

json_object.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ struct json_object* json_object_array_get_idx(const struct json_object *jso,
10001000
static int json_array_equal(struct json_object* jso1,
10011001
struct json_object* jso2)
10021002
{
1003-
int len, i;
1003+
size_t len, i;
10041004

10051005
len = json_object_array_length(jso1);
10061006
if (len != json_object_array_length(jso2))
@@ -1079,7 +1079,7 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2)
10791079
return 0;
10801080
}
10811081

1082-
int json_object_array_del_idx(struct json_object *jso, int idx, int count)
1082+
int json_object_array_del_idx(struct json_object *jso, size_t idx, size_t count)
10831083
{
10841084
return array_list_del_idx(jso->o.c_array, idx, count);
10851085
}

json_object.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ extern struct json_object* json_object_array_get_idx(const struct json_object *o
537537
* @param count the number of elements to delete
538538
* @returns 0 if the elements were successfully deleted
539539
*/
540-
extern int json_object_array_del_idx(struct json_object *obj, int idx, int count);
540+
extern int json_object_array_del_idx(struct json_object *obj, size_t idx, size_t count);
541541

542542
/* json_bool type methods */
543543

tests/test1.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ void test_array_del_idx(void);
5353
void test_array_del_idx()
5454
{
5555
int rc;
56-
int ii;
57-
int orig_array_len;
56+
size_t ii;
57+
size_t orig_array_len;
5858
json_object *my_array;
5959
#ifdef TEST_FORMATTED
6060
int sflags = 0;
@@ -105,7 +105,7 @@ void test_array_del_idx()
105105
int main(int argc, char **argv)
106106
{
107107
json_object *my_string, *my_int, *my_object, *my_array;
108-
int i;
108+
size_t i;
109109
#ifdef TEST_FORMATTED
110110
int sflags = 0;
111111
#endif

0 commit comments

Comments
 (0)