From: Eric Haszlakiewicz Date: Mon, 23 May 2016 02:08:28 +0000 (+0000) Subject: Merge branch 'fixes-for-upstream' of https://github.com/doctaweeks/json-c into doctaw... X-Git-Tag: json-c-0.13-20171207~164^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9a2915ce6603bfcd3de99f3111fe56cc2b4f02bd;p=thirdparty%2Fjson-c.git Merge branch 'fixes-for-upstream' of https://github.com/doctaweeks/json-c into doctaweeks-fixes-for-upstream --- 9a2915ce6603bfcd3de99f3111fe56cc2b4f02bd diff --cc arraylist.c index 1789ad26,54fd2bb3..a02266e8 --- a/arraylist.c +++ b/arraylist.c @@@ -58,23 -56,16 +58,23 @@@ array_list_get_idx(struct array_list *a return arr->array[i]; } - static int array_list_expand_internal(struct array_list *arr, int max) + static int array_list_expand_internal(struct array_list *arr, size_t max) { void *t; - int new_size; + size_t new_size; if(max < arr->size) return 0; - new_size = arr->size << 1; - if (new_size < max) + /* Avoid undefined behaviour on int32 overflow */ + if( arr->size >= INT_MAX / 2 ) new_size = max; - if(!(t = realloc(arr->array, new_size*sizeof(void*)))) return -1; + else + { + new_size = arr->size << 1; + 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; arr->array = (void**)t; (void)memset(arr->array + arr->size, 0, (new_size-arr->size)*sizeof(void*)); arr->size = new_size; @@@ -82,9 -73,8 +82,9 @@@ } int - array_list_put_idx(struct array_list *arr, int idx, void *data) + array_list_put_idx(struct array_list *arr, size_t idx, void *data) { + if( idx < 0 || idx > INT_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; diff --cc json_object.c index 46701e76,8e7e8a1b..b0b381e3 --- a/json_object.c +++ b/json_object.c @@@ -975,7 -959,7 +975,7 @@@ struct json_object* json_object_array_b return *result; } - int json_object_array_length(const struct json_object *jso) -size_t json_object_array_length(struct json_object *jso) ++size_t json_object_array_length(const struct json_object *jso) { return array_list_length(jso->o.c_array); } @@@ -991,8 -975,8 +991,8 @@@ int json_object_array_put_idx(struct js return array_list_put_idx(jso->o.c_array, idx, val); } -struct json_object* json_object_array_get_idx(struct json_object *jso, +struct json_object* json_object_array_get_idx(const struct json_object *jso, - int idx) + size_t idx) { return (struct json_object*)array_list_get_idx(jso->o.c_array, idx); } diff --cc json_object.h index a89de44d,f8106781..46540b74 --- a/json_object.h +++ b/json_object.h @@@ -457,7 -449,7 +457,7 @@@ extern struct array_list* json_object_g * @param obj the json_object instance * @returns an int */ - extern int json_object_array_length(const struct json_object *obj); -extern size_t json_object_array_length(struct json_object *obj); ++extern size_t json_object_array_length(const struct json_object *obj); /** Sorts the elements of jso of type json_type_array * @@@ -523,22 -515,9 +523,22 @@@ extern int json_object_array_put_idx(st * @param idx the index to get the element at * @returns the json_object at the specified index (or NULL) */ -extern struct json_object* json_object_array_get_idx(struct json_object *obj, +extern struct json_object* json_object_array_get_idx(const struct json_object *obj, - int idx); + size_t idx); +/** Delete an elements from a specified index in an array (a json_object of type json_type_array) + * + * The reference count will be decremented for each of the deleted objects. If there + * are no more owners of an element that is being deleted, then the value is + * freed. Otherwise, the reference to the value will remain in memory. + * + * @param obj the json_object instance + * @param idx the index to start deleting elements at + * @param count the number of elements to delete + * @returns 0 if the elements were successfully deleted + */ +extern int json_object_array_del_idx(struct json_object *obj, int idx, int count); + /* json_bool type methods */ /** Create a new empty json_object of type json_type_boolean