bug-bash
[Top][All Lists]
Advanced

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

[PATCH] local_builtin: do not overwrite previously saved options with lo


From: Emanuele Torre
Subject: [PATCH] local_builtin: do not overwrite previously saved options with local -
Date: Mon, 6 Feb 2023 15:08:24 +0100

Before this patch, running  local -  a second time in the same scope
would overwrite the previously saved options:

  a () { local -; set -C; echo "2: $-"; local -; set -u; echo "3: $-" ;}
  echo "1: $-"   # 1: himBHs
  a              # 2: himBCHs
                 # 3: himuBCHs
  echo "4: $-"   # 4: himBCHs

With this patch, calling  local -  a second time in the same scope
becomes a no-op:

  a () { local -; set -C; echo "2: $-"; local -; set -u; echo "3: $-" ;}
  echo "1: $-"   # 1: himBHs
  a              # 2: himBCHs
                 # 3: himuBCHs
  echo "4: $-"   # 4: himBHs

This matches the behavior of  local -  in other shells that implement it
(e.g. busybox ash  and  dash)
---
 builtins/declare.def | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/builtins/declare.def b/builtins/declare.def
index f7dac728..ccf4e807 100644
--- a/builtins/declare.def
+++ b/builtins/declare.def
@@ -410,10 +410,13 @@ declare_internal (WORD_LIST *list, int local_var)
       if (local_var && variable_context && STREQ (name, "-"))
        {
          var = make_local_variable ("-", 0);
-         FREE (value_cell (var));              /* just in case */
-         value = get_current_options ();
-         var_setvalue (var, value);
-         VSETATTR (var, att_invisible);
+         /* local -; local -;  is a no-op. */
+         if (value_cell (var) == NULL)
+           {
+             value = get_current_options ();
+             var_setvalue (var, value);
+             VSETATTR (var, att_invisible);
+           }
          NEXT_VARIABLE ();
        }
 
-- 
2.39.1




reply via email to

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