Skip to content

Commit

Permalink
builtin -d should not delete special builtins
Browse files Browse the repository at this point in the history
The man page for the builtin command says special builtins cannot
be deleted. This wasn't the case though, running `builtin -d` on
a special builtin was deleting it. As an example, the following
set of commands was ending with 'export: not found':

$ builtin -d export
$ export foo=bar

This commit backports the bugfix from ksh93v- (2014-12-24-beta),
which added an error check to prevent special builtins from being
deleted.

src/cmd/ksh93/sh/nvdisc.c:
 - Add an error check to prevent special builtins from being deleted.

src/cmd/ksh93/tests/builtins.sh
 - Add a regression test for using `builtin -d` on special builtins.
  • Loading branch information
JohnoKing committed Jun 12, 2020
1 parent 7b82c33 commit 017d088
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Any uppercase BUG_* names are modernish shell bug IDs.

2020-06-11:

- Fixed a bug that caused running 'builtin -d' on a special builtin to
delete it. The man page for the 'builtin' command documents that special
builtins cannot be deleted.

- POSIX compliance fix: It is now possible to set shell functions named
'alias' or 'unalias', overriding the commands by the same names. In
technical terms, they are now regular builtins, not special builtins.
Expand Down
2 changes: 2 additions & 0 deletions src/cmd/ksh93/sh/nvdisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,8 @@ Namval_t *sh_addbuiltin(const char *path, Shbltin_f bltin, void *extra)
stakseek(offset);
if(extra == (void*)1)
{
if(nv_isattr(np,BLT_SPC))
errormsg(SH_DICT,ERROR_exit(1),"cannot delete: %s%s",name,is_spcbuiltin);
if(np->nvfun && !nv_isattr(np,NV_NOFREE))
free((void*)np->nvfun);
dtdelete(sh.bltin_tree,np);
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/ksh93/tests/builtins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -686,5 +686,11 @@ PATH=$tmp:$PATH $SHELL <<-\EOF || err_exit "'whence' gets wrong path on init"
[[ -x $wc ]]
EOF
# ======
# `builtin -d` should not delete special builtins
(builtin -d export 2> /dev/null
PATH=/dev/null
whence -q export) || err_exit '`builtin -d` deletes special builtins'
# ======
exit $((Errors<125?Errors:125))

0 comments on commit 017d088

Please sign in to comment.