Re: [PATCH] Restrict named directories to scalar parameters.

Philippe Altherr <[email protected]> Tue, 16 Jun 2026 22:43:38 +0200
Newsgroups gmane.comp.shells.zsh.devel
Message-ID <CAGdYchuTkvc3P95yPgzpuwooRuQppKCrErqk-sb6HueCiQ_miQ@mail.gmail.com>
Here is an updated patch where the test uses the null PS1 trick.

- Restrict named directories to scalar parameters
<https://github.com/zsh-users/zsh/compare/master...paltherr:zsh:restrict-nameddirs-to-scalars>

Philippe
restrict-nameddirs-to-scalars.txt (text/plain, 2 KB)
diff --git a/Src/params.c b/Src/params.c
index e5bbe9cb9..8df594837 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -3975,7 +3975,7 @@ floatsetfn(Param pm, double x)
     pm->u.dval = x;
 }
 
-/* Function to get value of a scalar (string) parameter */
+/* Function to get value of a scalar (string) or nameref parameter */
 
 /**/
 mod_export char *
@@ -3984,7 +3984,7 @@ strgetfn(Param pm)
     return pm->u.str ? pm->u.str : (char *) hcalloc(1);
 }
 
-/* Function to set value of a scalar (string) parameter */
+/* Function to set value of a scalar (string) or nameref parameter */
 
 /**/
 mod_export void
@@ -3995,6 +3995,7 @@ strsetfn(Param pm, char *x)
 	pm->u.str = x;
     }
     if (!(pm->node.flags & PM_HASHELEM) && !pm->level &&
+	(PM_TYPE(pm->node.flags) == PM_SCALAR) &&
 	((pm->node.flags & PM_NAMEDDIR) || isset(AUTONAMEDIRS))) {
 	pm->node.flags |= PM_NAMEDDIR;
 	adduserdir(pm->node.nam, x, 0, 0);
diff --git a/Test/K01nameref.ztst b/Test/K01nameref.ztst
index 6cfe6d1d0..d8f7915e4 100644
--- a/Test/K01nameref.ztst
+++ b/Test/K01nameref.ztst
@@ -1575,6 +1575,36 @@ F:previously this could create an infinite recursion and crash
 >typeset PS1=zz
 *?*
 
+# Testing named directories requires to run in interactive mode
+
+ PS1= $ZTST_testdir/../Src/zsh -fis <<<'
+   typeset v1=/1 v2
+   typeset -n r1=v1 r2
+   { echo "~v1:" ~v1 } 2>&1
+   { echo "~r1:" ~r1 } 2>&1
+   echo "nameddirs=( ${(kv)nameddirs} )"
+   r1=/A
+   echo "nameddirs=( ${(kv)nameddirs} )"
+   hash -d v2=/2 r2=/R
+   echo "nameddirs=( ${(kv)nameddirs} )"
+   setopt autonamedirs
+   v2=V2
+   echo "nameddirs=( ${(kv)nameddirs} )"
+   r2=v2
+   echo "nameddirs=( ${(kv)nameddirs} )"
+   r2=/B
+   echo "nameddirs=( ${(kv)nameddirs} )"
+ '
+0:Only string parameters are promoted to named directories
+>~v1: /1
+>zsh: no such user or named directory: r1
+>nameddirs=( v1 /1 )
+>nameddirs=( v1 /A )
+>nameddirs=( v1 /A v2 /2 r2 /R )
+>nameddirs=( v1 /A r2 /R )
+>nameddirs=( v1 /A r2 /R )
+>nameddirs=( v1 /A v2 /B r2 /R )
+
  unset var1 var2
  typeset -n ref1=var1
  () {