bug-bash
[Top][All Lists]
Advanced

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

setarray[unsetkey] doesn't trigger nounset in arithmetic context


From: Emanuele Torre
Subject: setarray[unsetkey] doesn't trigger nounset in arithmetic context
Date: Sun, 21 May 2023 02:00:44 +0200
User-agent: Mutt/2.2.10 (2023-03-25)

This was already reported 12 and a half years ago, but there have not
been many replies:

  https://lists.gnu.org/archive/html/bug-bash/2010-12/msg00128.html

The issue seems to be caused by expr_streval() only checking for unbound
identifiers at this line:

  https://git.savannah.gnu.org/cgit/bash.git/tree/expr.c?h=devel#n1168
  if ((v == 0 || invisible_p (v)) && unbound_vars_is_error)

But then, after resolving the variable if the identifier is a array or
associative array variable, bash doesn't check that the result is set,
and just defaults to 0 if it isn't set at this line:

  https://git.savannah.gnu.org/cgit/bash.git/tree/expr.c?h=devel#n1219
  tval = (value && *value) ? subexpr (value) : 0;

This makes bash not trigger nounset if the identifier is a set
variable/array, even thought idenitifer[subscript] is not set.

  $ bash -uc 'printf %s\\n "$(( a ))"'
  bash: line 1: a: unbound variable
  $ bash -uc 'printf %s\\n "$(( a[1] ))"'
  bash: line 1: a: unbound variable
  $ a=xyz bash -uc 'printf %s\\n "$(( a[1] ))"'
  0
  $ bash -uc 'a=(1 2); let "a[10]"'
  0
  $ a=xyz bash -uc 'printf %s\\n "${a[1]}"'
  bash: line 1: a[1]: unbound variable
  $ bash -uc 'a=(1 2); printf %s\\n "${a[10]}"'
  bash: line 1: a[10]: unbound variable

o/
 emanuele6



reply via email to

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