From b07527e85f3616f5825b2a396b4193b82c8000d5 Mon Sep 17 00:00:00 2001 From: Emil Velikov Date: Wed, 9 Oct 2024 16:26:25 +0100 Subject: [PATCH] testsuite: preserve the existing LD_PRELOAD In order for the sanitisers to work correctly, we need to LD_PRELOAD the sanitizer shared library. So drop the explicit unset(LD_PRELOAD) and instead prepend the existing value to the LD_PRELOAD path that we construct. Const annotate the other LD_PRELOAD instance, while we're here. Signed-off-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/179 Signed-off-by: Lucas De Marchi --- testsuite/test-testsuite.c | 2 +- testsuite/testsuite.c | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/testsuite/test-testsuite.c b/testsuite/test-testsuite.c index 3b84aaa7..eff911a7 100644 --- a/testsuite/test-testsuite.c +++ b/testsuite/test-testsuite.c @@ -30,7 +30,7 @@ static noreturn int testsuite_uname(const struct test *t) exit(EXIT_FAILURE); if (!streq(u.release, TEST_UNAME)) { - char *ldpreload = getenv("LD_PRELOAD"); + const char *ldpreload = getenv("LD_PRELOAD"); ERR("u.release=%s should be %s\n", u.release, TEST_UNAME); ERR("LD_PRELOAD=%s\n", ldpreload); exit(EXIT_FAILURE); diff --git a/testsuite/testsuite.c b/testsuite/testsuite.c index 6e9d2089..e12426a5 100644 --- a/testsuite/testsuite.c +++ b/testsuite/testsuite.c @@ -161,8 +161,6 @@ static void test_export_environ(const struct test *t) size_t i; const struct keyval *env; - unsetenv("LD_PRELOAD"); - for (i = 0; i < _TC_LAST; i++) { const char *ldpreload; size_t ldpreloadlen; @@ -189,8 +187,27 @@ static void test_export_environ(const struct test *t) preload[preloadlen] = '\0'; } - if (preload != NULL) + if (preload != NULL) { + const char *existing_preload = getenv("LD_PRELOAD"); + if (existing_preload) { + char *tmp; + size_t len = strlen(existing_preload); + + tmp = malloc(preloadlen + 2 + len); + if (tmp == NULL) { + ERR("oom: test_export_environ()\n"); + return; + } + memcpy(tmp, existing_preload, len); + tmp[len++] = ' '; + memcpy(tmp + len, preload, preloadlen); + preloadlen += len; + tmp[preloadlen] = '\0'; + free(preload); + preload = tmp; + } setenv("LD_PRELOAD", preload, 1); + } free(preload); -- 2.47.2