@@ -64,24 +64,24 @@ Array *array_new()
6464Array * array_new_conf (ArrayConf * conf )
6565{
6666 float ex ;
67-
68- /* The expansion factor must be greater than one for the
67+
68+ /* The expansion factor must be greater than one for the
6969 * array to grow */
7070 if (conf -> exp_factor <= 1 )
7171 ex = DEFAULT_EXPANSION_FACTOR ;
72- else
72+ else
7373 ex = conf -> exp_factor ;
74-
74+
7575 /* Needed to avoid an integer overflow on the first resize and
7676 * to easily check for any future oveflows. */
7777 if (!conf -> capacity || ex >= MAX_ELEMENTS / conf -> capacity )
7878 return NULL ;
79-
79+
8080 Array * ar = conf -> mem_calloc (1 , sizeof (Array ));
8181
8282 if (ar == NULL )
8383 return NULL ;
84-
84+
8585 ar -> exp_factor = ex ;
8686 ar -> capacity = conf -> capacity ;
8787 ar -> mem_alloc = conf -> mem_alloc ;
@@ -409,13 +409,22 @@ Array *array_subarray(Array *ar, size_t b, size_t e)
409409 if (b > e || e > ar -> size )
410410 return NULL ;
411411
412- Array * sub_ar = ar -> mem_calloc (1 , sizeof (Array ));
412+ Array * sub_ar = ar -> mem_calloc (1 , sizeof (Array ));
413+
414+ if (!sub_ar )
415+ return NULL ;
416+
417+ /* Try to allocate the buffer */
418+ if (!(sub_ar -> buffer = ar -> mem_alloc (sub_ar -> capacity * sizeof (void * )))) {
419+ ar -> mem_free (sub_ar );
420+ return NULL ;
421+ }
422+
413423 sub_ar -> mem_alloc = ar -> mem_alloc ;
414424 sub_ar -> mem_calloc = ar -> mem_calloc ;
415425 sub_ar -> mem_free = ar -> mem_free ;
416426 sub_ar -> size = e - b + 1 ;
417427 sub_ar -> capacity = sub_ar -> size ;
418- sub_ar -> buffer = ar -> mem_alloc (sub_ar -> capacity * sizeof (void * ));
419428
420429 memcpy (sub_ar -> buffer ,
421430 & (ar -> buffer [b ]),
@@ -433,16 +442,22 @@ Array *array_subarray(Array *ar, size_t b, size_t e)
433442 *
434443 * @param[in] ar the array to be copied
435444 *
436- * @return a shallow copy of the specified array
445+ * @return a shallow copy of the specified array, or NULL if the allocation failed
437446 */
438447Array * array_copy_shallow (Array * ar )
439448{
440449 Array * copy = ar -> mem_alloc (sizeof (Array ));
441450
451+ if (!copy )
452+ return NULL ;
453+
454+ if (!(copy -> buffer = ar -> mem_calloc (copy -> capacity , sizeof (void * )))) {
455+ ar -> mem_free (copy );
456+ return NULL ;
457+ }
442458 copy -> exp_factor = ar -> exp_factor ;
443459 copy -> size = ar -> size ;
444460 copy -> capacity = ar -> capacity ;
445- copy -> buffer = ar -> mem_calloc (copy -> capacity , sizeof (void * ));
446461 copy -> mem_alloc = ar -> mem_alloc ;
447462 copy -> mem_calloc = ar -> mem_calloc ;
448463 copy -> mem_free = ar -> mem_free ;
@@ -464,16 +479,23 @@ Array *array_copy_shallow(Array *ar)
464479 * @param[in] ar the array to be copied
465480 * @param[in] cp the copy function that returns a copy of a array element
466481 *
467- * @return a deep copy of the specified array
482+ * @return a deep copy of the specified array, or NULL if the allocation failed
468483 */
469484Array * array_copy_deep (Array * ar , void * (* cp ) (void * ))
470485{
471- Array * copy = ar -> mem_alloc (sizeof (Array ));
486+ Array * copy = ar -> mem_alloc (sizeof (Array ));
487+
488+ if (!copy )
489+ return NULL ;
490+
491+ if (!(copy -> buffer = ar -> mem_calloc (copy -> capacity , sizeof (void * )))) {
492+ ar -> mem_free (copy );
493+ return NULL ;
494+ }
472495
473496 copy -> exp_factor = ar -> exp_factor ;
474497 copy -> size = ar -> size ;
475498 copy -> capacity = ar -> capacity ;
476- copy -> buffer = ar -> mem_calloc (copy -> capacity , sizeof (void * ));
477499 copy -> mem_alloc = ar -> mem_alloc ;
478500 copy -> mem_calloc = ar -> mem_calloc ;
479501 copy -> mem_free = ar -> mem_free ;
@@ -507,20 +529,27 @@ void array_reverse(Array *ar)
507529 * never shrink below 1.
508530 *
509531 * @param[in] ar the array whose capacity is being trimmed.
532+ *
533+ * @return true if the operation was successful
510534 */
511- void array_trim_capacity (Array * ar )
535+ bool array_trim_capacity (Array * ar )
512536{
513537 if (ar -> size == ar -> capacity )
514- return ;
538+ return false ;
515539
516- void * * new_buff = ar -> mem_calloc (ar -> size , sizeof (void * ));
517- size_t size = ar -> size < 1 ? 1 : ar -> size ;
540+ void * * new_buff = ar -> mem_calloc (ar -> size , sizeof (void * ));
541+
542+ if (!new_buff )
543+ return false;
544+
545+ size_t size = ar -> size < 1 ? 1 : ar -> size ;
518546
519547 memcpy (new_buff , ar -> buffer , size * sizeof (void * ));
520548 ar -> mem_free (ar -> buffer );
521549
522550 ar -> buffer = new_buff ;
523551 ar -> capacity = ar -> size ;
552+ return true;
524553}
525554
526555/**
0 commit comments