summaryrefslogtreecommitdiffstats
path: root/Src
diff options
context:
space:
mode:
authorPhilippe Altherr <philippe.altherr@gmail.com>2025-11-09 15:28:13 -0800
committerBart Schaefer <schaefer@zsh.org>2025-11-09 15:28:13 -0800
commit0d76a82c77706e6ab2adbb91d1d306824dfa6f33 (patch)
treea890a308c0199568c3dec5d53db84c6d6154e0a6 /Src
parentgithub #148: complete fortune databases (diff)
downloadzsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.tar
zsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.tar.gz
zsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.tar.bz2
zsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.tar.lz
zsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.tar.xz
zsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.tar.zst
zsh-0d76a82c77706e6ab2adbb91d1d306824dfa6f33.zip
54057: enable assignment through named reference to change type of the referent
Diffstat (limited to 'Src')
-rw-r--r--Src/params.c68
-rw-r--r--Src/zsh.h7
2 files changed, 58 insertions, 17 deletions
diff --git a/Src/params.c b/Src/params.c
index 5a0434e40..b76fb8a6b 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2203,6 +2203,7 @@ fetchvalue(Value v, char **pptr, int bracks, int flags)
} else {
Param pm;
int isvarat;
+ int isrefslice = 0;
isvarat = (t[0] == '@' && !t[1]);
if (flags & SCANPM_NONAMEREF)
@@ -2250,6 +2251,7 @@ fetchvalue(Value v, char **pptr, int bracks, int flags)
flags |= SCANPM_NOEXEC;
*ss = sav;
s = dyncat(ss,*pptr);
+ isrefslice = 1;
} else
s = *pptr;
}
@@ -2264,7 +2266,7 @@ fetchvalue(Value v, char **pptr, int bracks, int flags)
v->isarr = SCANPM_ARRONLY;
}
v->pm = pm;
- v->flags = 0;
+ v->flags = isrefslice ? VALFLAG_REFSLICE : 0;
v->start = 0;
v->end = -1;
if (bracks > 0 && (*s == '[' || *s == Inbrack)) {
@@ -3222,12 +3224,18 @@ assignsparam(char *s, char *val, int flags)
if (!(v = getvalue(&vbuf, &s, 1))) {
createparam(t, PM_SCALAR);
created = 1;
- } else if ((((v->pm->node.flags & PM_ARRAY) && !(flags & ASSPM_AUGMENT)) ||
- (v->pm->node.flags & PM_HASHED)) &&
- !(v->pm->node.flags & (PM_SPECIAL|PM_TIED)) &&
- unset(KSHARRAYS)) {
- unsetparam(t);
- createparam(t, PM_SCALAR);
+ } else if ((((v->pm->node.flags & PM_ARRAY) &&
+ !(v->flags & VALFLAG_REFSLICE) &&
+ !(flags & ASSPM_AUGMENT)) ||
+ (v->pm->node.flags & PM_HASHED)) &&
+ !(v->pm->node.flags & (PM_SPECIAL|PM_TIED)) &&
+ unset(KSHARRAYS)) {
+ if (resetparam(v->pm, PM_SCALAR)) {
+ unqueue_signals();
+ zsfree(val);
+ errflag |= ERRFLAG_ERROR;
+ return NULL;
+ }
/* not regarded as a new creation */
v = NULL;
}
@@ -3378,7 +3386,8 @@ assignaparam(char *s, char **val, int flags)
createparam(t, PM_ARRAY);
created = 1;
} else if (!(PM_TYPE(v->pm->node.flags) & (PM_ARRAY|PM_HASHED)) &&
- !(v->pm->node.flags & (PM_SPECIAL|PM_TIED))) {
+ !(v->flags & VALFLAG_REFSLICE) &&
+ !(v->pm->node.flags & (PM_SPECIAL|PM_TIED))) {
int uniq = v->pm->node.flags & PM_UNIQUE;
if ((flags & ASSPM_AUGMENT) && !(v->pm->node.flags & PM_UNSET)) {
/* insert old value at the beginning of the val array */
@@ -3391,8 +3400,12 @@ assignaparam(char *s, char **val, int flags)
free(val);
val = new;
}
- unsetparam(t);
- createparam(t, PM_ARRAY | uniq);
+ if (resetparam(v->pm, PM_ARRAY | uniq)) {
+ unqueue_signals();
+ freearray(val);
+ errflag |= ERRFLAG_ERROR;
+ return NULL;
+ }
v = NULL;
}
}
@@ -3600,11 +3613,15 @@ sethparam(char *s, char **val)
if (!(v = fetchvalue(&vbuf, &s, 1, SCANPM_ASSIGNING))) {
createparam(t, PM_HASHED);
checkcreate = 1;
- } else if (!(PM_TYPE(v->pm->node.flags) & PM_HASHED)) {
+ } else if (!(PM_TYPE(v->pm->node.flags) & PM_HASHED) &&
+ !(v->flags & VALFLAG_REFSLICE)) {
if (!(v->pm->node.flags & PM_SPECIAL)) {
- unsetparam(t);
- /* no WARNCREATEGLOBAL check here as parameter already existed */
- createparam(t, PM_HASHED);
+ if (resetparam(v->pm, PM_HASHED)) {
+ unqueue_signals();
+ freearray(val);
+ errflag |= ERRFLAG_ERROR;
+ return NULL;
+ }
v = NULL;
} else {
zerr("%s: can't change type of a special parameter", t);
@@ -3761,6 +3778,29 @@ setiparam_no_convert(char *s, zlong val)
return assignsparam(s, ztrdup(buf), ASSPM_WARN);
}
+/* Reset a parameter */
+
+/**/
+mod_export int
+resetparam(Param pm, int flags)
+{
+ char *s = pm->node.nam;
+ queue_signals();
+ if (pm != (Param)(paramtab == realparamtab ?
+ /* getnode2() to avoid autoloading */
+ paramtab->getnode2(paramtab, s) :
+ paramtab->getnode(paramtab, s))) {
+ unqueue_signals();
+ zerr("can't change type of hidden variable: %s", s);
+ return 1;
+ }
+ s = dupstring(s);
+ unsetparam_pm(pm, 0, 1);
+ unqueue_signals();
+ createparam(s, flags);
+ return 0;
+}
+
/* Unset a parameter */
/**/
diff --git a/Src/zsh.h b/Src/zsh.h
index ebb63f498..993108a8c 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -742,18 +742,19 @@ struct multio {
/* lvalue for variable assignment/expansion */
struct value {
- int isarr;
Param pm; /* parameter node */
+ char **arr; /* cache for hash turned into array */
+ int isarr;
int flags; /* flags defined below */
int start; /* first element of array slice, or -1 */
int end; /* 1-rel last element of array slice, or -1 */
- char **arr; /* cache for hash turned into array */
};
enum {
VALFLAG_INV = 0x0001, /* We are performing inverse subscripting */
VALFLAG_EMPTY = 0x0002, /* Subscripted range is empty */
- VALFLAG_SUBST = 0x0004 /* Substitution, so apply padding, case flags */
+ VALFLAG_SUBST = 0x0004, /* Substitution, so apply padding, case flags */
+ VALFLAG_REFSLICE= 0x0008 /* Value is a reference to an array slice */
};
#define MAX_ARRLEN 262144