public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: ps: fix compiler warning in ttynam
Date: Wed, 26 Feb 2020 20:13:00 -0000	[thread overview]
Message-ID: <20200226201330.12478.qmail@sourceware.org> (raw)

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);


                 reply	other threads:[~2020-02-26 20:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200226201330.12478.qmail@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).