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

Eitan Adler lists at eitanadler.com
Sun Jan 2 07:41:30 UTC 2011


> 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


More information about the freebsd-hackers mailing list