Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jan 2011 02:41:10 -0500
From:      Eitan Adler <lists@eitanadler.com>
To:        hackers@freebsd.org
Subject:   [patch] have rtprio check that arguments are numeric; change atoi to strtol
Message-ID:  <AANLkTimdEpZ1DBoKU9g6w-j=F9KS0ONvo9racgOesdPf@mail.gmail.com>
In-Reply-To: <AANLkTimmQHRoQOZBgfheds=_sv5SMzD9DrPfYk9em7j8@mail.gmail.com>
References:  <AANLkTimiJPiHBSw5i5TVJYfh9uGOyrNJx%2BoUPeB%2Bt%2BY_@mail.gmail.com> <6BEFA669-2E1F-44E6-897A-0A51DA939A74@gmail.com> <AANLkTimmQHRoQOZBgfheds=_sv5SMzD9DrPfYk9em7j8@mail.gmail.com>

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

> Just set the second argument to strtol to something non-NULL and then check
> the value returned; that will help provide the error handling with
> simplicity that you desire :).

How about this version? It also corrects a copy/paste error I have above

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

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

       switch (argc) {
       case 2:
-               proc = abs(atoi(argv[1]));      /* Should check if numeric
-                                                * arg! */
+               proc = abs((int)strtol(argv[1], &invalidChar, 10));
+               if (*invalidChar != '\0')
+                       errx(1,"Process should be a number");
               /* FALLTHROUGH */
       case 1:
               if (rtprio(RTP_LOOKUP, proc, &rtp) != 0)
@@ -104,16 +106,20 @@
                                       break;
                               }
                       } else {
-                               rtp.prio = atoi(argv[1]);
+                               rtp.prio = (int)strtol(argv[1],
&invalidChar, 10);
+                               if (*invalidChar != '\0')
+                                       errx(1,"Priority should be a
number", invalidChar);
                       }
               } else {
                       usage();
                       break;
               }

-               if (argv[2][0] == '-')
-                       proc = -atoi(argv[2]);
-
+               if (argv[2][0] == '-') {
+                       proc = -(int)strtol(argv[2], &invalidChar, 10);
+                       if (*invalidChar != '\0')
+                               errx(1,"Process should be a number");
+               }
               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?AANLkTimdEpZ1DBoKU9g6w-j=F9KS0ONvo9racgOesdPf>