bash - Function local read-only vs. global read-only variable with the same name -


i suprising behaviour when have function local read-only variable , global read-only variable same name.

when read-only option removed global declaration. i.e.

declare -r var="main" 

is changed to:

declare var="main" 

i expected behaviour. i've been reading bash man page can't find explanation behaviour. please point me section(s) of manual explaining issue ?

i think similar kind of issue how lexical scoping supported in different shell languages? more specific.

details:

$ cat readonly_variable.sh  #!/bin/bash  # expected output: # # bash_version = 3.2.25(1)-release # function # main # # instead getting: # # bash_version = 3.2.25(1)-release # ./readonly_variable.sh: line 6: local: var: readonly variable # main # main # # when read-only option (-r) removed global declaration (*), output # expected  set -o nounset  function func {   local -r var="function"   echo "$var" }  declare -r var="main" # (*)  echo bash_version = $bash_version echo $(func) echo $var  exit 0 

i'm stucked particular bash version.

$ ./readonly_variable.sh bash_version = 3.2.25(1)-release ./readonly_variable.sh: line 24: local: var: readonly variable main main $ 

actually, making local copies of readonly global variables explicitely forbidden security reasons, documented in bash source code (in variables.c:make_local_variable):

the test against old_var's context level disallow local copies of readonly global variables (since "i" believe security hole).

(where "i" not me, i'm merely quoting)

/* since called local/declare/typeset code, can    call builtin_error here without worry (of course, work    sets this_command_name).  variables `noassign'    attribute may not made local.  test against old_var's context    level disallow local copies of readonly global variables (since    believe security hole).  readonly copies of calling    function local variables ok. */ if (old_var && (noassign_p (old_var) ||    (readonly_p (old_var) && old_var->context == 0))) {   if (readonly_p (old_var))     sh_readonly (name);   return ((shell_var *)null); } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -