Skip to content

Commit

Permalink
Fix a SHOPT_DEVFD process substitution file descriptor
Browse files Browse the repository at this point in the history
This commit fixes a long-standing bug that caused a file
descriptor leak when passing a process substitution to a
function. The leak only occured when ksh was compiled with
SHOPT_DEVFD; the FIFO method was unaffected.

src/cmd/ksh93/sh/xec.c:
- When a process substitution is passed to a builtin, the
  remaining file descriptor is closed with sh_iorestore.
  Do the same thing when passing a process substitution
  to a function.
- This fix alone isn't enough, as a file descriptor leak
  could still occur if 'command' was given a function as an
  argument, then passed a process substitution. Add another
  sh_iorestore in this edge case to fix the second file
  descriptor leak.

src/cmd/ksh93/include/defs.h:
- Since the file descriptor leak is now fixed, remove the
  workaround that forced ksh to use the FIFO method.

Fixes: ksh93#67
  • Loading branch information
JohnoKing committed Mar 12, 2021
1 parent d2c1700 commit e1f3dd4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 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.

2021-03-12:

- Fixed a file descriptor leak that occurred when ksh used /dev/fd for
process substitutions passed to functions.

2021-03-11:

- Fixed an intermittent bug that caused process substitutions to infinitely
Expand Down
8 changes: 0 additions & 8 deletions src/cmd/ksh93/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@
# define mbmax() 1
#endif

/*
* Until a file descriptor leak with process substitution
* is fixed, disable /dev/fd use to avoid the problem.
* https://github.com/ksh93/ksh/issues/67
*/
#undef SHOPT_DEVFD
#define SHOPT_DEVFD 0

#include <sfio.h>
#include <error.h>
#include "FEATURE/externs"
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/ksh93/sh/xec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,8 @@ int sh_exec(register const Shnode_t *t, int flags)
sh_popcontext(shp,buffp);
sh_iorestore(shp,indx,jmpval);
}
if(shp->topfd>topfd)
sh_iorestore(shp,topfd,jmpval);
if(nq)
unset_instance(nq,&node,&nr,mode);
sh_funstaks(slp->slchild,-1);
Expand All @@ -1550,6 +1552,8 @@ int sh_exec(register const Shnode_t *t, int flags)
siglongjmp(*shp->jmplist,jmpval);
goto setexit;
}
else if(command && shp->topfd>topfd)
sh_iorestore(shp,topfd,0);
}
else if(!io)
{
Expand Down

0 comments on commit e1f3dd4

Please sign in to comment.