Re: Empty array referenced by indirection reports unbound variable

2022-02-06 Thread konsolebox
On Mon, Feb 7, 2022, 00:31 Chet Ramey,  wrote:

>  From CHANGES:
>
> ...



Well I'm glad.  Thanks again!


--
konsolebox


Re: Empty array referenced by indirection reports unbound variable

2022-02-06 Thread Chet Ramey

On 2/6/22 4:23 AM, konsolebox wrote:

On Thu, Dec 30, 2021 at 4:35 PM Chet Ramey  wrote:

Let's try it. Thanks for the report.


It seems to be already fixed in 5.2-alpha (thanks), but I can't see it
mentioned in the changelog.  Can you kindly confirm if it really is?


From CHANGES:

"bb. Array references using `@' and `*' that are the value of nameref variables
(declare -n ref='v[@]' ; echo $ref) no longer cause the shell to exit if
set -u is enabled and the array (v) is unset."

From the bash change log:

"subst.c
- parameter_brace_expand: when expanding an indirect variable, extend
  the special case for array[@] and array[*] (set -u/no positional
  parameters, obeying the baroque quoting rules) to the value of the
  indirection. Report and fix from konsolebox 
"



--
``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: Empty array referenced by indirection reports unbound variable

2022-02-06 Thread konsolebox
On Thu, Dec 30, 2021 at 4:35 PM Chet Ramey  wrote:
> Let's try it. Thanks for the report.

It seems to be already fixed in 5.2-alpha (thanks), but I can't see it
mentioned in the changelog.  Can you kindly confirm if it really is?


