Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 Nov 1997 10:20:03 -0800 (PST)
From:      Martin Kammerhofer <dada@sbox.tu-graz.ac.at>
To:        freebsd-bugs
Subject:   Re: bin/4947: ps(1) output is not parsable and -Ortprio doesn't work 
Message-ID:  <199711051820.KAA26613@hub.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR bin/4947; it has been noted by GNATS.

From: Martin Kammerhofer <dada@sbox.tu-graz.ac.at>
To: FreeBSD problems <FreeBSD-gnats-submit@freebsd.org>
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);
  }
  
 
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199711051820.KAA26613>