Skip to content

Commit

Permalink
parser: Fix VSLENGTH parsing with trailing garbage
Browse files Browse the repository at this point in the history
On Sat, Jun 19, 2021 at 02:44:46PM +0200, Denys Vlasenko wrote:
>
> CTLVAR and CTLBACKQ are not properly handled if encountered
> inside {$#...}. Testcase:
>
> dash -c "`printf 'echo ${#1\x82}'`" 00 111 222
>
> It should execute "echo ${#1 <byte 0x82> }" and thus print "3"
> (the length of $1, which is "111").
>
> Instead, it segfaults.
>
> (Ideally, it should fail since "1 <byte 0x82>" is not a valid
> variable name, but currently dash accepts e.g. "${#1abc}"
> as if it is "${#1}bc". A separate, less serious bug...).

In fact these two bugs are one and the same.  This patch fixes
both by detecting the invalid substitution and not emitting it
into the node tree.

Incidentally this reveals a bug in how we parse ${#10} that got
introduced recently, which is also fixed here.

Reported-by: Denys Vlasenko <vda.linux@googlemail.com>
Fixes: 7710a92 ("parser: Only accept single-digit parameter...")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
herbertx committed Sep 3, 2021
1 parent 6f6d1f2 commit 057cd65
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,8 @@ parsesub: {
do {
STPUTC(c, out);
c = pgetc_eatbnl();
} while (!subtype && is_digit(c));
} while ((subtype <= 0 || subtype >= VSLENGTH) &&
is_digit(c));
} else if (c != '}') {
int cc = c;

Expand Down Expand Up @@ -1312,6 +1313,8 @@ parsesub: {
break;
}
} else {
if (subtype == VSLENGTH && c != '}')
subtype = 0;
badsub:
pungetc();
}
Expand Down
1 change: 1 addition & 0 deletions src/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#define VSTRIMLEFT 0x8 /* ${var#pattern} */
#define VSTRIMLEFTMAX 0x9 /* ${var##pattern} */
#define VSLENGTH 0xa /* ${#var} */
/* VSLENGTH must come last. */

/* values of checkkwd variable */
#define CHKALIAS 0x1
Expand Down

0 comments on commit 057cd65

Please sign in to comment.