Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Assigning an array to a nameref "placeholder"



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@xxxxxxxx>
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





Messages sorted by: Reverse Date, Date, Thread, Author