Skip to content

Commit b92a6da

Browse files
committed
Ensure cleanup on UTIL_CTX_init_libp11 failures
1 parent c62fdbf commit b92a6da

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/util_uri.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ static int UTIL_CTX_enumerate_slots_unlocked(UTIL_CTX *ctx)
130130
UTIL_CTX_log(ctx, LOG_ERR, "Failed to enumerate slots\n");
131131
return 0;
132132
}
133-
if (ctx->slot_count < 1) {
133+
if (!ctx->slot_list || ctx->slot_count < 1) {
134134
UTIL_CTX_log(ctx, LOG_ERR, "No slot found\n");
135135
return 0;
136136
}
@@ -156,28 +156,27 @@ int UTIL_CTX_enumerate_slots(UTIL_CTX *ctx)
156156

157157
int UTIL_CTX_init_libp11(UTIL_CTX *ctx)
158158
{
159-
PKCS11_CTX *pkcs11_ctx;
160-
161-
if (ctx->pkcs11_ctx && ctx->slot_list)
159+
if (ctx->pkcs11_ctx && ctx->slot_list && ctx->slot_count > 0)
162160
return 0;
163161

164162
UTIL_CTX_log(ctx, LOG_NOTICE, "PKCS#11: Initializing the module: %s\n", ctx->module);
165163

166-
pkcs11_ctx = PKCS11_CTX_new();
167-
PKCS11_set_vlog_a_method(pkcs11_ctx, ctx->vlog);
168-
PKCS11_CTX_init_args(pkcs11_ctx, ctx->init_args);
169-
PKCS11_set_ui_method(pkcs11_ctx, ctx->ui_method, ctx->ui_data);
170-
if (PKCS11_CTX_load(pkcs11_ctx, ctx->module) < 0) {
164+
ctx->pkcs11_ctx = PKCS11_CTX_new();
165+
if (!ctx->pkcs11_ctx)
166+
return -1;
167+
PKCS11_set_vlog_a_method(ctx->pkcs11_ctx, ctx->vlog);
168+
PKCS11_CTX_init_args(ctx->pkcs11_ctx, ctx->init_args);
169+
PKCS11_set_ui_method(ctx->pkcs11_ctx, ctx->ui_method, ctx->ui_data);
170+
if (PKCS11_CTX_load(ctx->pkcs11_ctx, ctx->module) < 0) {
171171
UTIL_CTX_log(ctx, LOG_ERR, "Unable to load module %s\n", ctx->module);
172-
PKCS11_CTX_free(pkcs11_ctx);
172+
UTIL_CTX_free_libp11(ctx);
173173
return -1;
174174
}
175-
ctx->pkcs11_ctx = pkcs11_ctx;
176-
177-
if (UTIL_CTX_enumerate_slots_unlocked(ctx) != 1)
175+
if (UTIL_CTX_enumerate_slots_unlocked(ctx) != 1) {
176+
UTIL_CTX_free_libp11(ctx);
178177
return -1;
179-
180-
return ctx->pkcs11_ctx && ctx->slot_list ? 0 : -1;
178+
}
179+
return 0;
181180
}
182181

183182
void UTIL_CTX_free_libp11(UTIL_CTX *ctx)

0 commit comments

Comments
 (0)