Path: csiph.com!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: andrej@podzimek.org Newsgroups: gnu.bash.bug Subject: Variable references (declare -n, "nameref") are limited to a depth of 8. Date: Sat, 25 Apr 2020 08:55:11 +0200 Lines: 78 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org X-Trace: usenet.stanford.edu 1587797724 13324 209.51.188.17 (25 Apr 2020 06:55:24 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org Received-SPF: pass client-ip=2a02:168:5cd0::; envelope-from=andrej@podzimek.org; helo=charon.podzimek.org X-detected-operating-system: by eggs.gnu.org: Error: [-] PROGRAM ABORT : Malformed IPv6 address (bad octet value). Location : parse_addr6(), p0f-client.c:67 X-Received-From: 2a02:168:5cd0:: X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: Xref: csiph.com gnu.bash.bug:16241 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!'