diff --git a/src/shared/dlfcn-util.h b/src/shared/dlfcn-util.h index f8a16433fce..ca632f4e1f4 100644 --- a/src/shared/dlfcn-util.h +++ b/src/shared/dlfcn-util.h @@ -24,3 +24,11 @@ int dlopen_many_sym_or_warn_sentinel(void **dlp, const char *filename, int log_l /* libbpf is a bit confused about type-safety and API compatibility. Provide a macro that can tape over that mess. Sad. */ #define DLSYM_ARG_FORCE(arg) \ &sym_##arg, STRINGIFY(arg) + +static inline void *safe_dlclose(void *p) { + if (!p) + return NULL; + + assert_se(dlclose(p) == 0); + return NULL; +} diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index 3870f0401be..974b14439cf 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -111,11 +111,7 @@ void tpm2_context_destroy(struct tpm2_context *c) { sym_Esys_Finalize(&c->esys_context); c->tcti_context = mfree(c->tcti_context); - - if (c->tcti_dl) { - dlclose(c->tcti_dl); - c->tcti_dl = NULL; - } + c->tcti_dl = safe_dlclose(c->tcti_dl); } static inline void Esys_Finalize_wrapper(ESYS_CONTEXT **c) {