[PATCH 3/5] Always use the gpgme_new wrapper in crypt-gpgme.

Werner Koch wk at gnupg.org
Mon Dec 3 07:41:54 UTC 2018


The wrapper is much more convenient and there is no need to sometimes
use gpgme_new directly.  The perceived advantage on not bailing out in
an out-of-core condition is not realistic because other small amounts
of memory are allocated all over mutt anyway and thus function will
terminate the process as well.

This patch also changes the minimum version of gpgme to 1.4.0.  This
is so that we can always pass NULL to functions like gpgme_release.
Further 1.4.0 has new functions which we may soon like to use.
---
 configure.ac  |   4 +--
 crypt-gpgme.c | 111 ++++++++++++++++++++++++----------------------------------
 2 files changed, 47 insertions(+), 68 deletions(-)

diff --git a/configure.ac b/configure.ac
index 8ab8b0c3..892de59f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -129,11 +129,11 @@ AC_ARG_ENABLE(gpgme, AS_HELP_STRING([--enable-gpgme],[Enable GPGME support]),
 
 if test x"$enable_gpgme" = xyes; then
    AC_MSG_RESULT(yes)
-   AM_PATH_GPGME(1.2.0, AC_DEFINE(CRYPT_BACKEND_GPGME, 1,
+   AM_PATH_GPGME(1.4.0, AC_DEFINE(CRYPT_BACKEND_GPGME, 1,
 		 [Defined, if GPGME support is enabled]),
 		 [gpgme_found=no])
    if test x"$gpgme_found" = xno; then
-      AC_MSG_ERROR([*** GPGME not found or version is older than 1.2 ***])
+      AC_MSG_ERROR([*** GPGME not found or version is older than 1.4 ***])
    else
       MUTT_LIB_OBJECTS="$MUTT_LIB_OBJECTS crypt-gpgme.o crypt-mod-pgp-gpgme.o crypt-mod-smime-gpgme.o"
    fi
diff --git a/crypt-gpgme.c b/crypt-gpgme.c
index 55488357..ab0e2eec 100644
--- a/crypt-gpgme.c
+++ b/crypt-gpgme.c
@@ -61,6 +61,7 @@
 # include <sys/resource.h>
 #endif
 
+
 /*
  * Helper macros.
  */
@@ -644,57 +645,50 @@ static gpgme_key_t *create_recipient_set (const char *keylist,
   gpgme_key_t key = NULL;
   gpgme_ctx_t context = NULL;
 
-  err = gpgme_new (&context);
-  if (! err)
-    err = gpgme_set_protocol (context, protocol);
-
-  if (! err)
-    {
-      s = keylist;
-      do {
-	while (*s == ' ')
-	  s++;
-	for (i=0; *s && *s != ' ' && i < sizeof(buf)-1;)
-	  buf[i++] = *s++;
-	buf[i] = 0;
-	if (*buf)
-	  {
-	    if (i>1 && buf[i-1] == '!')
-	      {
-		/* The user selected to override the validity of that
-		   key. */
-		buf[i-1] = 0;
-
-		err = gpgme_get_key (context, buf, &key, 0);
-		if (! err)
-		  key->uids->validity = GPGME_VALIDITY_FULL;
-		buf[i-1] = '!';
-	      }
-	    else
-	      err = gpgme_get_key (context, buf, &key, 0);
+  context = create_gpgme_context ((protocol == GPGME_PROTOCOL_CMS));
+  s = keylist;
+  do {
+    while (*s == ' ')
+      s++;
+    for (i=0; *s && *s != ' ' && i < sizeof(buf)-1;)
+      buf[i++] = *s++;
+    buf[i] = 0;
+    if (*buf)
+      {
+        if (i>1 && buf[i-1] == '!')
+          {
+            /* The user selected to override the validity of that
+               key. */
+            buf[i-1] = 0;
+
+            err = gpgme_get_key (context, buf, &key, 0);
+            if (! err)
+              key->uids->validity = GPGME_VALIDITY_FULL;
+            buf[i-1] = '!';
+          }
+        else
+          err = gpgme_get_key (context, buf, &key, 0);
 
-            safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
-	    if (! err)
-              rset[rset_n++] = key;
-	    else
-	      {
-		mutt_error (_("error adding recipient `%s': %s\n"),
-			    buf, gpgme_strerror (err));
-                rset[rset_n] = NULL;
-                free_recipient_set (&rset);
-		gpgme_release (context);
-		return NULL;
+        safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
+        if (! err)
+          rset[rset_n++] = key;
+        else
+          {
+            mutt_error (_("error adding recipient `%s': %s\n"),
+                        buf, gpgme_strerror (err));
+            rset[rset_n] = NULL;
+            free_recipient_set (&rset);
+            gpgme_release (context);
+            return NULL;
 	      }
-	  }
-      } while (*s);
-    }
+      }
+  } while (*s);
 
   /* NULL terminate.  */
   safe_realloc (&rset, sizeof (*rset) * (rset_n + 1));
   rset[rset_n++] = NULL;
 
-  if (context)
-    gpgme_release (context);
+  gpgme_release (context);
 
   return rset;
 }
@@ -2048,11 +2042,11 @@ static int pgp_gpgme_extract_keys (gpgme_data_t keydata, FILE** fp, int dryrun)
   int rc = -1;
   time_t tt;
 
-  if ((err = gpgme_new (&tmpctx)) != GPG_ERR_NO_ERROR)
-  {
-    dprint (1, (debugfile, "Error creating GPGME context\n"));
-    return rc;
-  }
+#if GPGME_VERSION_NUMBER >= 0x010900 /* 1.9.0 */
+
+#endif
+
+  tmpctx = create_gpgme_context (0);
 
   if (dryrun)
   {
@@ -3717,15 +3711,7 @@ verify_key (crypt_key_t *key)
 
   print_key_info (key->kobj, fp);
 
-  err = gpgme_new (&listctx);
-  if (err)
-    {
-      fprintf (fp, "Internal error: can't create gpgme context: %s\n",
-               gpgme_strerror (err));
-      goto leave;
-    }
-  if ((key->flags & KEYFLAG_ISX509))
-      gpgme_set_protocol (listctx, GPGME_PROTOCOL_CMS);
+  listctx = create_gpgme_context ((key->flags & KEYFLAG_ISX509));
 
   k = key->kobj;
   gpgme_key_ref (k);
@@ -3840,14 +3826,7 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app, int secret)
   if (!pattern)
     return NULL;
 
-  err = gpgme_new (&ctx);
-  if (err)
-    {
-      mutt_error (_("gpgme_new failed: %s"), gpgme_strerror (err));
-      FREE (&pattern);
-      return NULL;
-    }
-
+  ctx = create_gpgme_context (0);
   db = NULL;
   kend = &db;
 
-- 
2.11.0




More information about the Mutt-dev mailing list