Skip to content

Commit

Permalink
Allow turning off brace expansion in comsubs (rhbz#1078698)
Browse files Browse the repository at this point in the history
There was no check for the -B/braceexpand option before calling
path_expand() to process brace expansion, making it impossible to
turn off brace expansion within command substitutions. Normally the
lexer flags brace expansion so that this code is not reached, but
shell code within command substitutions is handled differently.

Red Hat patches this by adding this check to the function itself:
https://src.fedoraproject.org/rpms/ksh/blob/642af4d6/f/ksh-20140301-fikspand.patch
But I think it's more logical to patch it at the point of decision.

src/cmd/ksh93/sh/macro.c: endfield():
- Decide to call either path_generate() or path_expand() based on
  the state of the SH_BRACEEXPAND shell option.
- Fix '#if SHOPT_BRACEPAT' preprocessor check that previously
  hardcoded this decision at compile time.

src/cmd/ksh93/tests/options.sh:
- Add tests.
  • Loading branch information
McDutchie committed Sep 24, 2020
1 parent c1d9eed commit a14d17c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh

Any uppercase BUG_* names are modernish shell bug IDs.

2020-09-24:

- An omission made it impossible to turn off brace expansion within command
substitutions (`...`, $(...) or ${ ...; }) as the code for parsing these
did not check the -B/braceexpand option. This check has now been added.

2020-09-23:

- Fixed a crash that could occur when running a pipeline containing
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/include/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-09-23"
#define SH_RELEASE "93u+m 2020-09-24"
7 changes: 4 additions & 3 deletions src/cmd/ksh93/sh/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -2489,10 +2489,11 @@ static void endfield(register Mac_t *mp,int split)
{
mp->shp->argaddr = 0;
#if SHOPT_BRACEPAT
count = path_generate(mp->shp,argp,mp->arghead);
#else
count = path_expand(mp->shp,argp->argval,mp->arghead);
if(sh_isoption(SH_BRACEEXPAND))
count = path_generate(mp->shp,argp,mp->arghead);
else
#endif /* SHOPT_BRACEPAT */
count = path_expand(mp->shp,argp->argval,mp->arghead);
if(count)
mp->fields += count;
else if(split) /* pattern is null string */
Expand Down
14 changes: 14 additions & 0 deletions src/cmd/ksh93/tests/options.sh
Original file line number Diff line number Diff line change
Expand Up @@ -547,5 +547,19 @@ print $'alias print=:\nprint foobar' > dotfile
"$SHELL" -m -c '[[ -o monitor ]]' || err_exit 'option -m on command line does not work'
"$SHELL" -o monitor -c '[[ -o monitor ]]' || err_exit 'option -o monitor on command line does not work'

# ======
# Brace expansion could not be turned off in command substitutions (rhbz#1078698)
set -B
expect='test{1,2}'
actual=$(set +B; echo `echo test{1,2}`)
[[ $actual == "$expect" ]] || err_exit 'Brace expansion not turned off in `comsub`' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
actual=$(set +B; echo $(echo test{1,2}))
[[ $actual == "$expect" ]] || err_exit 'Brace expansion not turned off in $(comsub)' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
actual=$(set +B; echo ${ echo test{1,2}; })
[[ $actual == "$expect" ]] || err_exit 'Brace expansion not turned off in ${ comsub; }' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"

# ======
exit $((Errors<125?Errors:125))

0 comments on commit a14d17c

Please sign in to comment.