Skip to content

Commit

Permalink
Fix v=$(<file) for closed FD 0,1,2 (rhbz#1066589)
Browse files Browse the repository at this point in the history
var=$(< file) now reads the file even if the standard inout,
standard output and/or standard error file descriptors are closed.

Original patch:
https://src.fedoraproject.org/rpms/ksh/blob/642af4d6/f/ksh-20120801-filecomsubst.patch

src/cmd/ksh93/sh/io.c: sh_redirect():
- When processing the '<' redirector as part of $(< ...), i.e. if
  flag==3, make sure the FD of the file to read is > 2 by calling
  sh_iomovefd(). Unlike the RedHat patch, this checks for flag==3
  to avoid unnecessary sh_iomovefd() calls for normal redirections,
  as there was no bug with those.

src/cmd/ksh93/tests/io.sh:
- Add test.
  • Loading branch information
McDutchie committed Sep 22, 2020
1 parent 5683155 commit fe6d090
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Any uppercase BUG_* names are modernish shell bug IDs.
to lose the output of the commands they ran. This bug occurred when ksh was
compiled with the SHOPT_SPAWN compile-time option.

- Bugfix: var=$(< file) now reads the file even if the standard inout, standard
output and/or standard error file descriptors are closed.

2020-09-20:

- Bugfix: when whence -v/-a found an "undefined" (i.e. autoloadable) function
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/sh/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,8 @@ int sh_redirect(Shell_t *shp,struct ionod *iop, int flag)
if(flag==SH_SHOWME)
goto traceit;
fd=sh_chkopen(fname);
if(flag==3) /* make sure that $(<file) works... */
fd=sh_iomovefd(fd); /* ...with stdin/stdout/stderr closed */
}
else if(sh_isoption(SH_RESTRICTED))
errormsg(SH_DICT,ERROR_exit(1),e_restricted,fname);
Expand Down
1 change: 1 addition & 0 deletions src/cmd/ksh93/tests/io.sh
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ got=$(<$tmp/22.out)
tmp=$tmp $SHELL 2> /dev/null -c 'exec 3<&1 ; exec 1<&- ; exec > $tmp/outfile;print foobar' || err_exit 'exec 1<&- causes failure'
[[ $(<$tmp/outfile) == foobar ]] || err_exit 'outfile does not contain foobar'
[[ $(<$tmp/outfile) == foobar ]] <&- >&- 2>&- || err_exit '$(<file) does not work with stdin, stdout and/or stderr closed'
print hello there world > $tmp/foobar
sed -e 's/there //' $tmp/foobar >; $tmp/foobar
Expand Down

0 comments on commit fe6d090

Please sign in to comment.