bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: andrej
Subject: Variable references (declare -n, "nameref") are limited to a depth of 8.
Date: Sat, 25 Apr 2020 08:55:11 +0200

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!'



reply via email to

[Prev in Thread] Current Thread [Next in Thread]