From 19c4471e5632ff7de8d8e6657cc2a77174db7a24 Mon Sep 17 00:00:00 2001 From: Kevin McCarthy Date: Mon, 19 Jan 2015 15:58:38 -0800 Subject: [PATCH] pgp_getkeybystr: Pull key matching out of the address match loop. 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 | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/pgpkey.c b/pgpkey.c index 26be753b..bd3265eb 100644 --- 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; + } } } -- 2.40.0