summaryrefslogtreecommitdiffstats
path: root/reftable/basics.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2024-12-22 08:24:31 +0100
committerJunio C Hamano <gitster@pobox.com>2024-12-22 09:58:23 +0100
commitd7282891f542b58410a209230009182b9057af79 (patch)
tree59c754a0110330941db2881c049f316c13885fcd /reftable/basics.c
parentreftable/stack: fix zero-sized allocation when there are no readers (diff)
downloadgit-d7282891f542b58410a209230009182b9057af79.tar.xz
git-d7282891f542b58410a209230009182b9057af79.zip
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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to '')
-rw-r--r--reftable/basics.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/reftable/basics.c b/reftable/basics.c
index 9a949e5cf8..eab5553d93 100644
--- a/reftable/basics.c
+++ b/reftable/basics.c
@@ -16,6 +16,8 @@ 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);
@@ -23,6 +25,11 @@ void *reftable_malloc(size_t 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);