#983649 menu.c: use "snprintf()" instead of "sprintf()"

Package:
src:nn
Source:
nn
Submitter:
Bjarni Ingi Gislason
Date:
2021-02-28 01:57:04 UTC
Severity:
normal
Tags:
#983649#5
Date:
2021-02-28 01:53:27 UTC
From:
To:
Dear Maintainer,

  Add the size of arrays to the arguments of some functions.

  Define named constants for the length of some arrays.

  Use "snprintf()" instead of "sprintf()".

Signed-off-by: Bjarni Ingi Gislason <bjarniig@rhi.hi.is>
---
 menu.c | 53 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 31 insertions(+), 22 deletions(-)

diff --git a/menu.c b/menu.c
index c33a156..22a4b19 100644
--- a/menu.c
+++ b/menu.c
@@ -37,7 +37,8 @@ static article_number next_root_article(register article_number root);
 static void     set_root_if_closed(void);
 static article_number thread_counters(article_number art);
 static void     cursor_at_id(void);
-static attr_type closed_attr(register struct menu_info * mi, char *cbuf);
+static attr_type closed_attr(register struct menu_info * mi, char *cbuf,
+			     size_t ncbuf );
 static void     mark(void);
 static void     toggle(void);
 static int      do_auto_kill(void);
@@ -92,8 +93,9 @@ int             select_on_sender = 0;	/* find command selects on sender */
 int             auto_select_subject = 0;	/* auto select articles with
 						 * same subj. */
 int             auto_read_limit = 0;	/* ignore auto_read_mode if less
-					 * articles */
+					   * articles */

+extern const size_t   NDELAYED_MSG;
 extern char     delayed_msg[];	/* give to msg() after redraw */

 int             flush_typeahead = 0;
@@ -273,9 +275,11 @@ cursor_at_id(void)
 }

 static          attr_type
-closed_attr(register struct menu_info * mi, char *cbuf)
+closed_attr(register struct menu_info * mi, char *cbuf, size_t ncbuf )
 {
-    char            lft[10], sel[10], unr[10];
+    const size_t    NLFT = 10, NSEL = 10, NUNR = 10;
+    char            lft[NLFT], sel[NSEL], unr[NUNR];
+
     attr_type       cattr;

     if (mi->mi_total == mi->mi_left)
@@ -293,12 +297,12 @@ closed_attr(register struct menu_info * mi, char *cbuf)

     lft[0] = sel[0] = unr[0] = NUL;
     if (mi->mi_left && mi->mi_left < mi->mi_unread)
-	sprintf(lft, "%d,", mi->mi_left);
+	snprintf(lft, NLFT, "%d,", mi->mi_left);
     if (mi->mi_selected && mi->mi_selected < mi->mi_unread)
-	sprintf(sel, "%d/", mi->mi_selected);
+	snprintf(sel, NSEL, "%d/", mi->mi_selected);
     if (mi->mi_unread && mi->mi_unread < mi->mi_total)
-	sprintf(unr, "%d:", mi->mi_unread);
-    sprintf(cbuf, "%s%s%s%d", lft, sel, unr, mi->mi_total);
+	snprintf(unr, NUNR, "%d:", mi->mi_unread);
+    snprintf(cbuf, ncbuf, "%s%s%s%d", lft, sel, unr, mi->mi_total);

     return cattr;
 }
@@ -312,7 +316,8 @@ mark(void)
     register struct menu_info *mi;
     int             lno, lnum, lsubj, lname;
     int             pad;
-    char            cbuf[80];
+#define    NCBUF  80
+    char            cbuf[NCBUF];
     attr_type       cattr = 0;

     ah = articles[firsta + cura];
@@ -325,7 +330,7 @@ mark(void)
 	lno = firstl + ah->menu_line;
 	gotoxy(0, lno);
 	tputc(ident[mi->mi_art_id]);
-	cattr = closed_attr(mi, cbuf);
+	cattr = closed_attr(mi, cbuf, NCBUF);
 	goto print_line;
     }
     if (cura < 0 || cura > numa)
