[git commit] ash: do not expand tilde in parameter expansion within quotes

Denys Vlasenko vda.linux at googlemail.com
Mon May 18 07:53:26 UTC 2015


commit: http://git.busybox.net/busybox/commit/?id=3df47f9cbbb7f16608cbc642026625cdf7f06aa9
branch: http://git.busybox.net/busybox/commit/?id=refs/heads/master

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 at pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c                                        |    8 +++-----
 ...nd-tilde-in-parameter-expansion-in-quotes.right |    1 +
 ...nd-tilde-in-parameter-expansion-in-quotes.tests |    2 ++
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index 4c43f1f..d87166c 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -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;
diff --git a/shell/ash_test/ash-vars/var-do-not-expand-tilde-in-parameter-expansion-in-quotes.right b/shell/ash_test/ash-vars/var-do-not-expand-tilde-in-parameter-expansion-in-quotes.right
new file mode 100644
index 0000000..4b9b4f0
--- /dev/null
+++ b/shell/ash_test/ash-vars/var-do-not-expand-tilde-in-parameter-expansion-in-quotes.right
@@ -0,0 +1 @@
+~root
diff --git a/shell/ash_test/ash-vars/var-do-not-expand-tilde-in-parameter-expansion-in-quotes.tests b/shell/ash_test/ash-vars/var-do-not-expand-tilde-in-parameter-expansion-in-quotes.tests
new file mode 100755
index 0000000..d8eb8fc
--- /dev/null
+++ b/shell/ash_test/ash-vars/var-do-not-expand-tilde-in-parameter-expansion-in-quotes.tests
@@ -0,0 +1,2 @@
+unset a
+echo "${a:-~root}"


More information about the busybox-cvs mailing list