]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Allow NULL to be passed to environment-related functions
authorTom Tromey <tromey@adacore.com>
Wed, 11 Dec 2024 18:25:08 +0000 (11:25 -0700)
committerTom Tromey <tromey@adacore.com>
Tue, 9 Dec 2025 18:48:45 +0000 (11:48 -0700)
This changse the various environment-related helper functions to
accept a NULL environment pointer.  This special case means that the
function should affect gdb's global environment.

Approved-By: Andrew Burgess <aburgess@redhat.com>
Reviewed-by: Kévin Le Gouguec <legouguec@adacore.com>
gdb/infcmd.c

index 185e5770501603e2f92538fc129882fb5f68953c..d8a62d0bffc777f983a733465a02c1f85a6bd93b 100644 (file)
@@ -1999,15 +1999,17 @@ info_program_command (const char *args, int from_tty)
 }
 \f
 
-/* A helper function that prints some info from ENV.  VAR is either
-   NULL, or the name of the variable to display.  */
+/* A helper function that prints some info from ENV.  If ENV is
+   nullptr, then the host environment is used; otherwise the provided
+   environment is used.  VAR is either NULL, or the name of the
+   variable to display.  */
 
 static void
 display_environment (const gdb_environ *env, const char *var)
 {
   if (var)
     {
-      const char *val = env->get (var);
+      const char *val = env == nullptr ? getenv (var) : env->get (var);
 
       if (val)
        {
@@ -2025,7 +2027,9 @@ display_environment (const gdb_environ *env, const char *var)
     }
   else
     {
-      char **envp = env->envp ();
+      char **envp = env == nullptr ? environ : env->envp ();
+      if (envp == nullptr)
+       return;
 
       for (int idx = 0; envp[idx] != nullptr; ++idx)
        {
@@ -2101,10 +2105,18 @@ set_var_in_environment (gdb_environ *env, const char *arg)
       gdb_printf (_("Setting environment variable "
                    "\"%s\" to null value.\n"),
                  var.c_str ());
-      env->set (var.c_str (), "");
+      if (env == nullptr)
+       setenv (var.c_str (), "", 1);
+      else
+       env->set (var.c_str (), "");
     }
   else
-    env->set (var.c_str (), val);
+    {
+      if (env == nullptr)
+       setenv (var.c_str (), val, 1);
+      else
+       env->set (var.c_str (), val);
+    }
 }
 
 static void
@@ -2125,10 +2137,20 @@ unset_var_in_environment (gdb_environ *env, const char *var, int from_tty)
       /* If there is no argument, delete all environment variables.
         Ask for confirmation if reading from the terminal.  */
       if (!from_tty || query (_("Delete all environment variables? ")))
-       env->clear ();
+       {
+         if (env == nullptr)
+           clearenv ();
+         else
+           env->clear ();
+       }
     }
   else
-    env->unset (var);
+    {
+      if (env == nullptr)
+       unsetenv (var);
+      else
+       env->unset (var);
+    }
 }
 
 static void