Path: csiph.com!weretis.net!feeder8.news.weretis.net!goblin1!goblin.stu.neva.ru!usenet.stanford.edu!not-for-mail From: Binarus Newsgroups: gnu.bash.bug Subject: Incorrect / Inconsistent behavior with nameref assignments in functions Date: Fri, 28 Aug 2020 10:56:34 +0200 Lines: 100 Approved: bug-bash@gnu.org Message-ID: References: NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1598619823 906 209.51.188.17 (28 Aug 2020 13:03:43 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org, bash@packages.debian.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=binarus.de; s=b201601; t=1598604994; bh=Rvn89U9hlRl0hlBe/S7OL6aqPpO4WnwmeN5qpdxr5ZY=; h=To:From:Subject:Date:From:Reply-To:Subject:Date:To:Cc:Resent-Date: Resent-From:Resent-To:Resent-Cc:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner: List-Archive; b=JoCNuL5rcHqJMdEpEUGCp3uS+msjrYzaJsjQVDFrRJQuib2vzZBEjYLtKiJ+UqIIz h5Z9ncZStIvzXsW4vWoJCcI/SwxQiPTfsbC6+qfMF+9B0/OTAUhqYsLuuge2etuo1T byUcVTT9nwOYkUYO5vz3KFvs7yOJQHGtpM2d75Ff/fPF3d2+ocZt4vWqhwgQYM9voV DOhiip7sNcmAHtsd7KH5Mj3DJxYv+r3GTKnKD78Sa/IdMwadmw7pudhwxgjRMjRmee PMap9yW3vk/nClp6X/nRTWsLB9SlBMk3L6AFyGV2RLYltaydHpU7j3qlLLhD9iBTb6 Pp4D1l0d4zpW1h8NpJ7lgkaiqP15IPB2SZrI0gfg5rfWVvzhJ5FcfbgIUuQurc6BtO povO5xHPOuJO3neNFvzj4Hoznk/RuTP/r+okfuVwe2kjWs+hBbpS4tlF9gTDXEZPj/ YO01HzvHUwQAB7QG40U8xqnEb7ZCoYMQljEMifq4tvzA6IZa40KgTlRaHlPLFeJPjt //43TgX1XFGi+Vl5yI7UUpM9EggBN237Gxxk5JB/3BcoN2Gxb3ZAXUeyLjW3m8bisM I5eHYO2noIlxWteWiHu4DTjR3CCtCTP7UOyin0YVoHj2WPQDSaBrstAAWH7exUaBmq CYWkFOyKYS2BRK1kpYmQYSAs= User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 Content-Language: en-US Received-SPF: pass client-ip=144.76.90.229; envelope-from=lists@binarus.de; helo=odysseus.binarus.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/28 04:56:34 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Fri, 28 Aug 2020 09:03:40 -0400 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:16824 Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' -DCONF_VENDOR='pc' -DLOCALEDIR$ uname output: Linux cerberus 4.9.0-13-amd64 #1 SMP Debian 4.9.228-1 (2020-07-05) x86_64 GNU/Linux Machine Type: x86_64-pc-linux-gnu Bash Version: 4.4 Patch Level: 12 Release Status: release Description: ------------ Under certain circumstances, assignments of namerefs to local variables in functions behaves in a way which makes namerefs completely useless. Furthermore, the behavior is not consistent. The two scripts given below really should produce the same output. Instead, the output is different. To reproduce, run the two scripts and observe the difference in the output. In summary, the -a qualifier alters the assignment in an undocumented and surprising way. Both scripts should work identically as expected. Important additional information: I have tried the same with exactly the same results in bash 5.0.3 and bash 5.0.11. Repeat-By: ---------- Consider the following two scripts: SCRIPT 1: #!/bin/bash function Dummy() { local -n namerefArray="$1" local -a -i myArray=("${namerefArray[@]}") local -p } declare -a -i myArray=('1' '2' '3') Dummy 'myArray' SCRIPT 2: #!/bin/bash function Dummy() { local -n namerefArray="$1" local myArray=("${namerefArray[@]}") local -p } declare -a -i myArray=('1' '2' '3') Dummy 'myArray' OUTPUT OF SCRIPT 1: myArray=() namerefArray=myArray OUTPUT OF SCRIPT 2: myArray=([0]="1" [1]="2" [2]="3") namerefArray=myArray That is, in SCRIPT 1, the assignment to myArray in the function destroys the contents of the variable referenced by namerefArray. I think that this a bug, but I am not completely sure about it. However, what I am quite sure about is that the behavior, whether or not it is correct, should not differ between the two variants. The attributes -a and -i in this case should not make any difference, but actually make a fundamental difference. Thank you very much, and best regards, Binarus