-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-12-30 Thread konsolebox
On Thu, Dec 30, 2021 at 4:35 PM Chet Ramey  wrote:
>
> On 12/27/21 11:37 PM, konsolebox wrote:
> > On Thu, Apr 8, 2021, 06:56 konsolebox,  > > wrote:
> >
> > On Thu, Apr 8, 2021 at 2:44 AM Chet Ramey  > > wrote:
> >  > Indirection does not check whether or not the variable it's 
> > indirecting
> >  > is $@/$* or ${array[@/*]}. It simply goes by the return value.
> >
> > It looks like it can easily be fixed with this:
>
> Let's try it. Thanks for the report.

Thank you very much.  This will finally make `set -u` a completely
reliable function.  If I recall correctly, having want_indir set means
the indirection parameter is already valid, so there's no need to test
for other name-validity checks and go straight to checking the
indirected value.  Anyhow I've been using the patch since April and
have `set -u` set globally and I haven't had any issues related to it
since.


-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-12-30 Thread Chet Ramey

On 12/27/21 11:37 PM, konsolebox wrote:
On Thu, Apr 8, 2021, 06:56 konsolebox, > wrote:


On Thu, Apr 8, 2021 at 2:44 AM Chet Ramey mailto:chet.ra...@case.edu>> wrote:
 > Indirection does not check whether or not the variable it's indirecting
 > is $@/$* or ${array[@/*]}. It simply goes by the return value.

It looks like it can easily be fixed with this:


Let's try it. Thanks for the report.

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/



Re: Empty array referenced by indirection reports unbound variable

2021-12-27 Thread konsolebox
On Thu, Apr 8, 2021, 06:56 konsolebox,  wrote:

> On Thu, Apr 8, 2021 at 2:44 AM Chet Ramey  wrote:
> > Indirection does not check whether or not the variable it's indirecting
> > is $@/$* or ${array[@/*]}. It simply goes by the return value.
>
> It looks like it can easily be fixed with this:
>
> diff --git a/subst.c b/subst.c
> index 4f12f22d..cc9a6803 100644
> --- a/subst.c
> +++ b/subst.c
> @@ -9245,7 +9245,13 @@ parameter_brace_expand (string, indexp, quoted,
> pflags, quoted_dollar_atp, conta
>  }
>
>  #if defined (ARRAY_VARS)
> -  if (valid_array_reference (name, 0))
> +  if (want_indir)
> +{
> +  if (*contains_dollar_at) {
> +all_element_arrayref = 1;
> +  }
> +}
> +  else if (valid_array_reference (name, 0))
>  {
>int qflags;
>char *t;
>

Chet:

Also this one. Any further thoughts? This fix looks simple enough to allow
consistency.

--
konsolebox


Re: Empty array referenced by indirection reports unbound variable

2021-04-07 Thread konsolebox
On Thu, Apr 8, 2021 at 2:44 AM Chet Ramey  wrote:
> Indirection does not check whether or not the variable it's indirecting
> is $@/$* or ${array[@/*]}. It simply goes by the return value.

It looks like it can easily be fixed with this:

diff --git a/subst.c b/subst.c
index 4f12f22d..cc9a6803 100644
--- a/subst.c
+++ b/subst.c
@@ -9245,7 +9245,13 @@ parameter_brace_expand (string, indexp, quoted,
pflags, quoted_dollar_atp, conta
 }

 #if defined (ARRAY_VARS)
-  if (valid_array_reference (name, 0))
+  if (want_indir)
+{
+  if (*contains_dollar_at) {
+all_element_arrayref = 1;
+  }
+}
+  else if (valid_array_reference (name, 0))
 {
   int qflags;
   char *t;


-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-04-07 Thread Chet Ramey

On 4/7/21 10:39 AM, konsolebox wrote:

On Wed, Apr 7, 2021 at 9:25 PM Chet Ramey  wrote:


On 4/5/21 4:45 PM, konsolebox wrote:

set -u
array=()
__ref=array[@]
: "${array[@]}" # Reports nothing


This is a special case, mirroring the special case that POSIX carved out
for $@ and $* in 2009.


: "${!__ref}" # Unbound variable


But this is not. Bash looks for __ref, finds it with a value of array[@],
and attempts to expand that. It expands to null (internally), since there
are no array elements, which becomes the value of the parameter expansion.

Since the expansion (${!__ref}) resulted in the null string, `set -u' kicks
in and bash reports a fatal error.


But that doesn't seem consistent since `set -- "${!__ref}"; echo "$#"`
shows 0, meaning it didn't expand to a null string but practically
nothing.  


The null string *is* nothing. Literally. 0x0. It's an ex-parrot.


Does that mean internally ${!__ref} expands to null string
but checks for a flag if it's an array expansion before the actual
resulting expansion? 


No. It just expands to a null string.


If that's the case shouldn't it be the same when
reporting unbound variable?


Indirection does not check whether or not the variable it's indirecting
is $@/$* or ${array[@/*]}. It simply goes by the return value.

--
``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: Empty array referenced by indirection reports unbound variable

2021-04-07 Thread Alex fxmbsw7 Ratchev
set sets args, and exits null
no bug there

On Wed, Apr 7, 2021, 16:40 konsolebox  wrote:

> On Wed, Apr 7, 2021 at 9:25 PM Chet Ramey  wrote:
> >
> > On 4/5/21 4:45 PM, konsolebox wrote:
> > > set -u
> > > array=()
> > > __ref=array[@]
> > > : "${array[@]}" # Reports nothing
> >
> > This is a special case, mirroring the special case that POSIX carved out
> > for $@ and $* in 2009.
> >
> > > : "${!__ref}" # Unbound variable
> >
> > But this is not. Bash looks for __ref, finds it with a value of array[@],
> > and attempts to expand that. It expands to null (internally), since there
> > are no array elements, which becomes the value of the parameter
> expansion.
> >
> > Since the expansion (${!__ref}) resulted in the null string, `set -u'
> kicks
> > in and bash reports a fatal error.
>
> But that doesn't seem consistent since `set -- "${!__ref}"; echo "$#"`
> shows 0, meaning it didn't expand to a null string but practically
> nothing.  Does that mean internally ${!__ref} expands to null string
> but checks for a flag if it's an array expansion before the actual
> resulting expansion?  If that's the case shouldn't it be the same when
> reporting unbound variable?
>
>
> --
> konsolebox
>
>


Re: Empty array referenced by indirection reports unbound variable

2021-04-07 Thread konsolebox
On Wed, Apr 7, 2021 at 9:25 PM Chet Ramey  wrote:
>
> On 4/5/21 4:45 PM, konsolebox wrote:
> > set -u
> > array=()
> > __ref=array[@]
> > : "${array[@]}" # Reports nothing
>
> This is a special case, mirroring the special case that POSIX carved out
> for $@ and $* in 2009.
>
> > : "${!__ref}" # Unbound variable
>
> But this is not. Bash looks for __ref, finds it with a value of array[@],
> and attempts to expand that. It expands to null (internally), since there
> are no array elements, which becomes the value of the parameter expansion.
>
> Since the expansion (${!__ref}) resulted in the null string, `set -u' kicks
> in and bash reports a fatal error.

But that doesn't seem consistent since `set -- "${!__ref}"; echo "$#"`
shows 0, meaning it didn't expand to a null string but practically
nothing.  Does that mean internally ${!__ref} expands to null string
but checks for a flag if it's an array expansion before the actual
resulting expansion?  If that's the case shouldn't it be the same when
reporting unbound variable?


-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-04-07 Thread Chet Ramey

On 4/5/21 4:45 PM, konsolebox wrote:

set -u
array=()
__ref=array[@]
: "${array[@]}" # Reports nothing


This is a special case, mirroring the special case that POSIX carved out
for $@ and $* in 2009.


: "${!__ref}" # Unbound variable


But this is not. Bash looks for __ref, finds it with a value of array[@],
and attempts to expand that. It expands to null (internally), since there
are no array elements, which becomes the value of the parameter expansion.

Since the expansion (${!__ref}) resulted in the null string, `set -u' kicks
in and bash reports a fatal error.

--
``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: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread konsolebox
On Tue, Apr 6, 2021 at 11:28 PM Robert Elz  wrote:
>   | @ means everything or nothing. Unbound variable errors should not apply 
> to it.
>
> I know nothing about namerefs and arrays and stuff like that,
> but did you consider the possibility that after
>
> array=()
>
> then
>
> array[@]
>
> is nothing, and so
>
> __ref=array[@]
>
> is just
>
> __ref=
>
> and consequently ${!__ref} is attempting to use '' as the name of a
> variable?

References are virtually like placeholders and don't expand the
referenced target themselves.

${!__ref} is synonymous to ${array[@]}, not ${${array[@]}}.

This is why you can do other parameter expansion features like
${!var+.} ${!var@a}.

-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread Robert Elz
Date:Tue, 6 Apr 2021 22:39:55 +0800
From:konsolebox 
Message-ID:  


  | @ means everything or nothing. Unbound variable errors should not apply to 
it.

I know nothing about namerefs and arrays and stuff like that,
but did you consider the possibility that after

array=()

then

array[@]

is nothing, and so

__ref=array[@]

is just

__ref=

and consequently ${!__ref} is attempting to use '' as the name of a
variable?

kre




Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread Alex fxmbsw7 Ratchev
im sorry i dont support set -u cant discuss it

On Tue, Apr 6, 2021, 16:40 konsolebox  wrote:

> On Tue, Apr 6, 2021 at 10:38 PM Alex fxmbsw7 Ratchev 
> wrote:
> >
> > what i meant was there is not one element defined so .. any reference to
> anything in it will be not set.. makes sense @ is not defined
> > i remember !var worked with it, or at least other defined sub elements
>
> @ means everything or nothing. Unbound variable errors should not apply to
> it.
>
> --
> konsolebox
>


Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread konsolebox
On Tue, Apr 6, 2021 at 10:38 PM Alex fxmbsw7 Ratchev  wrote:
>
> what i meant was there is not one element defined so .. any reference to 
> anything in it will be not set.. makes sense @ is not defined
> i remember !var worked with it, or at least other defined sub elements

@ means everything or nothing. Unbound variable errors should not apply to it.

-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread Alex fxmbsw7 Ratchev
what i meant was there is not one element defined so .. any reference to
anything in it will be not set.. makes sense @ is not defined
i remember !var worked with it, or at least other defined sub elements

On Tue, Apr 6, 2021, 16:26 konsolebox  wrote:

> On Tue, Apr 6, 2021 at 9:57 PM Alex fxmbsw7 Ratchev 
> wrote:
> >
> > arr=( ) implies no [0]=
>
> Also `arr=()` does not imply `arr[0]=`. `arr[0]` remains unset and not
> assigned to any. Not even an empty string.
>
> --
> konsolebox
>


Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread konsolebox
On Tue, Apr 6, 2021 at 9:57 PM Alex fxmbsw7 Ratchev  wrote:
>
> arr=( ) implies no [0]=

Also `arr=()` does not imply `arr[0]=`. `arr[0]` remains unset and not
assigned to any. Not even an empty string.

-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread konsolebox
On Tue, Apr 6, 2021 at 9:57 PM Alex fxmbsw7 Ratchev  wrote:
>
> arr=( ) implies no [0]=

arr[0] is not exactly being referenced here.


-- 
konsolebox



Re: Empty array referenced by indirection reports unbound variable

2021-04-06 Thread Alex fxmbsw7 Ratchev
arr=( ) implies no [0]=

On Mon, Apr 5, 2021, 22:46 konsolebox  wrote:

> set -u
> array=()
> __ref=array[@]
> : "${array[@]}" # Reports nothing
> : "${!__ref}" # Unbound variable
>
> Using bash version 5.1.4. I know this can be avoided by using namerefs
> instead but indirection is more portable when no assignment is needed
> and this sometimes allows my portable functions to have less versions.
>
> The detection of unbound variables seem to have become more reasonable
> since 5.0 and I hope it gets improved further.  I don't use set -u in
> older bash and it's only for development mode.
>
> --
> konsolebox
>
>