ash: do not expand tilde in parameter expansion within quotes

Test case:
   unset a
   echo "${a:-~root}"
Old result:
   /root
New result:
   ~root

Based on commit 170f44d from git://git.kernel.org/pub/scm/utils/dash/dash.git
by Herbert Xu

function                                             old     new   delta
evalvar                                              598     604      +6
parse_command                                       1440    1443      +3
localcmd                                             325     327      +2
readtoken1                                          3199    3200      +1
argstr                                              1180    1164     -16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 12/-16)             Total: -4 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2015-05-18 09:53:26 +02:00 committed by Denys Vlasenko
parent eb6b48ba74
commit 3df47f9cbb
3 changed files with 6 additions and 5 deletions

View File

@ -5529,7 +5529,7 @@ ash_arith(const char *s)
#define EXP_RECORD 0x20 /* need to record arguments for ifs breakup */
#define EXP_VARTILDE2 0x40 /* expand tildes after colons only */
#define EXP_WORD 0x80 /* expand word in parameter expansion */
#define EXP_QWORD 0x100 /* expand word in quoted parameter expansion */
#define EXP_QUOTED 0x100 /* expand word in double quotes */
/*
* rmescape() flags
*/
@ -6054,7 +6054,7 @@ argstr(char *p, int flags, struct strlist *var_str_list)
};
const char *reject = spclchars;
int quotes = flags & QUOTES_ESC;
int breakall = flags & EXP_WORD;
int breakall = (flags & (EXP_WORD | EXP_QUOTED)) == EXP_WORD;
int inquotes;
size_t length;
int startloc;
@ -6072,8 +6072,6 @@ argstr(char *p, int flags, struct strlist *var_str_list)
flags &= ~EXP_TILDE;
tilde:
q = p;
if ((unsigned char)*q == CTLESC && (flags & EXP_QWORD))
q++;
if (*q == '~')
p = exptilde(p, q, flags);
}
@ -6790,7 +6788,7 @@ evalvar(char *p, int flags, struct strlist *var_str_list)
if (varlen < 0) {
argstr(
p,
flags | (quoted ? EXP_TILDE|EXP_QWORD : EXP_TILDE|EXP_WORD),
flags | EXP_TILDE | EXP_WORD | (quoted ? EXP_QUOTED : 0),
var_str_list
);
goto end;

View File

@ -0,0 +1,2 @@
unset a
echo "${a:-~root}"