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>
This commit is contained in:
		| @@ -88,17 +88,17 @@ to_time_t (FILETIME *ptr) | |||||||
| } | } | ||||||
|  |  | ||||||
| static const char * | static const char * | ||||||
| ttynam (int ntty) | ttynam (int ntty, char buf[9]) | ||||||
| { | { | ||||||
|   static char buf[9]; |  | ||||||
|   char buf0[9]; |   char buf0[9]; | ||||||
|  |  | ||||||
|   if (ntty < 0) |   if (ntty < 0) | ||||||
|     strcpy (buf0, "?"); |     strcpy (buf0, "?"); | ||||||
|   else if (ntty & 0xffff0000) |   else if (ntty & 0xffff0000) | ||||||
|     sprintf (buf0, "cons%d", ntty & 0xff); |     snprintf (buf0, 9, "cons%d", ntty & 0xff); | ||||||
|   else |   else | ||||||
|     sprintf (buf0, "pty%d", ntty); |     snprintf (buf0, 9, "pty%d", ntty); | ||||||
|   sprintf (buf, " %-7s", buf0); |   snprintf (buf, 9, " %-7.7s", buf0); | ||||||
|   return buf; |   return buf; | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -358,6 +358,7 @@ main (int argc, char *argv[]) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
|       char uname[128]; |       char uname[128]; | ||||||
|  |       char ttyname[9]; | ||||||
|  |  | ||||||
|       if (fflag) |       if (fflag) | ||||||
| 	{ | 	{ | ||||||
| @@ -373,13 +374,13 @@ main (int argc, char *argv[]) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
|       if (sflag) |       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) |       else if (fflag) | ||||||
| 	printf (ffmt, uname, p->pid, p->ppid, ttynam (p->ctty), start_time (p), | 	printf (ffmt, uname, p->pid, p->ppid, ttynam (p->ctty, ttyname), | ||||||
| 		pname); | 		start_time (p), pname); | ||||||
|       else if (lflag) |       else if (lflag) | ||||||
| 	printf (lfmt, status, p->pid, p->ppid, p->pgid, | 	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, | 		p->version >= EXTERNAL_PINFO_VERSION_32_BIT ? p->uid32 : p->uid, | ||||||
| 		start_time (p), pname); | 		start_time (p), pname); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user