zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane@chazelas.org>
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Assigning an array to a nameref "placeholder"
Date: Tue, 27 Feb 2024 06:48:07 +0000	[thread overview]
Message-ID: <20240227064807.xa77q6qjek77nekq@chazelas.org> (raw)
In-Reply-To: <CAH+w=7YPkNSiSC_mp-THHvV-fV_rnbrrhVy6Q00ybeszVPUeyw@mail.gmail.com>

2024-02-26 14:18:03 -0800, Bart Schaefer:
> Another one from the ksh93u+ gitlab:
> 
> > nameref unsetref; unsetref+=(foo bar). This now produces a "removing nameref attribute" warning before performing the assignment.
> 
> I would rather make this an error, but it's probably not difficult
> either way.  Comments?

An error (or forbidding typeset -n ref=var without the =var part
like in mksh) would make a lot more sense to me as well.

The full commit log (see below) and
https://github.com/ksh93/ksh/issues/678#issuecomment-1716507741
gives the rationale there for not doing so.

In zsh, there's no backward compatibility to be considered as
the feature is new, so it's our best opportunity to make it the
best interface from the get go.

Note that bash has been known not to always make the best design
choices (including when copying other shells), so it's not
necessarily a good idea to use it as a reference.

Issuing a warning here would only make sense as preserving
backward compatibility while at the same warning users against
doing anything like that.

commit ebfbe62863c36a7b99eb0aa9f831184cdde86f3f
Author: Martijn Dekker <martijn@inlv.org>
Date:   Wed Sep 13 11:25:28 2023 +0100

    Fix segfault in unsetref+=(foo); add warning

    Emanuele Torre (@emanuele6) writes:
    > Appending a non-empty indexed array to an unset nameref crashes
    > with a segmentation fault:
    > $ arch/*/bin/ksh -c 'typeset -n ref; ref+=(); typeset -p ref'
    > typeset -n ref
    > $ arch/*/bin/ksh -c 'typeset -n ref; ref+=(foo); typeset -p ref'
    > Segmentation fault (core dumped)
    [...]
    > Appending an associative array errors correctly:
    > $ arch/*/bin/ksh -c 'typeset -n ref; ref+=([foo]=bar); typeset -p ref'
    > arch/linux.i386-64/bin/ksh: ref: no reference name

    For consistency, this commit makes all array assignments to an
    unset nameref (using either = or +=) succeed, but print a warning
    that the nameref attribute is removed -- a bash-like bahaviour.

    I think that this will cause fewer backward compatibility problems
    than making such assignments error out, since they used to succeed
    in some cases (e.g. 'typeset -n ref; ref=(foo bar)' always worked).

    It does mean the associative array reproducer above now generates a
    warning instead of an error.

    src/cmd/ksh93/sh/name.c: nv_create():
    - After dereferencing a nameref, if nv_isref(np) is still true, we
      know it must be an unset nameref. In that case, add a check for
      c==0 (the value occurring upon var=(foo) and var+=(foo)), and
      produce a warning instead of an error, as argued above.

    Resolves: https://github.com/ksh93/ksh/issues/678



  parent reply	other threads:[~2024-02-27  6:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-26 22:18 Bart Schaefer
2024-02-27  2:51 ` Bart Schaefer
2024-02-27  4:56   ` [PATCH] Appending an array to a scalar Bart Schaefer
2024-02-27  6:48 ` Stephane Chazelas [this message]
2024-02-28  2:10   ` Assigning an array to a nameref "placeholder" Bart Schaefer
2024-02-28  5:28     ` Stephane Chazelas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240227064807.xa77q6qjek77nekq@chazelas.org \
    --to=stephane@chazelas.org \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).