[patch] have rtprio check that arguments are numeric; change atoi to strtol

Eitan Adler lists at eitanadler.com
Sun Jan 2 06:15:34 UTC 2011


When looking at the rtprio(1) source I noticed that the dg in revision
3291 has indicated that he wanted to check to ensure that the user
provided priority was numeric. Also the man page for atoi says the
function is deprecated - so I replaced those functions as well.

Index: rtprio.c
===================================================================
--- rtprio.c	(revision 216679)
+++ rtprio.c	(working copy)
@@ -56,6 +56,7 @@
 	char   *p;
 	int     proc = 0;
 	struct rtprio rtp;
+	int c;

 	/* find basename */
 	if ((p = rindex(argv[0], '/')) == NULL)
@@ -70,8 +71,10 @@

 	switch (argc) {
 	case 2:
-		proc = abs(atoi(argv[1]));	/* Should check if numeric
-						 * arg! */
+		for (c=0; c < strlen(argv[1]); ++c)
+			if (!isdigit(argv[1][c]))
+				errx(1,"%s", "Priority should be a number");
+		proc = (int)strtol(argv[1], (char **)NULL, 10);
 		/* FALLTHROUGH */
 	case 1:
 		if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
@@ -104,7 +107,10 @@
 					break;
 				}
 			} else {
-				rtp.prio = atoi(argv[1]);
+			for (c=0; c < strlen(argv[1]); ++c)
+				if (!isdigit(argv[1][c]))
+					errx(1,"%s", "Priority should be a number");
+			rtp.prio = (int)strtol(argv[1], (char **)NULL, 10);
 			}
 		} else {
 			usage();
@@ -112,7 +118,7 @@
 		}

 		if (argv[2][0] == '-')
-			proc = -atoi(argv[2]);
+			proc = -(int)strtol(argv[2], (char **)NULL, 10);

 		if (rtprio(RTP_SET, proc, &rtp) != 0)
 			err(1, "%s", argv[0]);


-- 
Eitan Adler


More information about the freebsd-hackers mailing list