[newlib-cygwin] Cygwin: ps: fix compiler warning in ttynam
Corinna Vinschen
corinna@sourceware.org
Wed Feb 26 20:13:00 GMT 2020
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=09981903e6d3a42a23b13cfaed3c9b8b0f0e2f02
commit 09981903e6d3a42a23b13cfaed3c9b8b0f0e2f02
Author: Corinna Vinschen <corinna@vinschen.de>
Date: Wed Feb 26 21:08:51 2020 +0100
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>
Diff:
---
winsup/utils/ps.cc | 19 ++++++++++---------
1 file changed, 10 insertions(+), 9 deletions(-)
diff --git a/winsup/utils/ps.cc b/winsup/utils/ps.cc
index 2307f79..731f72c 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);
More information about the Cygwin-cvs
mailing list