summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2011-07-07 10:24:20 -0600
committerEric Blake <eblake@redhat.com>2011-07-07 10:58:40 -0600
commit366d08c003506c7ed3c040289f191e1f5b3eb4af (patch)
tree5ecebfe772ae314c654461523149300c1b83af34 /tests
parentc4bf665bae3880a9a8974d64c1fad9fe2c80079f (diff)
downloadgnulib-366d08c003506c7ed3c040289f191e1f5b3eb4af.tar.gz
getopt-gnu: avoid crash in glibc getopt
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>
Diffstat (limited to 'tests')
-rw-r--r--tests/test-getopt.h23
-rw-r--r--tests/test-getopt_long.h19
2 files changed, 42 insertions, 0 deletions
diff --git a/tests/test-getopt.h b/tests/test-getopt.h
index 9c877b3d18..e3fc589f53 100644
--- a/tests/test-getopt.h
+++ b/tests/test-getopt.h
@@ -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 */
}
diff --git a/tests/test-getopt_long.h b/tests/test-getopt_long.h
index 7a748674ae..c1035b1664 100644
--- a/tests/test-getopt_long.h
+++ b/tests/test-getopt_long.h
@@ -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++)
{