From owner-freebsd-bugs Wed Nov 5 10:20:08 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.7/8.8.7) id KAA26634 for bugs-outgoing; Wed, 5 Nov 1997 10:20:08 -0800 (PST) (envelope-from owner-freebsd-bugs) Received: (from gnats@localhost) by hub.freebsd.org (8.8.7/8.8.7) id KAA26613; Wed, 5 Nov 1997 10:20:03 -0800 (PST) (envelope-from gnats) Date: Wed, 5 Nov 1997 10:20:03 -0800 (PST) Message-Id: <199711051820.KAA26613@hub.freebsd.org> To: freebsd-bugs Cc: From: Martin Kammerhofer Subject: Re: bin/4947: ps(1) output is not parsable and -Ortprio doesn't work Reply-To: Martin Kammerhofer Sender: owner-freebsd-bugs@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk The following reply was made to PR bin/4947; it has been noted by GNATS. From: Martin Kammerhofer To: FreeBSD problems Cc: Subject: Re: bin/4947: ps(1) output is not parsable and -Ortprio doesn't work Date: Wed, 5 Nov 1997 19:15:10 +0100 (CET) On Wed, 5 Nov 1997, Bill Fenner wrote: > >+ default: > >+ strncpy(str, "?", 2); > > I'd do something like "%d:%d", prtp->type, prio (taking care, of course, > not to overflow) -- someone someday is going to be *really* frustrated that > ps just prints a "?". > You're right. Although currently 0 <= type <= 2 and 0 <= prio <= 31 holds, it isn't guaranteed to be that way for all times. How about this (relative to the last patch)? Index: print.c =================================================================== RCS file: /home/dada/cvsroot/bin/ps/print.c,v retrieving revision 1.3 diff -u -K -r1.3 print.c --- print.c 1997/11/05 08:59:33 1.3 +++ print.c 1997/11/05 18:05:12 @@ -666,27 +666,26 @@ VAR *v; struct rtprio *prtp; char str[8]; - unsigned prio; + unsigned type, prio; v = ve->var; prtp = (struct rtprio *) ((char *)KI_PROC(k) + v->off); prio = prtp->prio; - if (prio > 99) - prio = 99; /* ensure that 'str' can *never* overflow */ - switch (prtp->type) { + switch (type = prtp->type) { case RTP_PRIO_REALTIME: - sprintf(str, "real:%u", prio); + snprintf(str, sizeof(str), "real:%u", prio); break; case RTP_PRIO_NORMAL: - strncpy(str, "normal", 7); + strncpy(str, "normal", sizeof(str)); break; case RTP_PRIO_IDLE: - sprintf(str, "idle:%u", prio); + snprintf(str, sizeof(str), "idle:%u", prio); break; default: - strncpy(str, "?", 2); + snprintf(str, sizeof(str), "%u:%u", type, prio); break; } + str[sizeof(str)-1] = '\0'; (void)printf("%*s", v->width, str); }