diff options
author | Corinna Vinschen <corinna@vinschen.de> | 2020-02-26 23:08:51 +0300 |
---|---|---|
committer | Corinna Vinschen <corinna@vinschen.de> | 2020-02-26 23:08:51 +0300 |
commit | 09981903e6d3a42a23b13cfaed3c9b8b0f0e2f02 (patch) | |
tree | 3c84f3525748654cac1bbf04f3ebfed97ade9af1 /winsup/utils/ps.cc | |
parent | 0a37e9f0bc24c6d326816e6686c4eaa25b4fd83e (diff) |
Cygwin: ps: fix compiler warning in ttynam
The helper function ttynam creates a tty name by using sprintf wrongly
on a pretty short buffer. The foramt string only specifies a minimum
field length, not a maximum field length, so gcc-9.2.0 complains:
ps.cc:101:23: warning: 'sprintf' may write a terminating nul past the
end of the destination [-Wformat-overflow=]
Fix this thoroughly by specifying a maximum field width as well as by
using snprintf with a fixed buffer length. Also, drop using a static
buffer in favor of using a buffer in the caller.
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
Diffstat (limited to 'winsup/utils/ps.cc')
-rw-r--r-- | winsup/utils/ps.cc | 19 |
1 files changed, 10 insertions, 9 deletions
diff --git a/winsup/utils/ps.cc b/winsup/utils/ps.cc index 2307f7955..731f72c18 100644 --- a/winsup/utils/ps.cc +++ b/winsup/utils/ps.cc @@ -88,17 +88,17 @@ to_time_t (FILETIME *ptr) } static const char * -ttynam (int ntty) +ttynam (int ntty, char buf[9]) { - static char buf[9]; char buf0[9]; + if (ntty < 0) strcpy (buf0, "?"); else if (ntty & 0xffff0000) - sprintf (buf0, "cons%d", ntty & 0xff); + snprintf (buf0, 9, "cons%d", ntty & 0xff); else - sprintf (buf0, "pty%d", ntty); - sprintf (buf, " %-7s", buf0); + snprintf (buf0, 9, "pty%d", ntty); + snprintf (buf, 9, " %-7.7s", buf0); return buf; } @@ -358,6 +358,7 @@ main (int argc, char *argv[]) } char uname[128]; + char ttyname[9]; if (fflag) { @@ -373,13 +374,13 @@ main (int argc, char *argv[]) } if (sflag) - printf (dfmt, p->pid, ttynam (p->ctty), start_time (p), pname); + printf (dfmt, p->pid, ttynam (p->ctty, ttyname), start_time (p), pname); else if (fflag) - printf (ffmt, uname, p->pid, p->ppid, ttynam (p->ctty), start_time (p), - pname); + printf (ffmt, uname, p->pid, p->ppid, ttynam (p->ctty, ttyname), + start_time (p), pname); else if (lflag) printf (lfmt, status, p->pid, p->ppid, p->pgid, - p->dwProcessId, ttynam (p->ctty), + p->dwProcessId, ttynam (p->ctty, ttyname), p->version >= EXTERNAL_PINFO_VERSION_32_BIT ? p->uid32 : p->uid, start_time (p), pname); |