@@ -335,7 +340,8 @@ mark(void)

     if (ah->flag & A_CLOSED) {
 	struct menu_info old;
-	char            oldctr[80];
+#define        NOLDCTR  80
+	char            oldctr[NOLDCTR];

 	mi = &menu_info[ah->menu_line];
 	old = *mi;
@@ -345,12 +351,11 @@ mark(void)
 	    old.mi_left == mi->mi_left &&
 	    old.mi_unread == mi->mi_unread)
 	    return;
-
-	cattr = closed_attr(mi, cbuf);
+	cattr = closed_attr(mi, cbuf, NCBUF);

 	if (!slow_mode)
 	    goto print_line;
-	closed_attr(&old, oldctr);
+	closed_attr(&old, oldctr, NOLDCTR);
 	if (strcmp(cbuf, oldctr))
 	    goto print_line;
 	last_attr = cattr;
@@ -811,7 +816,7 @@ show_articles(void)
 	}

 	if (again > 1)
-	    sprintf(delayed_msg, "Showing %ld articles again", again);
+	    snprintf(delayed_msg, NDELAYED_MSG, "Showing %d articles again", again);
     } while (again);

     return MC_READGROUP;
@@ -868,6 +873,7 @@ loop:
 	cur_key = c;
 	map = key_map[c];
     }
+
     if (s_hangup)
 	map = K_QUIT;

@@ -915,7 +921,8 @@ char           *
 pct(long start, long end, long first, long last)
 {
     long            n = end - start;
-    static char     buf[16];
+#define    NBUF  16
+    static char     buf[NBUF];
     char           *fmt;

     if (first <= start || n <= 0) {
@@ -929,7 +936,7 @@ pct(long start, long end, long first, long last)
 	fmt = "%d%%";
     }

-    sprintf(buf, fmt, ((last - start) * 100) / n);
+    snprintf(buf, NBUF, fmt, ((last - start) * 100) / n);
     return buf;
 }

@@ -1103,7 +1110,8 @@ menu(fct_type print_header)
     article_number  o_firsta, temp1 = 0, temp2;
     int             o_mode;	/* for recursive calls */
     static int      menu_level = 0;
-    char            purpose[80], pr_fmt[60];
+#define    NPR_FMT 60
+    char            purpose[80], pr_fmt[NPR_FMT];
     article_number  elim_list[3];
     int             entry_check;
     int             auto_read;
@@ -1136,7 +1144,7 @@ menu(fct_type print_header)
 	auto_read = 0;
     }

-    sprintf(pr_fmt,
+    snprintf(pr_fmt, NPR_FMT,
 	    menu_level == 1 ?
 	    "\1\2-- SELECT %s-----%%s-----\1" :
 	    "\1\2-- SELECT %s-----%%s-----<%s%d>--\1",
@@ -1218,7 +1226,7 @@ empty_menu_hack:		/* do: "s_keyboard=1; goto empty_menu_hack;" */
 	    if (purpose[0])
 		strcpy(delayed_msg, purpose);
 	    else
-		sprintf(delayed_msg, "Entering %s, %ld articles",
+		snprintf(delayed_msg, NDELAYED_MSG, "Entering %s, %ld articles",
 			current_group->group_name, (long) n_articles);
 	    goto do_auto_read;
 	}
@@ -2454,7 +2462,8 @@ int
 alt_command(void)
 {
     int             ok_val, macro_cmd;
-    char           *cmd, brkchars[10];
+#define    NBRKCHARS 10
+    char           *cmd, brkchars[NBRKCHARS];

     if (get_from_macro)
 	ok_val = AC_UNCHANGED;
@@ -2465,7 +2474,7 @@ alt_command(void)

 again:

-    sprintf(brkchars, "?%c ", erase_key);
+    snprintf(brkchars, NBRKCHARS, "?%c ", erase_key);

     cmd = get_s(NONE, NONE, brkchars, alt_completion);
     if (cmd == NULL ||