bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: declare -A +A


From: Grisha Levit
Subject: Re: declare -A +A
Date: Mon, 12 Feb 2024 18:40:05 -0500

On Mon, Feb 12, 2024 at 4:13 PM Chet Ramey <chet.ramey@case.edu> wrote:
>
> On 2/7/24 2:23 AM, Grisha Levit wrote:
> > If a single declare command both sets and unsets the array or assoc
> > attribute for an existing scalar variable, the `value' member of the
> > SHELL_VAR is assigned an ARRAY* or HASH_TABLE* as appropriate, but
> > later ends up treated as a char*:
> >
> >      $ bash-asan -c 'X=Y; declare -A +A X; declare -p X'
> >      declare -- X=$'\200_\020\005\001'
> >
> >      $ bash-lsan -c 'X=Y; declare -A +A X; X=(Z)'
> >      ERROR: LeakSanitizer: detected memory leaks
> >      Direct leak of 1024 byte(s) in 1 object(s) allocated from:
> >          #0 0xaaaada7342f8 in malloc
> >          #1 0xaaaada7f24d0 in xmalloc xmalloc.c:107:10
> >          #2 0xaaaada7b81fc in hash_create hashlib.c:72:25
> >          #3 0xaaaada7cc0c8 in convert_var_to_assoc arrayfunc.c:117:10
> >
> > Maybe it would be appropriate to reject a request to turn off an
> > attribute that is being turned on?
>
> Only for indexed and associative arrays, since those are the attributes
> that cause changes in the underlying value storage format. That's different
> than turning the integer attribute on and off, for instance.
>
> Should it be an actual error, or should the shell just cancel out the
> attribute change requests and go on? What do folks think?

I think it's not obvious what canceling out would mean, e.g. in

    unset X; X=Y; declare -A +A X=(Z)

would canceling out +A and -A mean that X is converted to an indexed
array? Would it be an error if X already is an indexed array?

And should the order of options matter? It might seem natural to follow
the behavior of `set' and apply the options in order, having

    unset X; X=Y; declare +A -A X=(Z)

make an associative array -- but there may be too many complications if
vars of multiple types are modified in the same command.

And just to add to the test cases: while the above produce 'cannot
destroy...' errors, the following segfaults:

    unset X; X=Y; declare +A -A 'X=(Z)'
    Segmentation fault: 11

I don't really have a concrete suggestion, it just seems difficult to
devise and implement intuitive and consistent behavior for these option
combinations, so rejecting them seemed like a safe enough approach.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]