52692: local typeset of the name of a named reference hides the reference

This commit is contained in:
Bart Schaefer 2024-03-05 21:13:33 -08:00
parent b56250e9b9
commit 330821de01
5 changed files with 68 additions and 13 deletions

View file

@ -1,3 +1,10 @@
2024-03-05 Bart Schaefer <schaefer@zsh.org>
* 52692: Doc/Zsh/params.yo, Src/builtin.c, Src/params.c,
Test/K01nameref.ztst: local declaration (typeset) of the
name of a named reference hides the reference rather than
following it. Also fix two related crash bugs.
2024-03-05 Stephane Chazelas <stephane@chazelas.org>
* 52685: Doc/Zsh/restricted.yo: fix typo in the name of bash's

View file

@ -670,8 +670,9 @@ This manual was generated with Zsh tt(version()).
When a em(named reference) is created with `tt(typeset -n)', all uses
of var(pname) in assignments and expansions instead assign to or
expand var(rname). This also applies to `tt(unset )var(pname)' and to
most subsequent uses of `tt(typeset)' with the exception of
`tt(typeset -n)' and `tt(typeset +n)', so to remove a named reference,
most subsequent uses of `tt(typeset)' with the exceptions of declaring
a local in a called function, or updating a current-scope parameter with
`tt(typeset -n)' or `tt(typeset +n)'. Thus to remove a named reference,
use either `tt(unset -n )var(pname)' (preferred) or one of:
ifzman()
example(tt(typeset -n )var(pname=)

View file

@ -2030,11 +2030,10 @@ typeset_single(char *cname, char *pname, Param pm, int func,
int usepm, tc, keeplocal = 0, newspecial = NS_NONE, readonly, dont_set = 0;
char *subscript;
if (pm && (pm->node.flags & PM_NAMEREF) && !((off|on) & PM_NAMEREF)) {
if (!(off & PM_NAMEREF)) {
if ((pm = (Param)resolve_nameref(pm, NULL)))
pname = pm->node.nam;
}
if (pm && (pm->node.flags & PM_NAMEREF) && !((off|on) & PM_NAMEREF) &&
(pm->level == locallevel || !(on & PM_LOCAL))) {
if ((pm = (Param)resolve_nameref(pm, NULL)))
pname = pm->node.nam;
if (pm && (pm->node.flags & PM_NAMEREF) &&
(on & ~(PM_NAMEREF|PM_LOCAL|PM_READONLY))) {
/* Changing type of PM_SPECIAL|PM_AUTOLOAD is a fatal error. *
@ -3125,8 +3124,10 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func)
oldpm->u.str)
asg->value.scalar = dupstring(oldpm->u.str);
/* Defer read-only error to typeset_single() */
if (!(hn->flags & PM_READONLY))
if (!(hn->flags & PM_READONLY)) {
unsetparam_pm(oldpm, 0, 1);
hn = NULL;
}
}
/* Passing a NULL pm to typeset_single() makes the
* nameref read-only before assignment, which breaks
@ -3134,7 +3135,7 @@ bin_typeset(char *name, char **argv, LinkList assigns, Options ops, int func)
* so this is special-cased to permit that action
* like assign-at-create for other parameter types.
*/
if (!(hn->flags & PM_READONLY))
if (hn && !(hn->flags & PM_READONLY))
hn = NULL;
}
}

View file

@ -1034,7 +1034,8 @@ createparam(char *name, int flags)
}
if (oldpm && !(flags & PM_NAMEREF) &&
(!(oldpm->node.flags & PM_RO_BY_DESIGN) || !(flags & PM_LOCAL)) &&
(oldpm->level == locallevel ?
!(oldpm->node.flags & PM_RO_BY_DESIGN) : !(flags & PM_LOCAL)) &&
(oldpm->node.flags & PM_NAMEREF) &&
(oldpm = upscope(oldpm, oldpm->base))) {
Param lastpm;

View file

@ -51,9 +51,19 @@
0:remove nameref attribute
>typeset ptr=var
typeset -n ptr
typeset -t ptr
typeset -p ptr
typeset -n ptr=gvar
() {
local ptr
typeset -p ptr
}
typeset -p ptr
0:Local non-reference hides outside reference
>typeset ptr
>typeset -n ptr=gvar
typeset -n ptr
typeset -t ptr
typeset -p ptr
0:change type of a placeholder
F:Other type changes are fatal errors, should this also be?
>typeset -n ptr=''
@ -845,4 +855,39 @@ F:previously this could create an infinite recursion and crash
1:create nameref by pattern match not allowed
*?*typeset:1: invalid reference
#
# The following tests are run in interactive mode, using PS1 as an
# assignable special with side-effects. This crashed at one time.
#
# Note bypassing TYPESET_TO_UNSET here
$ZTST_testdir/../Src/zsh -fis <<<$'
typeset -n p=PS1
() {
typeset -p p
local p
typeset -p p
p=xx
typeset -p p
}
'
0:regression: assign to local that shadows global named reference
>typeset -g -n p=PS1
>typeset p=''
>typeset p=xx
*?*
# Note bypassing TYPESET_TO_UNSET here
$ZTST_testdir/../Src/zsh -fis <<<$'
() {
typeset p=PS1
typeset -n p
p=zz
}
typeset -p PS1
'
0:regression - converting a string into a named reference
>typeset PS1=zz
*?*
%clean