Skip to content

Commit

Permalink
Allow xcalloc() to return NULL when size is 0 for portability
Browse files Browse the repository at this point in the history
According to the calloc(3) man page, when nmemb or size is 0, calloc() can
either return NULL or a unique pointer that can be passed to free().
https://manpages.org/calloc/3

While gcc (13.3.0) and clang (15.0.7), in my environment, return *a unique
pointer* in this case, mruby's `mrb_calloc` returns *NULL*.
https://github.com/mruby/mruby/blob/3.3.0/src/gc.c#L253

Since `pm_constant_pool_init()` is commonly called with capacity=0 during
normal operation of Prism, `xcalloc()` needs to handle NULL returns to
integrate with mruby.

Per free()'s and realloc()'s specification, passing NULL pointer is a valid
operation, so this change should be safe.
  • Loading branch information
hasumikin committed Feb 1, 2025
1 parent a46edfd commit 528bd3d
Showing 1 changed file with 0 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/util/pm_constant_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pm_constant_id_list_init(pm_constant_id_list_t *list) {
void
pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity) {
list->ids = xcalloc(capacity, sizeof(pm_constant_id_t));
if (list->ids == NULL) abort();

list->size = 0;
list->capacity = capacity;
Expand Down

0 comments on commit 528bd3d

Please sign in to comment.