Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Jan 2011 21:49:47 +0000
From:      Alexander Best <arundel@freebsd.org>
To:        Konstantin Belousov <kib@FreeBSD.org>
Cc:        svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org
Subject:   Re: svn commit: r216955 - head/usr.sbin/rtprio
Message-ID:  <20110106214947.GA53760@freebsd.org>
In-Reply-To: <201101041413.p04EDA4f038360@svn.freebsd.org>
References:  <201101041413.p04EDA4f038360@svn.freebsd.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--3V7upXqbjpZ4EhLz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Tue Jan  4 11, Konstantin Belousov wrote:
> Author: kib
> Date: Tue Jan  4 14:13:09 2011
> New Revision: 216955
> URL: http://svn.freebsd.org/changeset/base/216955
> 
> Log:
>   Make the parsing of the integer arguments for rtprio(1)/idprio(1) stricter.
>   Style.

here's a patch with some changes. you might want to cherry pick from it. ;)

cheers.
alex

>   
>   Based on submission by:	Eitan Adler <lists eitanadler com>, keramida
>   Reviewed by:	jhb, keramida
>   MFC after:	1 week
> 
> Modified:
>   head/usr.sbin/rtprio/rtprio.c
> 
> Modified: head/usr.sbin/rtprio/rtprio.c
> ==============================================================================
> --- head/usr.sbin/rtprio/rtprio.c	Tue Jan  4 13:16:28 2011	(r216954)
> +++ head/usr.sbin/rtprio/rtprio.c	Tue Jan  4 14:13:09 2011	(r216955)
> @@ -37,31 +37,31 @@ __FBSDID("$FreeBSD$");
>  
>  #include <sys/param.h>
>  #include <sys/rtprio.h>
> -#include <sys/errno.h>
>  
>  #include <ctype.h>
>  #include <err.h>
> +#include <errno.h>
>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <string.h>
>  #include <unistd.h>
>  
> -static void usage();
> +static int parseint(const char *, const char *);
> +static void usage(void);
>  
>  int
> -main(argc, argv)
> -	int     argc;
> -	char  **argv;
> +main(int argc, char *argv[])
>  {
> -	char   *p;
> -	int     proc = 0;
>  	struct rtprio rtp;
> +	char *p;
> +	pid_t proc;
>  
>  	/* find basename */
>  	if ((p = rindex(argv[0], '/')) == NULL)
>  		p = argv[0];
>  	else
>  		++p;
> +	proc = 0;
>  
>  	if (!strcmp(p, "rtprio"))
>  		rtp.type = RTP_PRIO_REALTIME;
> @@ -70,12 +70,12 @@ main(argc, argv)
>  
>  	switch (argc) {
>  	case 2:
> -		proc = abs(atoi(argv[1]));	/* Should check if numeric
> -						 * arg! */
> +		proc = parseint(argv[1], "pid");
> +		proc = abs(proc);
>  		/* FALLTHROUGH */
>  	case 1:
>  		if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
> -			err(1, "%s", argv[0]);
> +			err(1, "RTP_LOOKUP");
>  		printf("%s: ", p);
>  		switch (rtp.type) {
>  		case RTP_PRIO_REALTIME:
> @@ -103,19 +103,17 @@ main(argc, argv)
>  					usage();
>  					break;
>  				}
> -			} else {
> -				rtp.prio = atoi(argv[1]);
> -			}
> +			} else
> +				rtp.prio = parseint(argv[1], "priority");
>  		} else {
>  			usage();
>  			break;
>  		}
>  
>  		if (argv[2][0] == '-')
> -			proc = -atoi(argv[2]);
> -
> +			proc = parseint(argv[2] + 1, "pid");
>  		if (rtprio(RTP_SET, proc, &rtp) != 0)
> -			err(1, "%s", argv[0]);
> +			err(1, "RTP_SET");
>  
>  		if (proc == 0) {
>  			execvp(argv[2], &argv[2]);
> @@ -123,12 +121,28 @@ main(argc, argv)
>  		}
>  		exit(0);
>  	}
> -	exit (1);
> +	exit(1);
> +}
> +
> +static int
> +parseint(const char *str, const char *errname)
> +{
> +	char *endp;
> +	long res;
> +
> +	errno = 0;
> +	res = strtol(str, &endp, 10);
> +	if (errno != 0 || endp == str || *endp != '\0')
> +		err(1, "%s must be a number", errname);
> +	if (res >= INT_MAX)
> +		err(1, "Integer overflow parsing %s", errname);
> +	return (res);
>  }
>  
>  static void
> -usage()
> +usage(void)
>  {
> +
>  	(void) fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
>  		"usage: [id|rt]prio",
>  		"       [id|rt]prio [-]pid",

-- 
a13x

--3V7upXqbjpZ4EhLz
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="rtprio.diff3"

diff --git a/usr.sbin/rtprio/rtprio.c b/usr.sbin/rtprio/rtprio.c
index 38dade8..e28eee8 100644
--- a/usr.sbin/rtprio/rtprio.c
+++ b/usr.sbin/rtprio/rtprio.c
@@ -67,6 +67,8 @@ main(int argc, char *argv[])
 		rtp.type = RTP_PRIO_REALTIME;
 	else if (!strcmp(p, "idprio"))
 		rtp.type = RTP_PRIO_IDLE;
+	else
+		errx(1, "invalid basename");
 
 	switch (argc) {
 	case 2:
@@ -76,20 +78,19 @@ main(int argc, char *argv[])
 	case 1:
 		if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
 			err(1, "RTP_LOOKUP");
-		printf("%s: ", p);
 		switch (rtp.type) {
 		case RTP_PRIO_REALTIME:
 		case RTP_PRIO_FIFO:
-			printf("realtime priority %d\n", rtp.prio);
+			warnx("realtime priority %d", rtp.prio);
 			break;
 		case RTP_PRIO_NORMAL:
-			printf("normal priority\n");
+			warnx("normal priority");
 			break;
 		case RTP_PRIO_IDLE:
-			printf("idle priority %d\n", rtp.prio);
+			warnx("idle priority %d", rtp.prio);
 			break;
 		default:
-			printf("invalid priority type %d\n", rtp.type);
+			errx(1, "invalid priority type %d", rtp.type);
 			break;
 		}
 		exit(0);
@@ -110,18 +111,17 @@ main(int argc, char *argv[])
 			break;
 		}
 
-		if (argv[2][0] == '-')
-			proc = parseint(argv[2] + 1, "pid");
-		if (rtprio(RTP_SET, proc, &rtp) != 0)
-			err(1, "RTP_SET");
-
-		if (proc == 0) {
+		if (argv[2][0] == '-') {
+			proc = parseint(argv[2], "pid");
+			proc = abs(proc);
+			if (rtprio(RTP_SET, proc, &rtp) != 0)
+				err(1, "RTP_SET");
+		} else {
 			execvp(argv[2], &argv[2]);
-			err(1, "%s", argv[2]);
+				err(1, "%s", argv[2]);
 		}
 		exit(0);
 	}
-	exit(1);
 }
 
 static int

--3V7upXqbjpZ4EhLz--



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