Fix a few memory leaks when enumerating keys with GpgME.
[claws.git] / src / plugins / pgpcore / select-keys.c
index fad1e7aa87bc0fc70e8bf3a4bc4bcc3d3aa7c029..47e4b66fd9b461f79d3a0f9e9f6e5f96829d4707 100644 (file)
@@ -1,5 +1,5 @@
 /* select-keys.c - GTK+ based key selection
- *      Copyright (C) 2001-2012 Werner Koch (dd9jn) and the Claws Mail team
+ * Copyright (C) 2001-2016 Werner Koch (dd9jn) and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify        
  * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -168,7 +167,7 @@ static void
 destroy_key (gpointer data)
 {
     gpgme_key_t key = data;
-    gpgme_key_release (key);
+    gpgme_key_unref (key);
 }
 
 static void
@@ -290,7 +289,7 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
 
     err = gpgme_op_keylist_start (ctx, pattern, 0);
     if (err) {
-        debug_print ("** gpgme_op_keylist_start(%s) failed: %s",
+        debug_print ("** gpgme_op_keylist_start(%s) failed: %s\n",
                      pattern != NULL ? pattern : "NULL", gpgme_strerror (err));
         sk->select_ctx = NULL;
         gpgme_release(ctx);
@@ -321,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);
@@ -336,7 +337,7 @@ fill_clist (struct select_keys_s *sk, const char *pattern, gpgme_protocol_t prot
 
     debug_print ("%% %s:%d:  ready\n", __FILE__ ,__LINE__ );
     if (gpgme_err_code(err) != GPG_ERR_EOF) {
-        debug_print ("** gpgme_op_keylist_next failed: %s",
+        debug_print ("** gpgme_op_keylist_next failed: %s\n",
                      gpgme_strerror (err));
         gpgme_op_keylist_end(ctx);
     }
@@ -345,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;
 }
 
 
@@ -505,7 +510,7 @@ select_btn_cb (GtkWidget *widget, gpointer data)
 
     cm_return_if_fail (sk);
     if (!sk->clist->selection) {
-        debug_print ("** nothing selected");
+        debug_print ("** nothing selected\n");
         return;
     }
     row = GPOINTER_TO_INT(sk->clist->selection->data);
@@ -531,7 +536,7 @@ select_btn_cb (GtkWidget *widget, gpointer data)
         if ( uid->validity < GPGME_VALIDITY_FULL ) {
             use_key = use_untrusted(key, uid, sk->proto);
             if (!use_key) {
-                debug_print ("** Key untrusted, will not encrypt");
+                debug_print ("** Key untrusted, will not encrypt\n");
                 return;
             }
         }