From: Kevin McCarthy <kevin@8t8.us>
Date: Wed, 30 Nov 2016 01:44:37 +0000 (-0800)
Subject: Add mutt_array_size macro, change interactive_check_cert() to use it. (see #3899)
X-Git-Tag: neomutt-20170113~15^2~13
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a07f30a7312811085462b0354f23c3a9c54f8209;p=neomutt

Add mutt_array_size macro, change interactive_check_cert() to use it. (see #3899)

While I have reservations about the construct, it does make the
interactive_check_cert() menu->max and part loop less fragile.
---

diff --git a/lib.h b/lib.h
index 109f5dd6c..7328bbcac 100644
--- a/lib.h
+++ b/lib.h
@@ -83,6 +83,10 @@
 # define MAX(a,b) ((a) < (b) ? (b) : (a))
 # define MIN(a,b) ((a) < (b) ? (a) : (b))
 
+/* Use this with care.  If the compiler can't see the array
+ * definition, it obviously won't produce a correct result. */
+#define mutt_array_size(x)  (sizeof (x) / sizeof ((x)[0]))
+
 /* For mutt_format_string() justifications */
 /* Making left 0 and center -1 is of course completely nonsensical, but
  * it retains compatibility for any patches that call mutt_format_string.
diff --git a/mutt_ssl.c b/mutt_ssl.c
index aa762ec06..696aa251a 100644
--- a/mutt_ssl.c
+++ b/mutt_ssl.c
@@ -1029,7 +1029,7 @@ static int interactive_check_cert (X509 *cert, int idx, int len)
   int done, row, i;
   FILE *fp;
 
-  menu->max = 23;
+  menu->max = mutt_array_size (part) * 2 + 9;
   menu->dialog = (char **) safe_calloc (1, menu->max * sizeof (char *));
   for (i = 0; i < menu->max; i++)
     menu->dialog[i] = (char *) safe_calloc (1, SHORT_STRING * sizeof (char));
@@ -1038,7 +1038,7 @@ static int interactive_check_cert (X509 *cert, int idx, int len)
   strfcpy (menu->dialog[row], _("This certificate belongs to:"), SHORT_STRING);
   row++;
   x509_subject = X509_get_subject_name (cert);
-  for (i = 0; i < 7; i++)
+  for (i = 0; i < mutt_array_size (part); i++)
     snprintf (menu->dialog[row++], SHORT_STRING, "   %s",
               x509_get_part (x509_subject, part[i]));
 
@@ -1046,7 +1046,7 @@ static int interactive_check_cert (X509 *cert, int idx, int len)
   strfcpy (menu->dialog[row], _("This certificate was issued by:"), SHORT_STRING);
   row++;
   x509_issuer = X509_get_issuer_name (cert);
-  for (i = 0; i < 7; i++)
+  for (i = 0; i < mutt_array_size (part); i++)
     snprintf (menu->dialog[row++], SHORT_STRING, "   %s",
               x509_get_part (x509_issuer, part[i]));