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

redirect: check args before executing redirections (re: 7b82c338)

The 'redirect' builtin command did not error out before executing
any valid redirections. For example, 'redirect ls >foo.txt' issued
an "incorrect syntax" error, but still created 'foo.txt' and left
standard output permanently redirected to it.

src/cmd/ksh93/sh/xec.c: sh_exec():
- If we have redirections (io != NULL), and the command is
  SYSREDIR, then check for arguments and error out if there are
  any, before calling sh_redirect() to execute redirections.
  (Note, the other check for arguments in b_exec() in bltins/misc.c
  must be kept, as that applies if there are no redirections.)

src/cmd/ksh93/sh/io.c: sh_redirect():
- Edit comments to better explain what the flag values do.

src/cmd/ksh93/bltins/misc.c:
- Add a dummy b_redirect() function declaration "for the dictionary
  generator" as has historically been done for other builtins that
  share one C function. I'm not sure what that dictionary generator
  is supposed to be, but this also improves greppability.

src/cmd/ksh93/data/builtins.c,
src/cmd/ksh93/sh.1:
- Fix misleading "I/O redirection arguments" term. I/O redirections
  are not arguments at all; no argument parser ever sees them.

src/cmd/ksh93/tests/io.sh:
- Test both conditions that should make 'redirect' produce an
  "incorrect syntax" error.
- Test that any redirections are not executed if erroneous
  non-redirection arguments exist.

src/cmd/ksh93/tests/builtins.sh:
- "... should show usage info on unrecognized options" test:
  Because 'redirect' now refuses to process redirections on error,
  the error message was not captured. The fix is to run the builtin
  in a braces block and add the redirection to the block.
This commit is contained in:
Martijn Dekker 2020-08-08 23:57:57 +01:00
parent e805c7d9b1
commit be5ea8bbb2
9 changed files with 37 additions and 14 deletions

View file

@ -877,7 +877,7 @@ EOF
(
builtin $(builtin -l | awk -F "/" '/\/opt/ {print $5}') # Load all /opt/ast/bin builtins
for name in $(builtin -l | grep -Ev '(echo|/opt|test|true|false|getconf|uname|\[|:)'); do
actual="$($name --this-option-does-not-exist 2>&1)"
actual=$({ "$name" --this-option-does-not-exist; } 2>&1)
expect="Usage: $name"
[[ $actual =~ $expect ]] || err_exit "$name should show usage info on unrecognized options (expected $(printf '%q' "$expect"), got $(printf '%q' "$actual"))"
done

View file

@ -430,6 +430,7 @@ then export LC_ALL=$lc_utf8
done
fi
fi
LC_ALL=C command true # restore correct shellquoting on old ksh: https://github.com/ksh93/ksh/issues/5
file=$tmp/file
(
@ -528,9 +529,14 @@ actual=$(redirect 2>&- 3>&2; echo ok)
[[ $actual == ok ]] || err_exit "redirection error in 'redirect' causes exit"
# Test that 'redirect' does not accept non-redir args
actual=$(redirect ls 2>&1)
expect="*: redirect: incorrect syntax"
[[ $actual == $expect ]] || err_exit "redirect command wrongly accepting non-redir args"
expect=$'*: redirect: incorrect syntax\n status = 2'
actual=$( (redirect /dev/null/foo) 2>&1; echo " status = $?" );
[[ $actual == $expect ]] || err_exit 'redirect command accepts non-redirection argument' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
actual=$( (redirect /dev/null/foo >$tmp/wrong_redirect) 2>&1; echo " status = $?" )
[[ $actual == $expect ]] || err_exit 'redirect command accepts non-redirection argument along with redirection' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
[[ -e $tmp/wrong_redirect ]] && err_exit "redirect command executes redirections before checking arguments"
# ======
# Process substitution