From 24e637b5adf64c7cae2dfe4a7e379710cff562f3 Mon Sep 17 00:00:00 2001 From: Alexey Sheplyakov Date: Fri, 21 Apr 2023 16:33:04 +0400 Subject: [PATCH] top: fixed build error with GCC 13 top/top.c: In function 'keys_window': top/top.c:4471:55: error: '%s' directive output may be truncated writing up to 127 bytes into a region of size 4 [-Werror=format-truncation=] 4471 | snprintf(q->rc.winname, sizeof(q->rc.winname), "%s", name); | ^~ ...... 5631 | if (tmp[0] && tmp[0] != kbd_ESC) win_names(w, tmp); | ~~~ In function 'win_names', inlined from 'keys_window' at top/top.c:5631:46: top/top.c:4471:7: note: 'snprintf' output between 1 and 128 bytes into a destination of size 4 4471 | snprintf(q->rc.winname, sizeof(q->rc.winname), "%s", name); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ top/top.c: In function 'keys_window': top/top.c:4472:49: error: '%s' directive output may be truncated writing up to 127 bytes into a region of size between 0 and 4 [-Werror=format-truncation=] 4472 | snprintf(q->grpname, sizeof(q->grpname), "%d:%s", q->winnum, name); | ^~ ...... 5631 | if (tmp[0] && tmp[0] != kbd_ESC) win_names(w, tmp); | ~~~ In function 'win_names', inlined from 'keys_window' at top/top.c:5631:46: top/top.c:4472:4: note: 'snprintf' output between 3 and 140 bytes into a destination of size 6 4472 | snprintf(q->grpname, sizeof(q->grpname), "%d:%s", q->winnum, name); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors 5628 if (ALTCHKw) { 5629 char tmp[SMLBUFSIZ]; 5630 STRLCPY(tmp, ioline(fmtmk(N_fmt(NAME_windows_fmt), w->rc.winname))); 5631 if (tmp[0] && tmp[0] != kbd_ESC) win_names(w, tmp); 5632 } 5633 break; $ grep -r -n -E 'define\s+SMLBUFSIZ\b' top/top.h:139:#define SMLBUFSIZ 128 4467 static void win_names (WIN_t *q, const char *name) { 4468 /* note: sprintf/snprintf results are "undefined" when src==dst, 4469 according to C99 & POSIX.1-2001 (thanks adc) */ 4470 if (q->rc.winname != name) 4471 snprintf(q->rc.winname, sizeof(q->rc.winname), "%s", name); 4472 snprintf(q->grpname, sizeof(q->grpname), "%d:%s", q->winnum, name); 4473 } // end: win_names $ grep -e 'winname' 'top/top.h' char winname [WINNAMSIZ], // name for the window, user changeable It looks like the above code indeed writes up to 128 bytes into a 4-byte buffer. To avoid the warning 1) Specify the maximal string length in the format string, like "%.4s". 2) Add one more byte to winname (for a terminating '\0' character). ALTBUG: #45927 l --- top/top.c | 9 +++++++-- top/top.h | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/top/top.c b/top/top.c index e64c8660..8636ac20 100644 --- a/top/top.c +++ b/top/top.c @@ -4462,16 +4462,21 @@ static void whack_terminal (void) { /*###### Windows/Field Groups support #################################*/ +#define xstr(s) str(s) +#define str(s) #s + /* * Value a window's name and make the associated group name. */ static void win_names (WIN_t *q, const char *name) { /* note: sprintf/snprintf results are "undefined" when src==dst, according to C99 & POSIX.1-2001 (thanks adc) */ if (q->rc.winname != name) - snprintf(q->rc.winname, sizeof(q->rc.winname), "%s", name); - snprintf(q->grpname, sizeof(q->grpname), "%d:%s", q->winnum, name); + snprintf(q->rc.winname, sizeof(q->rc.winname), "%." xstr(WINNAMSIZ) "s", name); + snprintf(q->grpname, sizeof(q->grpname), "%d:%." xstr(WINNAMSIZ) "s", q->winnum, name); } // end: win_names +#undef str +#undef xstr /* * This guy just resets (normalizes) a single window diff --git a/top/top.h b/top/top.h index 499d9957..de1e0bf6 100644 --- a/top/top.h +++ b/top/top.h @@ -378,7 +378,7 @@ typedef struct RCW_t { // the 'window' portion of an rcfile msgsclr, // " in msgs/pmts headclr, // " in cols head taskclr; // " in task rows - char winname [WINNAMSIZ], // name for the window, user changeable + char winname [WINNAMSIZ+1], // name for the window, user changeable fieldscur [PFLAGSSIZ]; // the fields for display & their order } RCW_t; @@ -427,7 +427,7 @@ typedef struct WIN_t { capclr_rowhigh [CLRBUFSIZ], // are only used when this capclr_rownorm [CLRBUFSIZ], // window is the 'Curwin'! cap_bold [CAPBUFSIZ], // support for View_NOBOLD toggle - grpname [GRPNAMSIZ], // window number:name, printable + grpname [GRPNAMSIZ+1], // window number:name, printable #ifdef USE_X_COLHDR columnhdr [ROWMINSIZ], // column headings for procflgs #else -- 2.33.7