Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jan 2011 01:15:13 -0500
From:      Eitan Adler <lists@eitanadler.com>
To:        freebsd-hackers@freebsd.org
Subject:   [patch] have rtprio check that arguments are numeric; change atoi to strtol
Message-ID:  <AANLkTimiJPiHBSw5i5TVJYfh9uGOyrNJx%2BoUPeB%2Bt%2BY_@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTimiJPiHBSw5i5TVJYfh9uGOyrNJx%2BoUPeB%2Bt%2BY_>