Skip to content

Commit 3573030

Browse files
pks-tgitster
authored andcommitted
reftable/basics: ban standard allocator functions
The reftable library uses pluggable allocators, which means that we shouldn't ever use the standard allocator functions. But it is an easy mistake to make to accidentally use e.g. free(3P) instead of the reftable-specific `reftable_free()` function, and we do not have any mechanism to detect this misuse right now. Introduce a couple of macros that ban the standard allocators, similar to how we do it in "banned.h". Note that we do not ban the following two classes of functions: - Macros like `FREE_AND_NULL()` or `REALLOC_ARRAY()`. As those expand to code that contains already-banned functions we'd get a compiler error even without banning those macros explicitly. - Git-specific allocators like `xmalloc()` and friends. The primary reason is that there are simply too many of them, so we're rather aiming for best effort here. Furthermore, the eventual goal is to make them unavailable in the reftable library place by not pulling them in via "git-compat-utils.h" anymore. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 24e0ade commit 3573030

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

reftable/basics.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ license that can be found in the LICENSE file or at
66
https://developers.google.com/open-source/licenses/bsd
77
*/
88

9+
#define REFTABLE_ALLOW_BANNED_ALLOCATORS
910
#include "basics.h"
1011
#include "reftable-basics.h"
1112

reftable/basics.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ char *reftable_strdup(const char *str);
7373
} while (0)
7474
#define REFTABLE_FREE_AND_NULL(p) do { reftable_free(p); (p) = NULL; } while (0)
7575

76+
#ifndef REFTABLE_ALLOW_BANNED_ALLOCATORS
77+
# define REFTABLE_BANNED(func) use_reftable_##func##_instead
78+
# undef malloc
79+
# define malloc(sz) REFTABLE_BANNED(malloc)
80+
# undef realloc
81+
# define realloc(ptr, sz) REFTABLE_BANNED(realloc)
82+
# undef free
83+
# define free(ptr) REFTABLE_BANNED(free)
84+
# undef calloc
85+
# define calloc(nelem, elsize) REFTABLE_BANNED(calloc)
86+
# undef strdup
87+
# define strdup(str) REFTABLE_BANNED(strdup)
88+
#endif
89+
7690
/* Find the longest shared prefix size of `a` and `b` */
7791
struct strbuf;
7892
int common_prefix_size(struct strbuf *a, struct strbuf *b);

0 commit comments

Comments
 (0)