Skip to content

Commit cf3ca70

Browse files
committed
t-reftable-basics: allow for malloc to be #defined
As indicated by the `#undef malloc` line in `reftable/basics.h`, it is quite common to use allocators other than the default one by defining `malloc` constants and friends. This pattern is used e.g. in Git for Windows, which uses the powerful and performant `mimalloc` allocator. Furthermore, in `reftable/basics.c` this `#undef malloc` is _specifically_ disabled by virtue of defining the `REFTABLE_ALLOW_BANNED_ALLOCATORS` constant before including `reftable/basic.h`, to ensure that such a custom allocator is also used in the reftable code. However, in 8db127d (reftable: avoid leaks on realloc error, 2024-12-28) and in 2cca185 (reftable: fix allocation count on realloc error, 2024-12-28), `reftable_set_alloc()` function calls were introduced that pass `malloc`, `realloc` and `free` function pointers as parameters _after_ `reftable/basics.h` ensured that they were no longer `#define`d. This would override the custom allocator and re-set it to the default allocator provided by, say, libc or MSVCRT. This causes problems because those calls happen after the initial allocator has already been used to initialize an array, which is subsequently resized using the overridden default `realloc()` allocator. You cannot mix and match allocators like that, which leads to a `STATUS_HEAP_CORRUPTION` (C0000374) on Windows, and when running this unit test through shell and/or `prove` (which only support 7-bit status codes), it surfaces as exit code 127. It is actually unnecessary to use those function pointers to `malloc`/`realloc`/`free`, though: The `reftable` code goes out of its way to fall back to the initial allocator when passing `NULL` parameters instead. So let's do that instead of causing heap corruptions. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 1e78120 commit cf3ca70

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

t/unit-tests/t-reftable-basics.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
157157

158158
old_alloc = alloc;
159159
old_arr = arr;
160-
reftable_set_alloc(malloc, realloc_stub, free);
160+
reftable_set_alloc(NULL, realloc_stub, NULL);
161161
check(REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc));
162162
check(arr == old_arr);
163163
check_uint(alloc, ==, old_alloc);
164164

165165
old_alloc = alloc;
166-
reftable_set_alloc(malloc, realloc, free);
166+
reftable_set_alloc(NULL, NULL, NULL);
167167
check(!REFTABLE_ALLOC_GROW(arr, old_alloc + 1, alloc));
168168
check(arr != NULL);
169169
check_uint(alloc, >, old_alloc);
@@ -188,11 +188,11 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
188188
arr[alloc - 1] = 42;
189189

190190
old_alloc = alloc;
191-
reftable_set_alloc(malloc, realloc_stub, free);
191+
reftable_set_alloc(NULL, realloc_stub, NULL);
192192
REFTABLE_ALLOC_GROW_OR_NULL(arr, old_alloc + 1, alloc);
193193
check(arr == NULL);
194194
check_uint(alloc, ==, 0);
195-
reftable_set_alloc(malloc, realloc, free);
195+
reftable_set_alloc(NULL, NULL, NULL);
196196

197197
reftable_free(arr);
198198
}

0 commit comments

Comments
 (0)