Fix a few memory leaks when enumerating keys with GpgME.
authorAndrej Kacian <ticho@claws-mail.org>
Tue, 11 Jul 2017 19:09:24 +0000 (21:09 +0200)
committerAndrej Kacian <ticho@claws-mail.org>
Tue, 11 Jul 2017 19:09:24 +0000 (21:09 +0200)
Also replaced uses of deprecated gpgme_key_release()
with gpgme_key_unref().

src/plugins/pgpcore/autocompletion.c
src/plugins/pgpcore/select-keys.c
src/plugins/pgpcore/sgpgme.c

index 286f241..a4f15a6 100644 (file)
@@ -100,7 +100,7 @@ static gboolean pgp_autocompletion_hook(gpointer source, gpointer data)
                                        i++;
                                }
                        }
-                       gpgme_key_release(key);
+                       gpgme_key_unref(key);
                }
                gpgme_release(ctx);
        }
index 69a7f8a..47e4b66 100644 (file)
@@ -167,7 +167,7 @@ static void
 destroy_key (gpointer data)
 {
     gpgme_key_t key = data;
-    gpgme_key_release (key);
+    gpgme_key_unref (key);
 }
 
 static void
@@ -320,6 +320,8 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
                g_free(raw_mail);
        }
        num_results++;
+       if (last_key != NULL)
+               gpgme_key_unref(last_key);
        last_key = key;
        key = NULL;
         update_progress (sk, ++running, pattern);
@@ -344,7 +346,11 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
            gpgme_release (ctx);
     }
     /*gtk_cmclist_thaw (select_keys.clist);*/
-    return (exact_match == TRUE && num_results == 1 ? last_key:NULL);
+    if (exact_match && num_results == 1)
+           return last_key;
+
+    gpgme_key_unref(last_key);
+    return NULL;
 }
 
 
index 2290526..0579f42 100644 (file)
@@ -607,37 +607,43 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
                /* Look for any key, not just private ones, or GPGMe doesn't
                 * correctly set the revoked flag. */
                err = gpgme_op_keylist_start(ctx, keyid, 0);
-               while ((err = gpgme_op_keylist_next(ctx, &key)) == 0) {
+               while (err == 0) {
+                       if ((err = gpgme_op_keylist_next(ctx, &key)) != 0)
+                               break;
+
                        if (key == NULL)
                                continue;
 
-                       if (!key->can_sign)
+                       if (!key->can_sign) {
+                               debug_print("skipping a key, can not be used for signing\n");
+                               gpgme_key_unref(key);
                                continue;
+                       }
 
                        if (key->protocol != gpgme_get_protocol(ctx)) {
                                debug_print("skipping a key (wrong protocol %d)\n", key->protocol);
-                               gpgme_key_release(key);
+                               gpgme_key_unref(key);
                                continue;
                        }
 
                        if (key->expired) {
                                debug_print("skipping a key, expired\n");
-                               gpgme_key_release(key);
+                               gpgme_key_unref(key);
                                continue;
                        }
                        if (key->revoked) {
                                debug_print("skipping a key, revoked\n");
-                               gpgme_key_release(key);
+                               gpgme_key_unref(key);
                                continue;
                        }
                        if (key->disabled) {
                                debug_print("skipping a key, disabled\n");
-                               gpgme_key_release(key);
+                               gpgme_key_unref(key);
                                continue;
                        }
 
                        if (found_key != NULL) {
-                               gpgme_key_release(key);
+                               gpgme_key_unref(key);
                                gpgme_op_keylist_end(ctx);
                                g_warning("ambiguous specification of secret key '%s'", keyid);
                                privacy_set_error(_("Secret key specification is ambiguous"));
@@ -645,7 +651,7 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
                        }
 
                        found_key = key;
-                }
+               }
                gpgme_op_keylist_end(ctx);
 
                if (found_key == NULL) {
@@ -658,7 +664,7 @@ gboolean sgpgme_setup_signers(gpgme_ctx_t ctx, PrefsAccount *account,
                debug_print("got key (proto %d (pgp %d, smime %d).\n",
                            found_key->protocol, GPGME_PROTOCOL_OpenPGP,
                            GPGME_PROTOCOL_CMS);
-               gpgme_key_release(found_key);
+               gpgme_key_unref(found_key);
 
                if (err) {
                        g_warning("error adding secret key: %s",
@@ -1094,6 +1100,7 @@ check_again:
        err = gpgme_op_keylist_start(ctx, NULL, TRUE);
        if (!err)
                err = gpgme_op_keylist_next(ctx, &key);
+       gpgme_key_unref(key); /* We're not interested in the key itself. */
        gpgme_op_keylist_end(ctx);
        if (gpg_err_code(err) == GPG_ERR_EOF) {
                if (gpgme_get_protocol(ctx) != GPGME_PROTOCOL_CMS) {