hush: expand_vars_to_list() should not assume it starts new word
authorDenys Vlasenko <vda.linux@googlemail.com>
Fri, 20 Jul 2018 12:45:12 +0000 (14:45 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Fri, 20 Jul 2018 14:27:26 +0000 (16:27 +0200)
function                                             old     new   delta
expand_variables                                     112     115      +3
expand_vars_to_list                                 1117    1108      -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 3/-9)               Total: -6 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/hush.c

index fca67dc179e786f40543768b076547c5240daa9b..b8af1b088ed81a5aee1858549496feec69cfd057 100644 (file)
@@ -3343,7 +3343,6 @@ static char **o_finalize_list(o_string *o, int n)
        char **list;
        int string_start;
 
-       n = o_save_ptr(o, n); /* force growth for list[n] if necessary */
        if (DEBUG_EXPAND)
                debug_print_list("finalized", o, n);
        debug_printf_expand("finalized n:%d\n", n);
@@ -6334,12 +6333,8 @@ static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
        char cant_be_null = 0; /* only bit 0x80 matters */
        char *p;
 
-       output->ended_in_ifs = 0;  /* did last unquoted expansion end with IFS chars? */
-
        debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg,
                        !!(output->o_expflags & EXP_FLAG_SINGLEWORD));
-       debug_print_list("expand_vars_to_list", output, n);
-       n = o_save_ptr(output, n);
        debug_print_list("expand_vars_to_list[0]", output, n);
 
        while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
@@ -6512,9 +6507,16 @@ static char **expand_variables(char **argv, unsigned expflags)
        output.o_expflags = expflags;
 
        n = 0;
-       while (*argv) {
-               n = expand_vars_to_list(&output, n, *argv);
-               argv++;
+       for (;;) {
+               /* go to next list[n] */
+               output.ended_in_ifs = 0;
+               n = o_save_ptr(&output, n);
+
+               if (!*argv)
+                       break;
+
+               /* expand argv[i] */
+               n = expand_vars_to_list(&output, n, *argv++);
        }
        debug_print_list("expand_variables", &output, n);