Browse Source

Merge pull request #660 from stoeckmann/arraylist

Validate size arguments in arraylist functions.
tags/json-c-0.16-20220414
Eric Hawicz GitHub 5 years ago
parent
commit
eb08a92218
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 0 deletions
  1. +4
    -0
      arraylist.c
  2. +22
    -0
      tests/test1.c

+ 4
- 0
arraylist.c View File

@@ -45,6 +45,8 @@ struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size
{ {
struct array_list *arr; struct array_list *arr;


if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *))
return NULL;
arr = (struct array_list *)malloc(sizeof(struct array_list)); arr = (struct array_list *)malloc(sizeof(struct array_list));
if (!arr) if (!arr)
return NULL; return NULL;
@@ -106,6 +108,8 @@ int array_list_shrink(struct array_list *arr, size_t empty_slots)
void *t; void *t;
size_t new_size; size_t new_size;


if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length)
return -1;
new_size = arr->length + empty_slots; new_size = arr->length + empty_slots;
if (new_size == arr->size) if (new_size == arr->size)
return 0; return 0;


+ 22
- 0
tests/test1.c View File

@@ -1,4 +1,5 @@
#include <assert.h> #include <assert.h>
#include <limits.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -307,6 +308,27 @@ int main(int argc, char **argv)
} }
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object)); printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));


json_object_put(my_array);
my_array = json_object_new_array_ext(INT_MIN + 1);
if (my_array != NULL)
{
printf("ERROR: able to allocate an array of negative size!\n");
fflush(stdout);
json_object_put(my_array);
my_array = NULL;
}

#if SIZEOF_SIZE_T == SIZEOF_INT
my_array = json_object_new_array_ext(INT_MAX / 2 + 2);
if (my_array != NULL)
{
printf("ERROR: able to allocate an array of insufficient size!\n");
fflush(stdout);
json_object_put(my_array);
my_array = NULL;
}
#endif

json_object_put(my_string); json_object_put(my_string);
json_object_put(my_int); json_object_put(my_int);
json_object_put(my_null); json_object_put(my_null);


Loading…
Cancel
Save