Skip to content

Commit

Permalink
pkcs15-lib.c - avoid memory leak, fix comments
Browse files Browse the repository at this point in the history
 On branch X25519-improvements-2
 Changes to be committed:
	modified:   pkcs15-lib.c
  • Loading branch information
dengert committed Sep 3, 2024
1 parent 25c6479 commit 67cc887
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/pkcs15init/pkcs15-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1904,11 +1904,13 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
r = sc_pkcs15_fix_ec_parameters(ctx, &key.u.ec.params);
LOG_TEST_GOTO_ERR(ctx, r, "Failed to fix EC public key parameters");

free(key.u.ec.ecpointQ.value); /* avoid memory leak if already allocated */

if (keyargs->key.u.ec.ecpointQ.value) {
key.u.ec.ecpointQ.value = malloc(keyargs->key.u.ec.ecpointQ.len);
if (!key.u.ec.ecpointQ.value) {
r = SC_ERROR_OUT_OF_MEMORY;
LOG_TEST_GOTO_ERR(ctx, r, "Failed to copy EC... public key parameters");
LOG_TEST_GOTO_ERR(ctx, r, "Failed to copy EC public key");
}
}
key.u.ec.ecpointQ.len = keyargs->key.u.ec.ecpointQ.len;
Expand Down Expand Up @@ -1984,7 +1986,7 @@ sc_pkcs15init_store_public_key(struct sc_pkcs15_card *p15card, struct sc_profile
key.u.ec.ecpointQ.value = malloc(keyargs->key.u.ec.ecpointQ.len);
if (!key.u.ec.ecpointQ.value) {
r = SC_ERROR_OUT_OF_MEMORY;
LOG_TEST_GOTO_ERR(ctx, r, "Cannot allocate EC params");
LOG_TEST_GOTO_ERR(ctx, r, "Cannot allocate EC public key");
}
key.u.ec.ecpointQ.len = keyargs->key.u.ec.ecpointQ.len;
memcpy(key.u.ec.ecpointQ.value, keyargs->key.u.ec.ecpointQ.value, key.u.ec.ecpointQ.len);
Expand Down

0 comments on commit 67cc887

Please sign in to comment.