Re: pop_var_context msg when eval code with errexit set

2022-10-13 Thread Chet Ramey

On 10/12/22 10:25 AM, Chet Ramey wrote:


 Then run this test script with 'errexit' option set:

 $ ./bash -e test_script
 test_script: line 5: pop_var_context: head of shell_variables not
a function context


It's an internal message warning that something might be wrong. We'll see
if something is.


In this case, it doesn't indicate that anything is wrong. The shell has
flushed all the local variable contexts because it's going to exit due
to errexit being enabled. I'll make a change to suppress the message in
this instance.

Chet

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




-v no longer works for associative arrays

2022-10-13 Thread Bryan Roessler
Configuration Information [Automatically generated, do not change]:

Machine: x86_64

OS: linux-musl

Compiler: gcc

Compilation CFLAGS: -g -O2

uname output: Linux d1a7b22c3308 5.19.14-200.fc36.x86_64 #1 SMP
PREEMPT_DYNAMIC Wed Oct
Machine Type: x86_64-pc-linux-musl



Bash Version: 5.2

Patch Level: 2

Release Status: release



Description:

bash-5.2# declare -A a
bash-5.2# a[foo]=bar
bash-5.2# [[ -v a[@] ]]; echo $?
1

bash-5.1# declare -A a
bash-5.1# a[foo]=bar
bash-5.1# [[ -v a[@] ]]; echo $?
0

If this is by design I missed it.


Repeat-By:

See above


Re: -v no longer works for associative arrays

2022-10-13 Thread Chet Ramey

On 10/13/22 10:42 AM, Bryan Roessler wrote:


Bash Version: 5.2
Patch Level: 2
Release Status: release

Description:

bash-5.2# declare -A a
bash-5.2# a[foo]=bar
bash-5.2# [[ -v a[@] ]]; echo $?
1


From CHANGES:

