1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Re-enable SHOPT_DEVFD, fixing process substitution fd leaks (#218)

This commit fixes a long-standing bug (present since at least
ksh93r) that caused a file descriptor leak when passing a process
substitution to a function, or (if compiled with SHOPT_SPAWN) to a
nonexistent command.

The leaks only occurred when ksh was compiled with SHOPT_DEVFD; the
FIFO method was unaffected.

src/cmd/ksh93/sh/xec.c: sh_exec():
- When a process substitution is passed to a built-in, the
  remaining file descriptor is closed with sh_iorestore. Do the
  same thing when passing a process substitution to a function.
  This is done by delaying the sh_iorestore() call to 'setexit:'
  where both built-ins and functions terminate and set the exit
  status ($?).
  This means that call now will not be executed if a longjmp is
  done, e.g. due to an error in a special built-in. However, there
  is already another sh_iorestore() call in main.c, exfile(), line
  418, that handles that scenario.
- sh_ntfork() can fail, so rather than assume it will succeed,
  handle a failure by closing extra file descriptors with
  sh_iorestore(). This fixes the leak on command not found with
  SHOPT_SPAWN.

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

src/cmd/ksh93/SHOPT.sh:
- Add SHOPT_DEVFD as a configurable option (default: probe).

src/cmd/ksh93/tests/io.sh:
- Add a regression test for the 'not found' file descriptor leak.
- Add a test to ensure it keeps working with 'command'.

Fixes: https://github.com/ksh93/ksh/issues/67
This commit is contained in:
Johnothan King 2021-03-13 05:46:42 -08:00 committed by GitHub
parent d2c1700f63
commit 6d63b57dd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 35 additions and 17 deletions

View file

@ -629,6 +629,7 @@ fi
# ======
# File descriptor leak with process substitution
# https://github.com/ksh93/ksh/issues/67
err=$(
ulimit -n 15 || exit 0
fdUser() {
@ -641,6 +642,21 @@ err=$(
) || err_exit 'Process substitution leaks file descriptors when used as argument to function' \
"(got $(printf %q "$err"))"
# File descriptor leak after 'command not found' with process substitution as argument
err=$(
ulimit -n 25 || exit 0
set +x
PATH=/dev/null
for ((i=1; i<10; i++))
do notfound <(:) >(:) 2> /dev/null
done 2>&1
exit 0
) || err_exit "Process substitution leaks file descriptors when used as argument to nonexistent command" \
"(got $(printf %q "$err"))"
got=$(command -x cat <(command -x echo foo) 2>&1) || err_exit "process substitution doesn't work with 'command'" \
"(got $(printf %q "$got"))"
# ======
# A redirection with a null command could crash under certain circumstances (rhbz#1200534)
"$SHELL" -i >/dev/null 2>&1 -c '