Skip to content

Commit

Permalink
reftable/basics: return NULL on zero-sized allocations
Browse files Browse the repository at this point in the history
In the preceding commits we have fixed a couple of issues when
allocating zero-sized objects. These issues were masked by
implementation-defined behaviour. Quoting malloc(3p):

  If size is 0, either:

    * A null pointer shall be returned and errno may be set to an
      implementation-defined value, or

    * A pointer to the allocated space shall be returned. The
      application shall ensure that the pointer is not used to access an
      object.

So it is perfectly valid that implementations of this function may or
may not return a NULL pointer in such a case.

Adapt both `reftable_malloc()` and `reftable_realloc()` so that they
return NULL pointers on zero-sized allocations. This should remove any
implementation-defined behaviour in our allocators and thus allows us to
detect such platform-specific issues more easily going forward.

Signed-off-by: Patrick Steinhardt <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
pks-t authored and gitster committed Dec 22, 2024
1 parent 2d3cb4b commit d728289
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions reftable/basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@ static void (*reftable_free_ptr)(void *);

void *reftable_malloc(size_t sz)
{
if (!sz)
return NULL;
if (reftable_malloc_ptr)
return (*reftable_malloc_ptr)(sz);
return malloc(sz);
}

void *reftable_realloc(void *p, size_t sz)
{
if (!sz) {
reftable_free(p);
return NULL;
}

if (reftable_realloc_ptr)
return (*reftable_realloc_ptr)(p, sz);
return realloc(p, sz);
Expand Down

0 comments on commit d728289

Please sign in to comment.