ash: fix a bug in standalone mode (corrupted getopt() state)
authorDenis Vlasenko <vda.linux@googlemail.com>
Mon, 20 Oct 2008 07:52:33 +0000 (07:52 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Mon, 20 Oct 2008 07:52:33 +0000 (07:52 -0000)
libbb/getopt32.c
libbb/vfork_daemon_rexec.c
shell/ash_test/ash-standalone/nofork_trashes_getopt.right [new file with mode: 0644]
shell/ash_test/ash-standalone/nofork_trashes_getopt.tests [new file with mode: 0755]
util-linux/getopt.c

index 43fb6eb8668af6ba325ca9c732daf877a6e4c57e..49fb5335d2bc74a8f96b481ee16d3ed167d019b2 100644 (file)
@@ -515,28 +515,6 @@ getopt32(char **argv, const char *applet_opts, ...)
                }
        }
 
-       /* In case getopt32 was already called:
-        * reset the libc getopt() function, which keeps internal state.
-        *
-        * BSD-derived getopt() functions require that optind be set to 1 in
-        * order to reset getopt() state.  This used to be generally accepted
-        * way of resetting getopt().  However, glibc's getopt()
-        * has additional getopt() state beyond optind, and requires that
-        * optind be set to zero to reset its state.  So the unfortunate state of
-        * affairs is that BSD-derived versions of getopt() misbehave if
-        * optind is set to 0 in order to reset getopt(), and glibc's getopt()
-        * will core dump if optind is set 1 in order to reset getopt().
-        *
-        * More modern versions of BSD require that optreset be set to 1 in
-        * order to reset getopt().   Sigh.  Standards, anyone?
-        */
-#ifdef __GLIBC__
-       optind = 0;
-#else /* BSD style */
-       optind = 1;
-       /* optreset = 1; */
-#endif
-       /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
        pargv = NULL;
 
        /* Note: just "getopt() <= 0" will not work well for
index da0dc03e58b236f38c360a383f75e30aea185bb1..17b373cd1b00222d236aaadc68f4c4e5b11fd670 100644 (file)
@@ -125,6 +125,7 @@ int FAST_FUNC run_nofork_applet_prime(struct nofork_save_area *old, int applet_n
        int rc, argc;
 
        applet_name = APPLET_NAME(applet_no);
+
        xfunc_error_retval = EXIT_FAILURE;
 
        /* Special flag for xfunc_die(). If xfunc will "die"
@@ -132,7 +133,30 @@ int FAST_FUNC run_nofork_applet_prime(struct nofork_save_area *old, int applet_n
         * die_sleep and longjmp here instead. */
        die_sleep = -1;
 
-       /* option_mask32 = 0; - not needed */
+       /* In case getopt() or getopt32() was already called:
+        * reset the libc getopt() function, which keeps internal state.
+        *
+        * BSD-derived getopt() functions require that optind be set to 1 in
+        * order to reset getopt() state.  This used to be generally accepted
+        * way of resetting getopt().  However, glibc's getopt()
+        * has additional getopt() state beyond optind, and requires that
+        * optind be set to zero to reset its state.  So the unfortunate state of
+        * affairs is that BSD-derived versions of getopt() misbehave if
+        * optind is set to 0 in order to reset getopt(), and glibc's getopt()
+        * will core dump if optind is set 1 in order to reset getopt().
+        *
+        * More modern versions of BSD require that optreset be set to 1 in
+        * order to reset getopt().  Sigh.  Standards, anyone?
+        */
+#ifdef __GLIBC__
+       optind = 0;
+#else /* BSD style */
+       optind = 1;
+       /* optreset = 1; */
+#endif
+       /* optarg = NULL; opterr = 1; optopt = 63; - do we need this too? */
+       /* (values above are what they initialized to in glibc and uclibc) */
+       /* option_mask32 = 0; - not needed, no applet depends on it being 0 */
 
        argc = 1;
        while (argv[argc])
@@ -161,8 +185,16 @@ int FAST_FUNC run_nofork_applet_prime(struct nofork_save_area *old, int applet_n
                        rc = 0;
        }
 
-       /* Restoring globals */
+       /* Restoring some globals */
        restore_nofork_data(old);
+
+       /* Other globals can be simply reset to defaults */
+#ifdef __GLIBC__
+       optind = 0;
+#else /* BSD style */
+       optind = 1;
+#endif
+
        return rc & 0xff; /* don't confuse people with "exitcodes" >255 */
 }
 
diff --git a/shell/ash_test/ash-standalone/nofork_trashes_getopt.right b/shell/ash_test/ash-standalone/nofork_trashes_getopt.right
new file mode 100644 (file)
index 0000000..573541a
--- /dev/null
@@ -0,0 +1 @@
+0
diff --git a/shell/ash_test/ash-standalone/nofork_trashes_getopt.tests b/shell/ash_test/ash-standalone/nofork_trashes_getopt.tests
new file mode 100755 (executable)
index 0000000..f42c507
--- /dev/null
@@ -0,0 +1,6 @@
+# In this test, rm is NOFORK and it modifies getopt internal state
+rm -f non_existent_file
+# Subsequent hexdump is run as NOEXEC, and thus still uses this state
+hexdump </dev/null
+# Did hexdump segfault etc?
+echo $?
index 4026303858230a73bae507e8040efd27efbca184..8b5e46c7b57e0ee4533ef701be8ef0f69194d6ac 100644 (file)
@@ -142,7 +142,8 @@ static const char *normalize(const char *arg)
  * Other settings are found in global variables.
  */
 #if !ENABLE_GETOPT_LONG
-#define generate_output(argv,argc,optstr,longopts) generate_output(argv,argc,optstr)
+#define generate_output(argv,argc,optstr,longopts) \
+       generate_output(argv,argc,optstr)
 #endif
 static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts)
 {
@@ -156,14 +157,6 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru
        if (quiet_errors) /* No error reporting from getopt(3) */
                opterr = 0;
 
-       /* Reset getopt(3) (see libbb/getopt32.c for long rant) */
-#ifdef __GLIBC__
-       optind = 0;
-#else /* BSD style */
-       optind = 1;
-       /* optreset = 1; */
-#endif
-
        while (1) {
                opt =
 #if ENABLE_GETOPT_LONG