]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Issue #332: fix a long-standing bug in array_list_put_idx() where it would attempt...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 9 Jul 2017 02:04:35 +0000 (19:04 -0700)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sun, 9 Jul 2017 02:04:35 +0000 (19:04 -0700)
Add a test that triggers the problem to ensure it stays fixed.

arraylist.c
tests/test1.c

index e859dfd052e08d9a51c1dd497b9f3dc25cdd4080..8439cc2df0113bbab9f2705fc4f740675f3ae0b5 100644 (file)
@@ -96,7 +96,8 @@ array_list_put_idx(struct array_list *arr, size_t idx, void *data)
 {
   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]);
+  if(idx < arr->length && arr->array[idx])
+    arr->free_fn(arr->array[idx]);
   arr->array[idx] = data;
   if(arr->length <= idx) arr->length = idx + 1;
   return 0;
index a53c4ce92a88a180901b730fc292a36953ecd0fc..3ddaf720364b0fa531a824e54cc82b0191778f5f 100644 (file)
@@ -120,6 +120,19 @@ void test_array_del_idx()
               (int)(orig_array_len + 1), rc, json_object_to_json_string(my_array));
 
        json_object_put(my_array);
+       
+       /* Delete some array indexes, then add more */
+       my_array = make_array();
+       rc = json_object_array_del_idx(my_array, 0, orig_array_len - 1);
+       printf("after del_idx(0,%d)=%d, my_array.to_string()=%s\n",
+              (int)(orig_array_len - 1), rc, json_object_to_json_string(my_array));
+       json_object_array_add(my_array, json_object_new_string("s1"));
+       json_object_array_add(my_array, json_object_new_string("s2"));
+       json_object_array_add(my_array, json_object_new_string("s3"));
+
+       printf("after adding more entries, my_array.to_string()=%s\n",
+              json_object_to_json_string(my_array));
+       json_object_put(my_array);
 }
 
 int main(int argc, char **argv)