From 6cf8890dc1f551a1e87ed8b8e67a733e95b1bb6d Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Tue, 20 Sep 2011 19:24:52 +0200 Subject: [PATCH] Allow NULL for free_public_key. --- g10/ChangeLog | 11 +++++++++++ g10/free-packet.c | 9 +++++++-- g10/keyedit.c | 10 +++------- g10/passphrase.c | 9 +++------ g10/pkclist.c | 9 +++++---- g10/pubkey-enc.c | 6 ++---- g10/revoke.c | 13 ++++--------- g10/skclist.c | 3 +-- 8 files changed, 36 insertions(+), 34 deletions(-) diff --git a/g10/ChangeLog b/g10/ChangeLog index 8ae574717..be131964d 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,14 @@ +2011-09-20 Werner Koch + + * free-packet.c (free_public_key): Allow a NULL argument. + * keyedit.c (keyedit_passwd): No more need to check that PK is NULL. + (menu_addrevoker): Ditto. + * passphrase.c (passphrase_get, passphrase_to_dek_ext): Ditto. + * skclist.c (release_sk_list): Ditto. + * revoke.c (gen_desig_revoke): Ditto. + * pubkey-enc.c (get_session_key): Ditto. + * pkclist.c (build_pk_list): Ditto. + 2011-09-20 Jim Meyering avoid use of freed pointer diff --git a/g10/free-packet.c b/g10/free-packet.c index 267568478..596322127 100644 --- a/g10/free-packet.c +++ b/g10/free-packet.c @@ -120,11 +120,16 @@ release_public_key_parts (PKT_public_key *pk) } +/* Free an allocated public key structure including all parts. + Passing NULL is allowed. */ void free_public_key (PKT_public_key *pk) { - release_public_key_parts (pk); - xfree(pk); + if (pk) + { + release_public_key_parts (pk); + xfree(pk); + } } diff --git a/g10/keyedit.c b/g10/keyedit.c index 62b193a81..fd42439a8 100644 --- a/g10/keyedit.c +++ b/g10/keyedit.c @@ -2253,8 +2253,7 @@ keyedit_passwd (ctrl_t ctrl, const char *username) leave: release_kbnode (keyblock); - if (pk) - free_public_key (pk); + free_public_key (pk); if (err) { log_info ("error changing the passphrase for `%s': %s\n", @@ -3327,9 +3326,7 @@ menu_addrevoker (ctrl_t ctrl, kbnode_t pub_keyblock, int sensitive) { char *answer; - if (revoker_pk) - free_public_key (revoker_pk); - + free_public_key (revoker_pk); revoker_pk = xmalloc_clear (sizeof (*revoker_pk)); tty_printf ("\n"); @@ -3453,8 +3450,7 @@ menu_addrevoker (ctrl_t ctrl, kbnode_t pub_keyblock, int sensitive) fail: if (sig) free_seckey_enc (sig); - if (revoker_pk) - free_public_key (revoker_pk); + free_public_key (revoker_pk); return 0; } diff --git a/g10/passphrase.c b/g10/passphrase.c index 481d29ed9..cc5655505 100644 --- a/g10/passphrase.c +++ b/g10/passphrase.c @@ -235,8 +235,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid, int repeat, memset (fpr, 0, MAX_FINGERPRINT_LEN ); if( keyid && get_pubkey( pk, keyid ) ) { - if (pk) - free_public_key( pk ); + free_public_key (pk); pk = NULL; /* oops: no key for some reason */ } @@ -344,8 +343,7 @@ passphrase_get ( u32 *keyid, int mode, const char *cacheid, int repeat, write_status_errcode ("get_passphrase", rc); } - if (pk) - free_public_key( pk ); + free_public_key (pk); if (rc) { xfree (pw); @@ -531,8 +529,7 @@ passphrase_to_dek_ext (u32 *keyid, int pubkey_algo, } tty_printf("\n"); - if (pk) - free_public_key( pk ); + free_public_key (pk); } if ( next_pw ) diff --git a/g10/pkclist.c b/g10/pkclist.c index 626250ff6..295ee0628 100644 --- a/g10/pkclist.c +++ b/g10/pkclist.c @@ -1064,8 +1064,7 @@ build_pk_list (ctrl_t ctrl, continue; /* Get and check key for the current name. */ - if (pk) - free_public_key (pk); + free_public_key (pk); pk = xmalloc_clear( sizeof *pk ); pk->req_usage = use; rc = get_pubkey_byname (ctrl, NULL, pk, answer, NULL, NULL, 0, 0 ); @@ -1078,7 +1077,8 @@ build_pk_list (ctrl_t ctrl, /* No validation for a default recipient. */ if (!key_present_in_pk_list(pk_list, pk)) { - free_public_key (pk); pk = NULL; + free_public_key (pk); + pk = NULL; log_info (_("skipped: public key " "already set as default recipient\n") ); } @@ -1108,7 +1108,8 @@ build_pk_list (ctrl_t ctrl, * present in the list */ if (!key_present_in_pk_list(pk_list, pk)) { - free_public_key(pk); pk = NULL; + free_public_key (pk); + pk = NULL; log_info(_("skipped: public key already set\n") ); } else diff --git a/g10/pubkey-enc.c b/g10/pubkey-enc.c index 1b94af54f..680182b5d 100644 --- a/g10/pubkey-enc.c +++ b/g10/pubkey-enc.c @@ -97,8 +97,7 @@ get_session_key (PKT_pubkey_enc * k, DEK * dek) for (;;) { - if (sk) - free_public_key (sk); + free_public_key (sk); sk = xmalloc_clear (sizeof *sk); rc = enum_secret_keys (&enum_context, sk); if (rc) @@ -127,8 +126,7 @@ get_session_key (PKT_pubkey_enc * k, DEK * dek) } leave: - if (sk) - free_public_key (sk); + free_public_key (sk); return rc; } diff --git a/g10/revoke.c b/g10/revoke.c index 2c696cc9c..396b6d475 100644 --- a/g10/revoke.c +++ b/g10/revoke.c @@ -262,11 +262,8 @@ gen_desig_revoke( const char *uname, strlist_t locusr ) { SK_LIST list; - if (pk2) - { - free_public_key (pk2); - pk2 = NULL; - } + free_public_key (pk2); + pk2 = NULL; if(sk_list) { @@ -417,10 +414,8 @@ gen_desig_revoke( const char *uname, strlist_t locusr ) log_error(_("no revocation keys found for \"%s\"\n"),uname); leave: - if( pk ) - free_public_key( pk ); - if (pk2) - free_public_key (pk2); + free_public_key (pk); + free_public_key (pk2); if( sig ) free_seckey_enc( sig ); diff --git a/g10/skclist.c b/g10/skclist.c index 912104ef5..5a3ea9503 100644 --- a/g10/skclist.c +++ b/g10/skclist.c @@ -51,8 +51,7 @@ release_sk_list (SK_LIST sk_list) for (; sk_list; sk_list = sk_rover) { sk_rover = sk_list->next; - if (sk_list->pk) - free_public_key (sk_list->pk); + free_public_key (sk_list->pk); xfree (sk_list); } }