From 017d088c39e6db71538946ae6362d04606242409 Mon Sep 17 00:00:00 2001 From: Johnothan King Date: Thu, 11 Jun 2020 20:18:01 -0700 Subject: [PATCH] `builtin -d` should not delete special builtins 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. --- NEWS | 4 ++++ src/cmd/ksh93/sh/nvdisc.c | 2 ++ src/cmd/ksh93/tests/builtins.sh | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/NEWS b/NEWS index ea6fb0fee..b837cd928 100644 --- a/NEWS +++ b/NEWS @@ -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. diff --git a/src/cmd/ksh93/sh/nvdisc.c b/src/cmd/ksh93/sh/nvdisc.c index b8a741a9c..db027fad8 100644 --- a/src/cmd/ksh93/sh/nvdisc.c +++ b/src/cmd/ksh93/sh/nvdisc.c @@ -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); diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index 0fd7a2810..e270a11d1 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -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))