From b60fc972c4cae3366f773e6ff2a650627c280e5b Mon Sep 17 00:00:00 2001 From: Michael Elkins Date: Sun, 12 Sep 2010 10:39:56 -0700 Subject: [PATCH] fix crash when index contains 0 keys --- smime.c | 46 ++++++++++++++++------------------------------ 1 file changed, 16 insertions(+), 30 deletions(-) diff --git a/smime.c b/smime.c index 2068fcfe3..ef7efc126 100644 --- a/smime.c +++ b/smime.c @@ -352,9 +352,8 @@ static void smime_entry (char *s, size_t l, MUTTMENU * menu, int num) char* smime_ask_for_key (char *prompt, char *mailbox, short public) { char *fname; - smime_id *Table; - long cert_num; /* Will contain the number of certificates. - * To be able to get it, the .index file will be read twice... */ + smime_id *table = 0; + int table_count; char index_file[_POSIX_PATH_MAX]; FILE *index; char buf[LONG_STRING]; @@ -370,19 +369,6 @@ char* smime_ask_for_key (char *prompt, char *mailbox, short public) snprintf(index_file, sizeof (index_file), "%s/.index", public ? NONULL(SmimeCertificates) : NONULL(SmimeKeys)); - index = fopen(index_file, "r"); - if (index == NULL) - { - mutt_perror (index_file); - return NULL; - } - /* Count Lines */ - cert_num = 0; - while (!feof(index)) { - if (fgets(buf, sizeof(buf), index)) cert_num++; - } - safe_fclose (&index); - FOREVER { *qry = 0; @@ -401,7 +387,7 @@ char* smime_ask_for_key (char *prompt, char *mailbox, short public) } /* Read Entries */ cur = 0; - Table = safe_calloc(cert_num, sizeof (smime_id)); + table_count = 0; while (!feof(index)) { numFields = fscanf (index, MUTT_FORMAT(STRING) " %x.%i " MUTT_FORMAT(STRING), fields[0], &hash, &hash_suffix, fields[2]); @@ -416,12 +402,14 @@ char* smime_ask_for_key (char *prompt, char *mailbox, short public) !mutt_stristr(fields[2], qry)) continue; - Table[cur].hash = hash; - Table[cur].suffix = hash_suffix; - strncpy(Table[cur].email, fields[0], sizeof(Table[cur].email)); - strncpy(Table[cur].nick, fields[2], sizeof(Table[cur].nick)); - Table[cur].trust = *fields[4]; - Table[cur].public = public; + ++table_count; + safe_realloc(&table, sizeof(smime_id) * table_count); + table[cur].hash = hash; + table[cur].suffix = hash_suffix; + strncpy(table[cur].email, fields[0], sizeof(table[cur].email)); + strncpy(table[cur].nick, fields[2], sizeof(table[cur].nick)); + table[cur].trust = *fields[4]; + table[cur].public = public; cur++; } @@ -442,7 +430,7 @@ char* smime_ask_for_key (char *prompt, char *mailbox, short public) menu->max = cur; menu->make_entry = smime_entry; menu->help = helpstr; - menu->data = Table; + menu->data = table; menu->title = title; /* sorting keys might be done later - TODO */ @@ -454,7 +442,7 @@ char* smime_ask_for_key (char *prompt, char *mailbox, short public) switch (mutt_menuLoop (menu)) { case OP_GENERIC_SELECT_ENTRY: cur = menu->current; - hash = 1; + hash = 1; done = 1; break; case OP_EXIT: @@ -463,14 +451,12 @@ char* smime_ask_for_key (char *prompt, char *mailbox, short public) break; } } - if (hash) { - fname = safe_malloc(13); /* Hash + '.' + Suffix + \0 */ - sprintf(fname, "%.8x.%i", Table[cur].hash, Table[cur].suffix); - } + if (table_count && hash) + safe_asprintf(&fname, "%.8x.%i", table[cur].hash, table[cur].suffix); else fname = NULL; mutt_menuDestroy (&menu); - FREE (&Table); + FREE (&table); set_option (OPTNEEDREDRAW); if (fname) return fname; -- 2.40.0