Skip to content

Commit

Permalink
Fix memory management and memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
olszomal authored and mtrojnar committed Feb 10, 2025
1 parent 3352258 commit 388b3b3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/eng_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/util_uri.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 388b3b3

Please sign in to comment.