Fix a few memory leaks when enumerating keys with GpgME.
[claws.git] / src / plugins / pgpcore / sgpgme.c
index 2290526a7541e5cd4b0faf03eaa8b39a7a09a0df..0579f42056e51f609ce5704830efeb0464f2c63e 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) {