sh: Make sure defined functions can actually be called.

Add some conservative checks on function names:
- Disallow expansions or quoting characters; these can only be called via
  strange control characters
- Disallow '/'; these functions cannot be called anyway, as exec.c assumes
  they are pathnames
- Make the CTL* bytes work properly in function names.

These are syntax errors.

POSIX does not require us to support more than names (letters, digits and
underscores, not starting with a digit), but I do not want to restrict it
that much at this time.

Exp-run done by:	pav (with some other sh(1) changes)
This commit is contained in:
Jilles Tjoelker 2010-10-24 20:45:13 +00:00
parent 3dec7d0c15
commit 074e83b14e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=214291
2 changed files with 32 additions and 3 deletions

View File

@ -639,10 +639,14 @@ simplecmd(union node **rpp, union node *redir)
if (readtoken() != TRP)
synexpect(TRP);
funclinno = plinno;
#ifdef notdef
if (! goodname(n->narg.text))
/*
* - Require plain text.
* - Functions with '/' cannot be called.
*/
if (!noexpand(n->narg.text) || quoteflag ||
strchr(n->narg.text, '/'))
synerror("Bad function name");
#endif
rmescapes(n->narg.text);
n->type = NDEFUN;
n->narg.next = command();
funclinno = 0;

View File

@ -0,0 +1,25 @@
# $FreeBSD$
# POSIX does not require these bytes to work in function names,
# but making them all work seems a good goal.
failures=0
unset LC_ALL
export LC_CTYPE=en_US.ISO8859-1
i=128
set -f
while [ "$i" -le 255 ]; do
c=$(printf \\"$(printf %o "$i")")
ok=0
eval "$c() { ok=1; }"
$c
ok1=$ok
ok=0
"$c"
if [ "$ok" != 1 ] || [ "$ok1" != 1 ]; then
echo "Bad results for character $i" >&2
: $((failures += 1))
fi
unset -f $c
i=$((i+1))
done
exit $((failures > 0))