Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16241

Variable references (declare -n, "nameref") are limited to a depth of 8.

From andrej@podzimek.org
Newsgroups gnu.bash.bug
Subject Variable references (declare -n, "nameref") are limited to a depth of 8.
Date 2020-04-25 08:55 +0200
Message-ID <mailman.1172.1587797724.3066.bug-bash@gnu.org> (permalink)
References <courier.000000005EA3DECF.0000B702@charon.podzimek.org>

Show all headers | View raw


Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/bin' -DSTANDARD_UTILS_PATH='/usr/bin' -DSYS_BASHRC='/etc/bash.bashrc' -DSYS_BASH_LOGOUT='/etc/bash.bash_logout' -DNON_INTERACTIVE_LOGIN_SHELLS -Wno-parentheses -Wno-format-security
uname output: Linux charon 5.6.4-arch1-1-user-regd #1 SMP PREEMPT Fri, 17 Apr 2020 12:06:27 +0000 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.0
Patch Level: 16
Release Status: release

Description:

  While looking for a way to share a "cache" array with a recursive function
  call stack (using local -n (nameref)), I hit a well-known problem with
  "circular name reference" (which has been around for a long time). The problem
  can be circumvented by renaming the variable twice per recursion level:

  recursive_function() {
    ...
    local -r cache_name="cache_${#FUNCNAME[@]}"
    local -n "$cache_name"="$1"
    local -n cache="$cache_name"
    ...
    recursive_function "$cache_name" 'other' 'arguments' ...
    ...
  }

  Now "${cache[@]}" references the shared variable under a consistent name,
  regardless call stack depth, and without the "circular name reference" issue.

  Sadly, this stops working at a certain (very small) call stack depth. There is
  no warning or error message; the ${cache[@]} just becomes empty unexpectedly.

  Importantly, this is also reproducible with a plain string variable and
  without any functions or (recursive) calls involved:

Repeat-By:

  previous=a
  declare ${previous}='This is set!'
  for var in {b..k}; do
    declare -n ${var}=${previous}
    previous="${var}"
  done
  for var in {a..k}; do
    echo "${var}: '${!var}'"
  done

Actual output:

  a: 'This is set!'
  b: 'This is set!'
  c: 'This is set!'
  d: 'This is set!'
  e: 'This is set!'
  f: 'This is set!'
  g: 'This is set!'
  h: 'This is set!'
  i: 'This is set!'
  j: ''
  k: ''

Expected output:

  a: 'This is set!'
  b: 'This is set!'
  c: 'This is set!'
  d: 'This is set!'
  e: 'This is set!'
  f: 'This is set!'
  g: 'This is set!'
  h: 'This is set!'
  i: 'This is set!'
  j: 'This is set!'
  k: 'This is set!'

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Variable references (declare -n, "nameref") are limited to a depth of 8. andrej@podzimek.org - 2020-04-25 08:55 +0200

csiph-web