summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@behdad.org>2022-12-29 19:54:51 -0700
committerBehdad Esfahbod <behdad@behdad.org>2022-12-30 10:34:46 -0700
commit71c44431397cc4bd60a48d6787cdfd8880628223 (patch)
tree95bf78ede0b542e25eef79998d2b2e7eb73e7758 /util
parent4913f079501e9dac28c6aed061b7c69e151e6f5c (diff)
downloadcairo-71c44431397cc4bd60a48d6787cdfd8880628223.tar.gz
[malloc-stats] Fix to not crash
Can't use constructor as malloc might be called before that.
Diffstat (limited to 'util')
-rw-r--r--util/malloc-stats.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/util/malloc-stats.c b/util/malloc-stats.c
index cf712bd3a..eba0b2ac0 100644
--- a/util/malloc-stats.c
+++ b/util/malloc-stats.c
@@ -176,9 +176,14 @@ static void *(*old_malloc)(size_t);
static void *(*old_realloc)(void *, size_t);
static int enable_hook = 0;
+static void init(void);
+
void *
malloc(size_t size)
{
+ if (!old_malloc)
+ init ();
+
if (enable_hook) {
enable_hook = 0;
void *caller = __builtin_return_address(0);
@@ -192,6 +197,9 @@ malloc(size_t size)
void *
realloc(void *ptr, size_t size)
{
+ if (!old_malloc)
+ init ();
+
if (enable_hook) {
enable_hook = 0;
void *caller = __builtin_return_address(0);
@@ -202,7 +210,7 @@ realloc(void *ptr, size_t size)
return old_realloc (ptr, size);
}
-static void __attribute__ ((constructor))
+static void
init(void)
{
old_malloc = dlsym(RTLD_NEXT, "malloc");