]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
expr: don't interpret argv[0] as an operand
authorJim Meyering <meyering@redhat.com>
Wed, 15 Oct 2008 07:19:29 +0000 (09:19 +0200)
committerJim Meyering <meyering@redhat.com>
Wed, 15 Oct 2008 13:34:58 +0000 (15:34 +0200)
* src/expr.c (main): Given a first argument like -22 (negative, with
two or more digits), expr would decrement optind to 0 and then attempt
to evaluate argv[0].

src/expr.c

index dc416160223a32f88923acd3f3b82c77450cf023..b7bec646bcdde5d8d777ecbe7714e8eced3e2385 100644 (file)
@@ -261,6 +261,7 @@ main (int argc, char **argv)
 {
   VALUE *v;
   int c;
+  int saved_optind;
 
   initialize_main (&argc, &argv);
   set_program_name (argv[0]);
@@ -273,6 +274,7 @@ main (int argc, char **argv)
 
   /* The argument -0 should not result in an error message. */
   opterr = 0;
+  saved_optind = 1;
 
   while ((c = getopt_long (argc, argv, "+", long_options, NULL)) != -1)
     {
@@ -282,7 +284,7 @@ main (int argc, char **argv)
        */
       if ('?' == c)
        {
-         --optind;
+         optind = saved_optind;
          break;
        }
       else
@@ -305,6 +307,7 @@ main (int argc, char **argv)
 
            case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
          }
+      saved_optind = optind;
     }
 
   if (argc <= optind)