From 388b3b3fd65818fbd08a02e38b2e1a20595cc3a5 Mon Sep 17 00:00:00 2001 From: olszomal Date: Fri, 7 Feb 2025 08:58:20 +0100 Subject: [PATCH] Fix memory management and memory leak --- src/eng_back.c | 4 +++- src/util.h | 2 +- src/util_uri.c | 7 ++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/eng_back.c b/src/eng_back.c index 42cdf498..043910f1 100644 --- a/src/eng_back.c +++ b/src/eng_back.c @@ -95,8 +95,10 @@ ENGINE_CTX *ENGINE_CTX_new() return NULL; memset(ctx, 0, sizeof(ENGINE_CTX)); ctx->util_ctx = UTIL_CTX_new(); - if (!ctx->util_ctx) + if (!ctx->util_ctx) { + OPENSSL_free(ctx); return NULL; + } pthread_mutex_init(&ctx->lock, 0); mod = getenv("PKCS11_MODULE_PATH"); diff --git a/src/util.h b/src/util.h index a4225cfc..daa12083 100644 --- a/src/util.h +++ b/src/util.h @@ -57,7 +57,7 @@ int UTIL_CTX_ctrl_set_user_interface(UTIL_CTX *ctx, UI_METHOD *ui_method); int UTIL_CTX_ctrl_set_callback_data(UTIL_CTX *ctx, void *callback_data); int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx); int UTIL_CTX_init_libp11(UTIL_CTX *ctx); -int UTIL_CTX_free_libp11(UTIL_CTX *ctx); +void UTIL_CTX_free_libp11(UTIL_CTX *ctx); void UTIL_CTX_set_vlog_a(UTIL_CTX *ctx, PKCS11_VLOG_A_CB vlog); void UTIL_CTX_set_debug_level(UTIL_CTX *ctx, int debug_level); diff --git a/src/util_uri.c b/src/util_uri.c index c6718642..49cf6306 100644 --- a/src/util_uri.c +++ b/src/util_uri.c @@ -145,7 +145,7 @@ int UTIL_CTX_init_libp11(UTIL_CTX *ctx) if (ctx->pkcs11_ctx && ctx->slot_list) return 0; - UTIL_CTX_log(ctx, LOG_NOTICE, "PKCS#11: Initializing the engine: %s\n", ctx->module); + UTIL_CTX_log(ctx, LOG_NOTICE, "PKCS#11: Initializing the module: %s\n", ctx->module); pkcs11_ctx = PKCS11_CTX_new(); PKCS11_set_vlog_a_method(pkcs11_ctx, ctx->vlog); @@ -164,7 +164,7 @@ int UTIL_CTX_init_libp11(UTIL_CTX *ctx) return ctx->pkcs11_ctx && ctx->slot_list ? 0 : -1; } -int UTIL_CTX_free_libp11(UTIL_CTX *ctx) +void UTIL_CTX_free_libp11(UTIL_CTX *ctx) { if (ctx->slot_list) { PKCS11_release_all_slots(ctx->pkcs11_ctx, @@ -230,7 +230,8 @@ void UTIL_CTX_log(UTIL_CTX *ctx, int level, const char *format, ...) static char *dump_hex(unsigned char *val, const size_t len) { - int i, j = 0, size = 2 * len + 1; + int j = 0; + size_t i, size = 2 * len + 1; char *hexbuf = OPENSSL_malloc((size_t)size); if (!hexbuf)