Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH] Fix "unset local-environment" when clearenv not available
Date: Wed, 10 Dec 2025 09:24:02 -0700	[thread overview]
Message-ID: <20251210162402.2026139-1-tromey@adacore.com> (raw)

Tom de Vries pointed out that clearenv isn't available on all hosts.
Since this seems like a niche command at best, it seemed fine to
disable this command on such platforms.
---
 gdb/config.in                        |  3 +++
 gdb/configure                        |  1 +
 gdb/configure.ac                     |  1 +
 gdb/infcmd.c                         | 15 ++++++++++++++-
 gdb/testsuite/gdb.base/local-env.exp | 24 +++++++++++++++++++-----
 5 files changed, 38 insertions(+), 6 deletions(-)

diff --git a/gdb/config.in b/gdb/config.in
index 9e2bba42e28..62d9e0eda6d 100644
--- a/gdb/config.in
+++ b/gdb/config.in
@@ -110,6 +110,9 @@
    the CoreFoundation framework. */
 #undef HAVE_CFPREFERENCESCOPYAPPVALUE
 
+/* Define to 1 if you have the `clearenv' function. */
+#undef HAVE_CLEARENV
+
 /* Define if compiling support to gdb compile. */
 #undef HAVE_COMPILE
 
diff --git a/gdb/configure b/gdb/configure
index cc446c6e4c8..dcf4fee40dd 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -30123,6 +30123,7 @@ $as_echo "#define AC_APPLE_UNIVERSAL_BUILD 1" >>confdefs.h
 
 for ac_func in  \
   btowc \
+  clearenv \
   getgid \
   getpgid \
   getrlimit \
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 1b9939b0754..3f44d9e684b 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -1458,6 +1458,7 @@ AC_C_BIGENDIAN
 
 AC_CHECK_FUNCS([ \
   btowc \
+  clearenv \
   getgid \
   getpgid \
   getrlimit \
diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index 3ba286738a0..ed4fb819faf 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -2146,12 +2146,25 @@ unset_var_in_environment (gdb_environ *env, const char *var, int from_tty)
 {
   if (var == 0)
     {
+      /* If there is no clearenv, don't bother asking the question.  */
+#ifndef HAVE_CLEARENV
+      if (env == nullptr)
+	from_tty = 0;
+#endif
+
       /* 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? ")))
 	{
+	  /* This was handled above.  */
 	  if (env == nullptr)
-	    clearenv ();
+	    {
+#ifdef HAVE_CLEARENV
+	      clearenv ();
+#else
+	      error (_("Cannot clear the local environment on this host."));
+#endif
+	    }
 	  else
 	    env->clear ();
 	}
diff --git a/gdb/testsuite/gdb.base/local-env.exp b/gdb/testsuite/gdb.base/local-env.exp
index fedcf9bc3e4..5bb639e2842 100644
--- a/gdb/testsuite/gdb.base/local-env.exp
+++ b/gdb/testsuite/gdb.base/local-env.exp
@@ -59,12 +59,26 @@ gdb_test "show local-environment EDITOR" \
     "confirm unset environment variable worked"
 
 # Verify that we can unset all environment variables.
-gdb_test "unset local-environment" "" "unset all environment variables" \
-    "Delete all environment variables. .y or n. $" \
-    "y"
+# Note that on some platforms this is not possible.
+set can_unset_all 0
+# Disable confirmation so we don't have to deal with the question.
+gdb_test_multiple "with confirm off -- unset local-environment" \
+    "unset all environment variables" {
+	-re -wrap "Cannot clear the local environment on this host." {
+	    # Nothing.
+	    pass $gdb_test_name
+	}
+
+	-re "\r\n$gdb_prompt $" {
+	    set can_unset_all 1
+	    pass $gdb_test_name
+	}
+    }
 
-gdb_test_no_output "show local-environment" \
-    "all environment variables have been unset"
+if {$can_unset_all} {
+    gdb_test_no_output "show local-environment" \
+	"all environment variables have been unset"
+}
 
 # Verify that we can set a specific environment variable.
 test_set_show_env_var "EDITOR" "emacs" "set environment variable"

base-commit: 7979dbef836b1c8035b9fe9dd6e09c342d85a5e2
-- 
2.52.0


             reply	other threads:[~2025-12-10 16:24 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-10 16:24 Tom Tromey [this message]
2025-12-11  8:40 ` Tom de Vries
2025-12-11 15:43   ` Tom de Vries
2025-12-11  8:44 ` Tom de Vries
2025-12-11 13:55   ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251210162402.2026139-1-tromey@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox