]> granicus.if.org Git - neomutt/commitdiff
pgp_getkeybystr: Pull key matching out of the address match loop.
authorKevin McCarthy <kevin@8t8.us>
Mon, 19 Jan 2015 23:58:38 +0000 (15:58 -0800)
committerKevin McCarthy <kevin@8t8.us>
Mon, 19 Jan 2015 23:58:38 +0000 (15:58 -0800)
Since the key is invariant inside the address loop, there is no need to
match against it with each address.

All the keys should have at least one address record (see bb3b01f41ed2),
but in case a record was malformed, add a check for that to keep the
same logic.

pgpkey.c

index 26be753b6957d715e6437010340cbf1560249cc0..bd3265eb6d7944c97c35fbf3e3b6645084110570 100644 (file)
--- a/pgpkey.c
+++ b/pgpkey.c
@@ -955,30 +955,45 @@ pgp_key_t pgp_getkeybystr (char *p, short abilities, pgp_ring_t keyring)
   pl = (!mutt_strncasecmp (p, "0x", 2) ? p + 2 : p);
   ps = (mutt_strlen (pl) == 16 ? pl + 8 : pl);
 
-  /* If ps != pl it means a long ID (or name of 16 characters) was given, do
-   * not attempt to match short IDs then. Also, it is unnecessary to try to
-   * match pl against long IDs if ps == pl as pl could not be a long ID. */
-
   for (k = keys; k; k = kn)
   {
     kn = k->next;
     if (abilities && !(k->flags & abilities))
       continue;
 
+    /* This shouldn't happen, but keys without any addresses aren't selectable
+     * in pgp_select_key().
+     */
+    if (!k->address)
+      continue;
+
     match = 0;
 
-    for (a = k->address; a; a = a->next)
+    dprint (5, (debugfile, "pgp_getkeybystr: matching \"%s\" against key %s:\n",
+                p, pgp_long_keyid (k)));
+
+    /* If ps != pl it means a long ID (or name of 16 characters) was given, do
+     * not attempt to match short IDs then. Also, it is unnecessary to try to
+     * match pl against long IDs if ps == pl as pl could not be a long ID. */
+    if (!*p ||
+        (ps != pl && mutt_strcasecmp (pl, pgp_long_keyid (k)) == 0) ||
+        (ps == pl && mutt_strcasecmp (ps, pgp_short_keyid (k)) == 0))
     {
-      dprint (5, (debugfile, "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\": ",
-                  p, pgp_long_keyid (k), NONULL (a->addr)));
-      if (!*p ||
-          (ps != pl && mutt_strcasecmp (pl, pgp_long_keyid (k)) == 0) ||
-          (ps == pl && mutt_strcasecmp (ps, pgp_short_keyid (k)) == 0) ||
-         mutt_stristr (a->addr, p))
+      dprint (5, (debugfile, "\t\tmatch.\n"));
+      match = 1;
+    }
+    else
+    {
+      for (a = k->address; a; a = a->next)
       {
-       dprint (5, (debugfile, "match.\n"));
-       match = 1;
-       break;
+        dprint (5, (debugfile, "pgp_getkeybystr: matching \"%s\" against key %s, \"%s\":\n",
+                    p, pgp_long_keyid (k), NONULL (a->addr)));
+        if (mutt_stristr (a->addr, p))
+        {
+          dprint (5, (debugfile, "\t\tmatch.\n"));
+          match = 1;
+          break;
+        }
       }
     }