Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [bug?] word based string subscripting past the end
- X-seq: zsh-workers 54773
- From: dana <dana@xxxxxxx>
- To: "Stephane Chazelas" <stephane@xxxxxxxxxxxx>
- Cc: "Zsh hackers list" <zsh-workers@xxxxxxx>
- Subject: Re: [bug?] word based string subscripting past the end
- Date: Sun, 14 Jun 2026 16:41:01 +0000
- Archived-at: <https://zsh.org/workers/54773>
- Feedback-id: i9be146f9:Fastmail
- In-reply-to: <ai6ZVCz2l7ZtxSXs@chazelas.org>
- List-id: <zsh-workers.zsh.org>
- References: <ai6ZVCz2l7ZtxSXs@chazelas.org>
On Sun 14 Jun 2026, at 12:15, Stephane Chazelas wrote:
> I'd have expected an empty output like for character
> subscripting or array subscripting:
so would i. and i can't find anything in the documentation that contradicts
that expectation
this makes it work like $arr[n] does
dana
diff --git a/Src/params.c b/Src/params.c
index e5bbe9cb9..c361527be 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -1621,15 +1621,15 @@ getarg(char **str, int *inv, Value v, int a2, zlong *w,
}
if (word && !v->scanflags) {
s = t = getstrvalue(v);
+ if (!s || !*s)
+ return 0;
+
i = wordcount(s, sep, 0);
if (r < 0)
r += i + 1;
- if (r < 1)
- r = 1;
- if (r > i)
- r = i;
- if (!s || !*s)
+ if (r < 1 || r > i)
return 0;
+
while ((d = findword(&s, sep)) && --r);
if (!d)
return 0;
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index 57cdc027c..42748577d 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -308,3 +308,18 @@ F:In math, (($i)) should be the same as ((i)), see workers/47748.
F:Regression test for inconsistency of failed (i) on zero-length string
>2
>1
+
+ string='foo bar baz'
+ for 1 in 0 1 2 3 4 -1 -2 -3 -4; do
+ print -r - $1 $string[(w)$1]
+ done
+0:(w)
+>0
+>1 foo
+>2 bar
+>3 baz
+>4
+>-1 baz
+>-2 bar
+>-3 foo
+>-4
Messages sorted by:
Reverse Date,
Date,
Thread,
Author