hush: GETOPT_RESET() _after_ getopts too.

NOEXEC'ed applets which use getopt() need this.

function                                             old     new   delta
builtin_getopts                                      403     413     +10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-08-29 14:32:17 +02:00
parent 55af51c66d
commit 6016181b68
4 changed files with 6 additions and 5 deletions

View File

@ -1212,7 +1212,7 @@ uint32_t getopt32long(char **argv, const char *optstring, const char *longopts,
* By ~2008, OpenBSD 3.4 was changed to survive glibc-like optind = 0 * By ~2008, OpenBSD 3.4 was changed to survive glibc-like optind = 0
* (to interpret it as if optreset was set). * (to interpret it as if optreset was set).
*/ */
#ifdef __GLIBC__ #if 1 /*def __GLIBC__*/
#define GETOPT_RESET() (optind = 0) #define GETOPT_RESET() (optind = 0)
#else /* BSD style */ #else /* BSD style */
#define GETOPT_RESET() (optind = 1) #define GETOPT_RESET() (optind = 1)

View File

@ -517,7 +517,7 @@ vgetopt32(char **argv, const char *applet_opts, const char *applet_long_options,
} }
/* In case getopt32 was already called: /* In case getopt32 was already called:
* reset the libc getopt() function, which keeps internal state. * reset libc getopt() internal state.
* run_nofork_applet() does this, but we might end up here * run_nofork_applet() does this, but we might end up here
* also via gunzip_main() -> gzip_main(). Play safe. * also via gunzip_main() -> gzip_main(). Play safe.
*/ */

View File

@ -9956,7 +9956,7 @@ Test that VAR is a valid variable name?
* until we get Nth result (or failure). * until we get Nth result (or failure).
* (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set). * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set).
*/ */
optind = 0; /* reset getopt() state */ GETOPT_RESET();
count = 0; count = 0;
n = string_array_len(argv); n = string_array_len(argv);
do { do {
@ -9971,6 +9971,7 @@ Test that VAR is a valid variable name?
/* Set OPTIND. Prevent resetting of the magic counter! */ /* Set OPTIND. Prevent resetting of the magic counter! */
set_local_var_from_halves("OPTIND", utoa(optind)); set_local_var_from_halves("OPTIND", utoa(optind));
G.getopt_count = count; /* "next time, give me N+1'th result" */ G.getopt_count = count; /* "next time, give me N+1'th result" */
GETOPT_RESET(); /* just in case */
/* Set OPTARG */ /* Set OPTARG */
/* Always set or unset, never left as-is, even on exit/error: /* Always set or unset, never left as-is, even on exit/error:

View File

@ -425,8 +425,8 @@ shell_builtin_ulimit(char **argv)
* ulimit 123 -c2 -l 456 * ulimit 123 -c2 -l 456
*/ */
/* In case getopt was already called: /* In case getopt() was already called:
* reset the libc getopt() function, which keeps internal state. * reset libc getopt() internal state.
*/ */
GETOPT_RESET(); GETOPT_RESET();