j. Associative array assignment and certain instances of referencing (e.g.,
   `test -v') now allow `@' and `*' to be used as keys.

There are other ways to test whether or not an associative array has any
set elements, but there was no way to use `*' or `@' as a key in previous
versions.

There was a long discussion about this back in early 2021. Some of the
relevant messages about `@' and `*' keys start here:

https://lists.gnu.org/archive/html/bug-bash/2021-04/msg00058.html

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: -v no longer works for associative arrays

2022-10-13 Thread Greg Wooledge
On Thu, Oct 13, 2022 at 10:42:02AM -0400, Bryan Roessler wrote:
> Description:
> 
> bash-5.2# declare -A a
> bash-5.2# a[foo]=bar
> bash-5.2# [[ -v a[@] ]]; echo $?
> 1
> 
> bash-5.1# declare -A a
> bash-5.1# a[foo]=bar
> bash-5.1# [[ -v a[@] ]]; echo $?
> 0
> 
> If this is by design I missed it.

unicorn:~$ bash-5.2
unicorn:~$ declare -A a=([@]=at [foo]=bar)
unicorn:~$ [[ -v a[@] ]]; echo $?
0

What are you even trying to do there?  Determine whether the associative
array has 1 or more elements?  You can use ${#a[@]} for that, just like
with indexed arrays.

unicorn:~$ echo "${#a[@]}"
2



Re: -v no longer works for associative arrays

2022-10-13 Thread Oğuz
13 Ekim 2022 Perşembe tarihinde Chet Ramey  yazdı:

> There are other ways to test whether or not an associative array has any
> set elements, but there was no way to use `*' or `@' as a key in previous
> versions.
>

 test -v seems broken anyway

$ declare -A foo=(a 42)
$ declare -a bar=(42)
$ test -v foo; echo $?
1
$ test -v bar; echo $?
0


-- 
Oğuz


Re: -v no longer works for associative arrays

2022-10-13 Thread Chet Ramey

On 10/13/22 12:06 PM, Oğuz wrote:


  test -v seems broken anyway

$ declare -A foo=(a 42)
$ declare -a bar=(42)
$ test -v foo; echo $?
1
$ test -v bar; echo $?
0


You know that referencing an array variable without a subscript is
equivalent to referencing element 0 (or "0").

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: -v no longer works for associative arrays

2022-10-13 Thread Oğuz
13 Ekim 2022 Perşembe tarihinde Chet Ramey  yazdı:
>
> You know that referencing an array variable without a subscript is
> equivalent to referencing element 0 (or "0").
>

Yes, but I didn't know that it applied to test -v as well.

Thanks for the reply


-- 
Oğuz


Re: -v no longer works for associative arrays

2022-10-13 Thread Bryan Roessler
For indexed arrays, yes. I hadn't made the connection with associative
arrays since I don't typically index them with numbers, thanks.

$ declare -A f[0]=true
$ declare -A g[foo]=bar
$ test -v f; echo $?
0
$ test -v g; echo $?
1

On Thu, Oct 13, 2022 at 12:10 PM Chet Ramey  wrote:

> On 10/13/22 12:06 PM, Oğuz wrote:
>
> >   test -v seems broken anyway
> >
> > $ declare -A foo=(a 42)
> > $ declare -a bar=(42)
> > $ test -v foo; echo $?
> > 1
> > $ test -v bar; echo $?
> > 0
>
> You know that referencing an array variable without a subscript is
> equivalent to referencing element 0 (or "0").
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/
>
>


Re: -v no longer works for associative arrays

2022-10-13 Thread Chet Ramey

On 10/13/22 12:47 PM, Bryan Roessler wrote:
For indexed arrays, yes. I hadn't made the connection with associative 
arrays since I don't typically index them with numbers, thanks.


There has to be some default, and "0" is at least consistent with indexed
arrays.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/




Re: -v no longer works for associative arrays

2022-10-13 Thread Greg Wooledge
On Thu, Oct 13, 2022 at 02:35:55PM -0400, Chet Ramey wrote:
> On 10/13/22 12:47 PM, Bryan Roessler wrote:
> > For indexed arrays, yes. I hadn't made the connection with associative
> > arrays since I don't typically index them with numbers, thanks.
> 
> There has to be some default, and "0" is at least consistent with indexed
> arrays.

I actually would have preferred an error message.  Something like
"bash: test: missing array index" would have been clear enough for me.

The current behavior is absolutely mystifying for most people.

I'm not actually requesting a change.  I recognize that it's far too
late for that.  Hmm...

If at some point you choose to add a shopt that turns on extra warnings,
I'd love it if *this* could be included in said warnings.  The use of an
array variable with no index, having index "0" be chosen for you, is
worthy of a warning, in such a hypothetical mode.



Bash segfaults when using extglob on an empty var

2022-10-13 Thread Justin Wood (Callek)
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -O2 -flto=auto -ffat-lto-objects -fexceptions -g
-grecord-gcc-switches -pipe -Wall -Werror=format-security
-Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS
-specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong
-specs=/usr/lib/rpm/redhat/redhat-annobin-cc1  -m64  -mtune=generic
-fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection
uname output: Linux lyra 5.19.14-200.fc36.x86_64 #1 SMP PREEMPT_DYNAMIC Wed
Oct 5 21:31:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-redhat-linux-gnu

Bash Version: 5.2
Patch Level: 2
Release Status: release

Description:
Bash segfaults when using extglob on an empty var. Regression in 5.2.2
compared to 5.1.16

#0  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:74
#1  0x55cbe3eb4cd5 in pat_subst (string=string@entry=0x55cbe62cf690 "",
pat=0x55cbe62cf821 "*([.])",
rep=rep@entry=0x0, mflags=mflags@entry=65) at
/usr/src/debug/bash-5.2.2-1.fc36.x86_64/subst.c:8960
#2  0x55cbe3eb5737 in parameter_brace_patsub
(varname=varname@entry=0x55cbe6296780
"foo",
value=value@entry=0x55cbe62959b0 "", estatep=estatep@entry=0x7ffeada25340,
patsub=,
patsub@entry=0x55cbe62cf560 "#*([.])", quoted=quoted@entry=0,
pflags=pflags@entry=8, flags=0)
at /usr/src/debug/bash-5.2.2-1.fc36.x86_64/subst.c:9216
#3  0x55cbe3eb6ffa in parameter_brace_expand
(string=string@entry=0x55cbe6295c70
"${foo/#*([.])}",
indexp=indexp@entry=0x7ffeada253f4, quoted=quoted@entry=0,
pflags=pflags@entry=8,
quoted_dollar_atp=quoted_dollar_atp@entry=0x7ffeada254d8,
contains_dollar_at=contains_dollar_at@entry=0x7ffeada254d0)
at /usr/src/debug/bash-5.2.2-1.fc36.x86_64/subst.c:9960
#4  0x55cbe3eb92dd in param_expand (string=string@entry=0x55cbe6295c70
"${foo/#*([.])}",
sindex=sindex@entry=0x7ffeada254dc, quoted=quoted@entry=0,
expanded_something=expanded_something@entry=0x0,
contains_dollar_at=contains_dollar_at@entry=0x7ffeada254d0,
quoted_dollar_at_p=quoted_dollar_at_p@entry=0x7ffeada254d8,
had_quoted_null_p=0x7ffeada254d4, pflags=8)
at /usr/src/debug/bash-5.2.2-1.fc36.x86_64/subst.c:10522

Repeat-By:

shopt -s extglob
foo=
foo=${foo/#*([.])} || echo sad
#  parens required to reproduce, but unnecessary otherwise.