From 3667aa4f71ff1d585c7f9144054ce6499ffeebd1 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 8 Apr 2021 06:30:22 +0100 Subject: [PATCH] Make readonly variables exportable again (re: 264ba48b) $ /usr/local/bin/ksh -c 'readonly v=1; export v' /usr/local/bin/ksh: export: v: is read only Every POSIX shell (even zsh, as of 5.8) allows this. So did ksh, until the referenced commit. src/cmd/ksh93/bltins/typeset.c: setall(): - Allow setting attributes on a readonly variable if any of NV_ASSIGN (== NV_NOFREE), NV_EXPORT or NV_RDONLY are the only flag bits that are set. This allows readonly, export, typeset -r, typeset -x, and typeset -rx on variable arguments without an assignment. Note that NV_ASSIGN is set for the first variable argument even though it is not an assignment, so we must allow it. The logic (or lack thereof) of that is yet to be worked out. src/cmd/ksh93/tests/readonly.sh: - Tests. Resolves: https://github.com/ksh93/ksh/issues/258 --- src/cmd/ksh93/bltins/typeset.c | 3 ++- src/cmd/ksh93/tests/readonly.sh | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) mode change 100644 => 100755 src/cmd/ksh93/tests/readonly.sh diff --git a/src/cmd/ksh93/bltins/typeset.c b/src/cmd/ksh93/bltins/typeset.c index 80761dd47..fe7d036c4 100644 --- a/src/cmd/ksh93/bltins/typeset.c +++ b/src/cmd/ksh93/bltins/typeset.c @@ -732,7 +732,8 @@ static int setall(char **argv,register int flag,Dt_t *troot,struct tdata *tp np = nv_open(name,troot,nvflags|((nvflags&NV_ASSIGN)?0:NV_ARRAY)|((iarray|(nvflags&(NV_REF|NV_NOADD)==NV_REF))?NV_FARRAY:0)); if(!np) continue; - if(np->nvflag&NV_RDONLY && !tp->pflag && (flag & ~NV_NOFREE) != NV_RDONLY) + if(np->nvflag&NV_RDONLY && !tp->pflag + && (flag & ~(NV_ASSIGN|NV_RDONLY|NV_EXPORT))) /* allow readonly/export on readonly vars */ { errormsg(SH_DICT,ERROR_exit(1),e_readonly,nv_name(np)); UNREACHABLE(); diff --git a/src/cmd/ksh93/tests/readonly.sh b/src/cmd/ksh93/tests/readonly.sh old mode 100644 new mode 100755 index cb8c29b11..778c54090 --- a/src/cmd/ksh93/tests/readonly.sh +++ b/src/cmd/ksh93/tests/readonly.sh @@ -331,5 +331,14 @@ do done unset i n got rtests +# ====== +# readonly variables should still accept setting the readonly or export attributes +# https://github.com/ksh93/ksh/issues/258 +(readonly v=1; readonly v) 2>/dev/null || err_exit "readonly variable cannot be set readonly (1)" +(readonly v=1; typeset -r v) 2>/dev/null || err_exit "readonly variable cannot be set readonly (2)" +(readonly v=1; export v) 2>/dev/null || err_exit "readonly variable cannot be exported (1)" +(readonly v=1; typeset -x v) 2>/dev/null || err_exit "readonly variable cannot be exported (2)" +(readonly v=1; typeset -rx v) 2>/dev/null || err_exit "readonly variable cannot be set readonly and exported" + # ====== exit $((Errors<125?Errors:125))