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;
}
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;
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);
}
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);
}
* @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
*
* @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