Skip to content

Commit d728289

Browse files
pks-tgitster
authored andcommitted
reftable/basics: return NULL on zero-sized allocations
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]>
1 parent 2d3cb4b commit d728289

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

reftable/basics.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ static void (*reftable_free_ptr)(void *);
1616

1717
void *reftable_malloc(size_t sz)
1818
{
19+
if (!sz)
20+
return NULL;
1921
if (reftable_malloc_ptr)
2022
return (*reftable_malloc_ptr)(sz);
2123
return malloc(sz);
2224
}
2325

2426
void *reftable_realloc(void *p, size_t sz)
2527
{
28+
if (!sz) {
29+
reftable_free(p);
30+
return NULL;
31+
}
32+
2633
if (reftable_realloc_ptr)
2734
return (*reftable_realloc_ptr)(p, sz);
2835
return realloc(p, sz);

0 commit comments

Comments
 (0)