getopt-gnu: avoid crash in glibc getopt
authorEric Blake <eblake@redhat.com>
Thu, 7 Jul 2011 16:24:20 +0000 (10:24 -0600)
committerEric Blake <eblake@redhat.com>
Thu, 7 Jul 2011 16:58:40 +0000 (10:58 -0600)
Use of "W;" in an option string may be rare, but we might as well
avoid crashing on it.

* m4/getopt.m4 (gl_GETOPT_CHECK_HEADRS): Detect the problem.
* tests/test-getopt.h (test_getopt): Enhance test.
* tests/test-getopt_long.h (test_getopt_long): Likewise.
* doc/posix-functions/getopt.texi (getopt): Document it.
* doc/glibc-functions/getopt_long.texi (getopt_long): Likewise.
* doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
Likewise.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
doc/glibc-functions/getopt_long.texi
doc/glibc-functions/getopt_long_only.texi
doc/posix-functions/getopt.texi
m4/getopt.m4
tests/test-getopt.h
tests/test-getopt_long.h

index a5d52ee138022a99ecf91ca8153bdce5f93b7765..e95d288da27ddd6380e5a30cf25334323c71e8be 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2011-07-07  Eric Blake  <eblake@redhat.com>
+
+       getopt-gnu: avoid crash in glibc getopt
+       * m4/getopt.m4 (gl_GETOPT_CHECK_HEADRS): Detect the problem.
+       * tests/test-getopt.h (test_getopt): Enhance test.
+       * tests/test-getopt_long.h (test_getopt_long): Likewise.
+       * doc/posix-functions/getopt.texi (getopt): Document it.
+       * doc/glibc-functions/getopt_long.texi (getopt_long): Likewise.
+       * doc/glibc-functions/getopt_long_only.texi (getopt_long_only):
+       Likewise.
+
 2011-07-07  Ulrich Drepper  <drepper@gmail.com>
 
        getopt: handle W; without long options in getopt [BZ #12922]
index f4d3561c03256f171fb06327c5686bfadbd31d24..c5e86fe926c55e816ab9aac9e5c8d67e6b4d030a 100644 (file)
@@ -37,6 +37,10 @@ MacOS X 10.5, OpenBSD 4.0, AIX 5.2, IRIX 6.5, Solaris 11 2010-11, Cygwin 1.5.x.
 @item
 This function is missing on some platforms:
 AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Interix 3.5.
+@item
+This function crashes if the option string includes @code{W;} but
+there are no long options, on some platforms:
+glibc 2.14.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 1b90c416f77512149baf6e1c9749e491ead574a9..89316707160abf465ab76a8d3c85bda058e991ea 100644 (file)
@@ -38,6 +38,10 @@ MacOS X 10.5, OpenBSD 4.0, AIX 5.2, Solaris 11 2010-11, Cygwin 1.5.x.
 This function is missing on some platforms:
 MacOS X 10.3, FreeBSD 5.2.1, NetBSD 5.0, AIX 5.1, HP-UX 11, IRIX 6.5,
 OSF/1 5.1, mingw, Interix 3.5.
+@item
+This function crashes if the option string includes @code{W;} but
+there are no long options, on some platforms:
+glibc 2.14.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index bab4922e191543227bda1983852e1257b077d364..099c2e3d0739c6302f39dd0cebb57d72f5ac3fac 100644 (file)
@@ -48,6 +48,10 @@ AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 9, Interix 3.5.
 The function @code{getopt_long_only} is missing on some platforms:
 MacOS X 10.3, FreeBSD 5.2.1, NetBSD 5.0, AIX 5.1, HP-UX 11, IRIX 6.5,
 OSF/1 5.1, Solaris 9, mingw, Interix 3.5.
+@item
+This function crashes if the option string includes @code{W;} on some
+platforms:
+glibc 2.14.
 @end itemize
 
 Portability problems not fixed by Gnulib:
index 4d8450ff907211f50845f552481165937e68e7a2..db586bd7ef0866d640e22ddf0e828828535fd5eb 100644 (file)
@@ -1,4 +1,4 @@
-# getopt.m4 serial 35
+# getopt.m4 serial 36
 dnl Copyright (C) 2002-2006, 2008-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -281,6 +281,13 @@ dnl is ambiguous with environment values that contain newlines.
                else if (getopt (3, argv, "+:a:b") != ':')
                  result |= 64;
              }
+             /* This code dumps core on glibc 2.14.  */
+             {
+               char *argv[] = { "program", "-W", "dummy", NULL };
+               optind = opterr = 1;
+               if (getopt (3, argv, "W;") != 'W')
+                 result |= 128;
+             }
              return result;
            ]])],
         [gl_cv_func_getopt_gnu=yes],
index 9c877b3d181282e3776a6772d7b389ca58178a52..e3fc589f5379890b4a0410aa4968a9cfded9dadb 100644 (file)
@@ -1365,5 +1365,28 @@ test_getopt (void)
       ASSERT (optind == 3);
       ASSERT (!output);
     }
+
+  /* Check that 'W' does not dump core:
+     http://sourceware.org/bugzilla/show_bug.cgi?id=12922
+     Technically, POSIX says the presence of ';' in the opt-string
+     gives unspecified behavior, so we only test this when GNU compliance
+     is desired.  */
+  for (start = OPTIND_MIN; start <= 1; start++)
+    {
+      int argc = 0;
+      const char *argv[10];
+      int c;
+      int pos = ftell (stderr);
+
+      argv[argc++] = "program";
+      argv[argc++] = "-W";
+      argv[argc++] = "dummy";
+      argv[argc] = NULL;
+      optind = start;
+      opterr = 1;
+      ASSERT (getopt (argc, (char **) argv, "W;") == 'W');
+      ASSERT (ftell (stderr) == pos);
+      ASSERT (optind == 2);
+    }
 #endif /* GNULIB_TEST_GETOPT_GNU */
 }
index 7a748674aeda35dacf5fdd0a71b8d86560f437ca..c1035b166473dbbe1915a66267b424068080c93b 100644 (file)
@@ -314,6 +314,25 @@ test_getopt_long (void)
       }
   }
 
+  /* Test that 'W' does not dump core:
+     http://sourceware.org/bugzilla/show_bug.cgi?id=12922  */
+  {
+    int argc = 0;
+    const char *argv[10];
+    int option_index;
+    int c;
+
+    argv[argc++] = "program";
+    argv[argc++] = "-W";
+    argv[argc++] = "dummy";
+    argv[argc] = NULL;
+    optind = 1;
+    opterr = 0;
+    c = do_getopt_long (argc, argv, "W;", NULL, &option_index);
+    ASSERT (c == 'W');
+    ASSERT (optind == 2);
+  }
+
   /* Test processing of boolean short options.  */
   for (start = 0; start <= 1; start++)
     {