Skip to content

Commit f15aa57

Browse files
author
Denys Vlasenko
committed
ash: [PARSER] Fix parsing of ${##1}
Upstream commit: Date: Thu, 4 Oct 2007 22:15:10 +0800 [PARSER] Fix parsing of ${##1} Previously dash treated ${##1} as a length operation. This patch fixes that. Test case: set -- a echo ${##1}OK Old result: 1OK New result: OK This was a real bug in ash (but not in hush). Signed-off-by: Denys Vlasenko <[email protected]>
1 parent e19923f commit f15aa57

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

shell/ash.c

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11728,16 +11728,9 @@ parsesub: {
1172811728
subtype = VSNORMAL;
1172911729
if (c == '{') {
1173011730
c = pgetc_eatbnl();
11731-
if (c == '#') {
11732-
c = pgetc_eatbnl();
11733-
if (c == '}')
11734-
c = '#'; /* ${#} - same as $# */
11735-
else
11736-
subtype = VSLENGTH; /* ${#VAR} */
11737-
} else {
11738-
subtype = 0;
11739-
}
11731+
subtype = 0;
1174011732
}
11733+
varname:
1174111734
if (c <= 255 /* not PEOA or PEOF */ && is_name(c)) {
1174211735
/* $[{[#]]NAME[}] */
1174311736
do {
@@ -11752,8 +11745,23 @@ parsesub: {
1175211745
} while (isdigit(c));
1175311746
} else if (is_special(c)) {
1175411747
/* $[{[#]]<specialchar>[}] */
11755-
USTPUTC(c, out);
11748+
int cc = c;
11749+
1175611750
c = pgetc_eatbnl();
11751+
if (!subtype && cc == '#') {
11752+
subtype = VSLENGTH;
11753+
if (c == '_' || isalnum(c))
11754+
goto varname;
11755+
cc = c;
11756+
c = pgetc_eatbnl();
11757+
if (cc == '}' || c != '}') {
11758+
pungetc();
11759+
subtype = 0;
11760+
c = cc;
11761+
cc = '#';
11762+
}
11763+
}
11764+
USTPUTC(cc, out);
1175711765
} else {
1175811766
goto badsub;
1175911767
}

shell/ash_test/ash-vars/param_expand_len.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ Make sure len parsing doesnt break arg count
77
Testing len op
88
4 3 2 1 0 0
99
0 3 0
10+
Nothing:
11+
Nothing:
12+
One:1

shell/ash_test/ash-vars/param_expand_len.tests

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ unset e
1515
f=abc
1616
g=
1717
echo ${#e} ${#f} ${#g}
18+
19+
set -- a
20+
# This must be interpreted as: $# ("1"), then remove trailing "1".
21+
# IOW: empty result.
22+
echo Nothing:${##1}
23+
echo Nothing:${#%1}
24+
echo One:${##x}

shell/hush_test/hush-vars/param_expand_len.right

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ Make sure len parsing doesnt break arg count
77
Testing len op
88
4 3 2 1 0 0
99
0 3 0
10+
Nothing:
11+
Nothing:
12+
One:1

shell/hush_test/hush-vars/param_expand_len.tests

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ unset e
1515
f=abc
1616
g=
1717
echo ${#e} ${#f} ${#g}
18+
19+
set -- a
20+
# This must be interpreted as: $# ("1"), then remove trailing "1".
21+
# IOW: empty result.
22+
echo Nothing:${##1}
23+
echo Nothing:${#%1}
24+
echo One:${##x}

0 commit comments

Comments
 (0)