]> err.no Git - util-linux/commitdiff
renice: correctly detect errors in arguments
authorLaMont Jones <lamont@debian.org>
Mon, 7 Jan 2008 02:53:56 +0000 (19:53 -0700)
committerLaMont Jones <lamont@debian.org>
Mon, 7 Jan 2008 02:53:56 +0000 (19:53 -0700)
renice was using atoi(), which does no error detection, meaning that:
"renice +20 blah" was accepted as valid.

Addresses-Debian-Bug: 385245
Signed-off-by: LaMont Jones <lamont@debian.org>
sys-utils/renice.c

index 2807ae3bcb17269348339ea40c665e89856e0f86..a8bf73d90d83888501cd49aa507766c4486378d9 100644 (file)
 
 int donice(int,int,int);
 
+void usage(void)
+{
+       fprintf(stderr, _("usage: renice priority [ [ -p ] pids ] "
+                         "[ [ -g ] pgrps ] [ [ -u ] users ]\n"));
+       exit(1);
+}
 /*
  * Change the priority (nice) of processes
  * or groups of processes which are already
@@ -58,6 +64,7 @@ main(int argc, char **argv)
 {
        int which = PRIO_PROCESS;
        int who = 0, prio, errs = 0;
+       char *endptr=NULL;
 
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
@@ -65,11 +72,12 @@ main(int argc, char **argv)
 
        argc--, argv++;
        if (argc < 2) {
-               fprintf(stderr, _("usage: renice priority [ [ -p ] pids ] "
-                                 "[ [ -g ] pgrps ] [ [ -u ] users ]\n"));
-               exit(1);
+               usage();
+       }
+       prio = strtol(*argv,&endptr,10);
+       if (*endptr) {
+               usage();
        }
-       prio = atoi(*argv);
        argc--, argv++;
 #if 0
        if (prio > PRIO_MAX)
@@ -100,8 +108,8 @@ main(int argc, char **argv)
                        }
                        who = pwd->pw_uid;
                } else {
-                       who = atoi(*argv);
-                       if (who < 0) {
+                       who = strtol(*argv,&endptr,10);
+                       if (who < 0 || *endptr) {
                                fprintf(stderr, _("renice: %s: bad value\n"),
                                        *argv);
                                continue;