Skip to content

Commit

Permalink
Fix command substitutions run on the same line as a here-doc (#91)
Browse files Browse the repository at this point in the history
When a command substitution is run on the same line as a here-document,
a syntax error occurs due to a regression introduced in ksh93u+ 2011-04-15:

true << EOF; true $(true)
EOF
syntax error at line 1: `<<EOF' here-document not contained within command substitution

The regression is caused by an error check that was added to make
the following script causes a syntax error (because the here-document
isn't completed inside of the command substitution):

$(true << EOF)
EOF

src/cmd/ksh93/sh/lex.c:
- Only throw an error when a here-document in a command substitution
  isn't completed inside of the command substitution.

src/cmd/ksh93/tests/heredoc.sh:
- Add a regression test for running a command substitution on the
  same line as a here-document.
- Add a missed regression test for using here-documents in command
  substitutions. This is the original bug that was fixed in ksh93u+
  2011-04-15 (it is why the error message was added), but a regression
  test for here-documents in command substitutions wasn't added in
  that version.

This bugfix was backported from ksh93v- 2013-10-10-alpha.
  • Loading branch information
JohnoKing committed Jul 23, 2020
1 parent f207cd5 commit 6e515f1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh

Any uppercase BUG_* names are modernish shell bug IDs.

2020-07-23:

- A command substitution that is run on the same line as a here-document
will no longer cause a syntax error.

2020-07-22:

- Fixed two race conditions when running external commands on
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-07-22"
#define SH_RELEASE "93u+m 2020-07-23"
3 changes: 2 additions & 1 deletion src/cmd/ksh93/sh/lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,7 @@ static int comsub(register Lex_t *lp, int endtok)
{
register int n,c,count=1;
register int line=lp->sh->inlineno;
struct ionod *inheredoc = lp->heredoc;
char *first,*cp=fcseek(0),word[5];
int off, messages=0, assignok=lp->assignok, csub;
struct lexstate save;
Expand Down Expand Up @@ -1689,7 +1690,7 @@ static int comsub(register Lex_t *lp, int endtok)
lp->lexd.dolparen--;
lp->lex = save;
lp->assignok = (endchar(lp)==RBRACT?assignok:0);
if(lp->heredoc)
if(lp->heredoc && !inheredoc)
errormsg(SH_DICT,ERROR_exit(SYNBAD),e_lexsyntax5,lp->sh->inlineno,lp->heredoc->ioname);
return(messages);
}
Expand Down
12 changes: 12 additions & 0 deletions src/cmd/ksh93/tests/heredoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,16 @@ print foo > $tmp/foofile
x=$( $SHELL 2> /dev/null 'read <<< $(<'"$tmp"'/foofile) 2> /dev/null;print -r "$REPLY"')
[[ $x == foo ]] || err_exit '<<< $(<file) not working'
# ======
# A syntax error should not occur if a command substitution is run on the same line
# as a here document.
$SHELL -c 'true << EOF || true "$(true)"
EOF' || err_exit 'placing a command substitution and here-doc on the same line causes a syntax error'
# A here-document in a command substitution should cause a syntax error if it isn't
# completed inside of the command substitution.
$SHELL -c '$(true << !)
!' 2> /dev/null && err_exit "a here-doc that isn't completed before the closing ) in a command substitution doesn't cause an error"
# ======
exit $((Errors<125?Errors:125))

0 comments on commit 6e515f1

Please sign in to comment.