1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 11:42:21 +00:00

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
This commit is contained in:
Martijn Dekker 2021-04-08 06:30:22 +01:00
parent d0a5cab1ab
commit 3667aa4f71
2 changed files with 11 additions and 1 deletions

View file

@ -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();

9
src/cmd/ksh93/tests/readonly.sh Normal file → Executable file
View file

@ -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))