local -r issue in conjunction with trap

2022-07-15 Thread Robert Stoll
Hi 

Unfortunately I have never built bash on my own and it would probably take me 
too much time to do it. 
Thus, I am writing to this e-mail in the hope that someone more experienced can 
try to reproduce it with the latest version.
I do hope that my bug-report helps nonetheless to improve bash. 

Following a script to reproduce the problem: 

=

#!/usr/bin/env bash
set -e

function trapHook(){
echo "$1"
local -r readonlyVar=$1
}

function test1(){
local -r readonlyVar=2
trap 'trapHook $readonlyVar' EXIT
}
function test2(){
local -r readonlyVar=2
trap 'trapHook $readonlyVar' EXIT
exit 0
}
test1 # works as exit happens outside of test1
test2 # fails with ./src/test.sh: line 6: local: readonlyVar: readonly variable

=

> bash -version


GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


> lsb_release -d 
Description:Ubuntu 20.04.4 LTS

> uname -r
5.15.0-41-generic


Please let me know in case you need further information.

Kind regards,
Robert Stoll

robert.st...@tegonal.com
+41 31 328 33 60

Tegonal Genossenschaft  
Wasserwerkgasse 2
3011 Bern, Switzerland
http://tegonal.com
--
http://twitter.com/tegonal
http://github.com/tegonal



Re: local -r issue in conjunction with trap

2022-07-15 Thread Lawrence Velázquez
On Fri, Jul 15, 2022, at 12:13 PM, Robert Stoll wrote:
> Following a script to reproduce the problem: 
>
> =
>
> #!/usr/bin/env bash
> set -e
>
> function trapHook(){
>   echo "$1"
>   local -r readonlyVar=$1
> }
>
> function test1(){
>   local -r readonlyVar=2
>   trap 'trapHook $readonlyVar' EXIT
> }
> function test2(){
>   local -r readonlyVar=2
>   trap 'trapHook $readonlyVar' EXIT
>   exit 0
> }
> test1 # works as exit happens outside of test1
> test2 # fails with ./src/test.sh: line 6: local: readonlyVar: readonly 
> variable

You can't shadow a readonly variable:

https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00152.html
https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00153.html
https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00201.html
https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00204.html

The trap is incidental -- you can observe the same behavior without
it -- but it's worth noting that the trap command runs in the context
from which the shell exits, not the context in which the trap is
set.

What do you think the bug is, exactly?  How do you think this should
behave?

-- 
vq



Re: local -r issue in conjunction with trap

2022-07-15 Thread Lawrence Velázquez
On Fri, Jul 15, 2022, at 6:26 PM, Lawrence Velázquez wrote:
> it's worth noting that the trap command runs in the context
> from which the shell exits

Sorry, this might not be clear.  Here I am referring to the command
that is provided as the argument to the "trap" utility.

> not the context in which the trap is set.

And here I am referring to the invocation of the "trap" utility
itself.

-- 
vq



Re: local -r issue in conjunction with trap

2022-07-15 Thread Koichi Murase
2022年7月16日(土) 7:28 Lawrence Velázquez :
> You can't shadow a readonly variable:
>
> https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00152.html
> https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00153.html
> https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00201.html
> https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00204.html

We can shadow local readonly variables. What we cannot are global
readonly variables.

It seems to me a bug. While the following works,

$ bash-dev -ec 'T(){ local -r v=; }; trap T 0; F() { local -r v=; exit; }; F'

the following fails

$ bash-dev -e <<< 'T(){ local -r v=; }; trap T 0; F() { local -r v=; exit; }; F'
main: line 1: local: v: readonly variable

Tested on the current devel 87a6e89e (+ define(relstatus, release) in
configure.ac)

--
Koichi



Re: local -r issue in conjunction with trap

2022-07-15 Thread Lawrence Velázquez
On Fri, Jul 15, 2022, at 7:06 PM, Koichi Murase wrote:
> 2022年7月16日(土) 7:28 Lawrence Velázquez :
>> You can't shadow a readonly variable:
>>
>> https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00152.html
>> https://lists.gnu.org/archive/html/bug-bash/2019-03/msg00153.html
>> https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00201.html
>> https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00204.html
>
> We can shadow local readonly variables. What we cannot are global
> readonly variables.

Hm, you are right.  I thought I'd tested that, but I must have done
it wrong.


> It seems to me a bug. While the following works,
>
> $ bash-dev -ec 'T(){ local -r v=; }; trap T 0; F() { local -r v=; exit; }; F'
>
> the following fails
>
> $ bash-dev -e <<< 'T(){ local -r v=; }; trap T 0; F() { local -r v=; exit; }; 
> F'
> main: line 1: local: v: readonly variable
>
> Tested on the current devel 87a6e89e (+ define(relstatus, release) in
> configure.ac)

Same with 5.1.16.  It also fails when run as a script file:

% cat /tmp/foo.bash
set -e

T() {
local -r v=
}

trap T 0

F() {
local -r v
exit
}

F
% bash /tmp/foo.bash
/tmp/foo.bash: line 4: local: v: readonly variable


-- 
vq