mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2025-10-31 22:25:25 -04:00
tests: Wrap calloc by just returning NULL if we're called too early
Since glibc dlsym() calls calloc, we get a call to our calloc wrapper as we try to look up the real calloc implementation. dlsym() will fall back to a static buffer in case calloc returns NULL, so that's what we'll do. This is all highly glibc dependent, of course, but the entire malloc weak symbol wrapper mechanism is, so there's no loss of generality here.
This commit is contained in:
parent
61e9196f56
commit
a2c79b14a1
1 changed files with 13 additions and 0 deletions
|
|
@ -37,6 +37,7 @@ static int num_alloc;
|
|||
static void* (*sys_malloc)(size_t);
|
||||
static void (*sys_free)(void*);
|
||||
static void* (*sys_realloc)(void*, size_t);
|
||||
static void* (*sys_calloc)(size_t, size_t);
|
||||
|
||||
extern const struct test __start_test_section, __stop_test_section;
|
||||
|
||||
|
|
@ -63,6 +64,17 @@ realloc(void* mem, size_t size)
|
|||
return sys_realloc(mem, size);
|
||||
}
|
||||
|
||||
__attribute__ ((visibility("default"))) void *
|
||||
calloc(size_t nmemb, size_t size)
|
||||
{
|
||||
if (sys_calloc == NULL)
|
||||
return NULL;
|
||||
|
||||
num_alloc++;
|
||||
|
||||
return sys_calloc(nmemb, size);
|
||||
}
|
||||
|
||||
static const struct test *
|
||||
find_test(const char *name)
|
||||
{
|
||||
|
|
@ -96,6 +108,7 @@ int main(int argc, char *argv[])
|
|||
siginfo_t info;
|
||||
|
||||
/* Load system malloc, free, and realloc */
|
||||
sys_calloc = dlsym(RTLD_NEXT, "calloc");
|
||||
sys_realloc = dlsym(RTLD_NEXT, "realloc");
|
||||
sys_malloc = dlsym(RTLD_NEXT, "malloc");
|
||||
sys_free = dlsym(RTLD_NEXT, "free");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue