zsh-workers
 help / color / mirror / code / Atom feed
* [BUG] ksh_zero_subscript messes with associative arrays
@ 2022-07-26 16:18 Felipe Contreras
  2022-07-26 16:32 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Felipe Contreras @ 2022-07-26 16:18 UTC (permalink / raw)
  To: Zsh hackers list

Hello,

The test is very simple:

    setopt ksh_zero_subscript
    typeset -A args
    (( $+args[foo] )) && echo set

ksh_zero_subscript shouldn't be changing the behavior of the code, correct?

I'm using zsh 5.9.

-- 
Felipe Contreras


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] ksh_zero_subscript messes with associative arrays
  2022-07-26 16:18 [BUG] ksh_zero_subscript messes with associative arrays Felipe Contreras
@ 2022-07-26 16:32 ` Peter Stephenson
  2022-07-26 17:00   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2022-07-26 16:32 UTC (permalink / raw)
  To: Zsh hackers list


> On 26/07/2022 17:18 Felipe Contreras <felipe.contreras@gmail.com> wrote:
> The test is very simple:
> 
>     setopt ksh_zero_subscript
>     typeset -A args
>     (( $+args[foo] )) && echo set
> 
> ksh_zero_subscript shouldn't be changing the behavior of the code, correct?

I can't see how it should be having any kind of effect on an associative array,
no

pws

diff --git a/Src/params.c b/Src/params.c
index 27ea82298..f587657c7 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -1996,7 +1996,8 @@ getindex(char **pptr, Value v, int flags)
 		 * altered the start index in getarg().
 		 * Are we being strict?
 		 */
-		if (isset(KSHZEROSUBSCRIPT)) {
+		if (isset(KSHZEROSUBSCRIPT) &&
+		    !(v->pm->node.flags & PM_HASHED)) {
 		    /*
 		     * We're not.
 		     * Treat this as accessing the first element of the
diff --git a/Test/D06subscript.ztst b/Test/D06subscript.ztst
index adbd398c4..2546013a9 100644
--- a/Test/D06subscript.ztst
+++ b/Test/D06subscript.ztst
@@ -224,6 +224,11 @@
 0:(R) yuckily returns the first element on failure with KSH_ZERO_SUBSCRIPT
 >XfimbleX
 
+  typeset -A empty_hash
+  (( $+empty_hash[unset_param] )) || echo Entry is not set
+0:KSH_ZERO_SUBSCRIPT has no effect on hashes
+>Entry is not set
+
   unsetopt KSH_ZERO_SUBSCRIPT
   array[(R)notfound,(r)notfound]=(help help here come the seventies retreads)
   print $array


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] ksh_zero_subscript messes with associative arrays
  2022-07-26 16:32 ` Peter Stephenson
@ 2022-07-26 17:00   ` Bart Schaefer
  2022-07-26 17:10     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2022-07-26 17:00 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

On Tue, Jul 26, 2022 at 9:33 AM Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
>
> > On 26/07/2022 17:18 Felipe Contreras <felipe.contreras@gmail.com> wrote:
> >
> > ksh_zero_subscript shouldn't be changing the behavior of the code, correct?
>
> I can't see how it should be having any kind of effect on an associative array,
> no

Something is tickling my brain to the effect that this was left this
way for bug-compatibility with some version of (perhaps pd) ksh ...
not that it was ever referred to as "bug compatible", just that ksh
behaved that way too.

Doesn't mean we can't change it now, but it nags me.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] ksh_zero_subscript messes with associative arrays
  2022-07-26 17:00   ` Bart Schaefer
@ 2022-07-26 17:10     ` Bart Schaefer
  2022-07-27  8:21       ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2022-07-26 17:10 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

On Tue, Jul 26, 2022 at 10:00 AM Bart Schaefer
<schaefer@brasslantern.com> wrote:
>
> Something is tickling my brain to the effect that this was left this
> way for bug-compatibility with some version of (perhaps pd) ksh ...

More of this comes back to me ... I think the situation is/was this:

hash=( [Y]=why [0]=zero )
echo $hash
zero

That is, reference to the entire hash without subscript returns a
reference to the element with key "0", just as reference to an array
without subscript returns the 0th element.

So there's probably not after all a connection to other unset
elements, but I think PWS's patch will alter the above behavior as
well?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [BUG] ksh_zero_subscript messes with associative arrays
  2022-07-26 17:10     ` Bart Schaefer
@ 2022-07-27  8:21       ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2022-07-27  8:21 UTC (permalink / raw)
  To: Zsh hackers list

> On 26/07/2022 18:10 Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Tue, Jul 26, 2022 at 10:00 AM Bart Schaefer
> <schaefer@brasslantern.com> wrote:
> >
> > Something is tickling my brain to the effect that this was left this
> > way for bug-compatibility with some version of (perhaps pd) ksh ...
> 
> More of this comes back to me ... I think the situation is/was this:
> 
> hash=( [Y]=why [0]=zero )
> echo $hash
> zero
> 
> That is, reference to the entire hash without subscript returns a
> reference to the element with key "0", just as reference to an array
> without subscript returns the 0th element.
> 
> So there's probably not after all a connection to other unset
> elements, but I think PWS's patch will alter the above behavior as
> well?

Yes, I remember this oddity now.

However, "echo $hash" with only kshzerosubscript (not ksharrays ---
with which it would be pointless) echoes the full hash, so no
change here.  But I suspect there are other cases it does affect.

Do we actually care?  Could we just document it?  Or is the effect
so small anyway it's best to leave it the way it is and note the
oddities with hashes?

pws


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-07-27  8:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-26 16:18 [BUG] ksh_zero_subscript messes with associative arrays Felipe Contreras
2022-07-26 16:32 ` Peter Stephenson
2022-07-26 17:00   ` Bart Schaefer
2022-07-26 17:10     ` Bart Schaefer
2022-07-27  8:21       ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).