From cc737f7b738d72528d44dbabbd8c42c23b113b19 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 10 Dec 2025 07:23:37 -0700 Subject: [PATCH] Fix "unset local-environment" when clearenv not available 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. Reviewed-By: Tom de Vries --- 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..c62ea746635 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 -wrap "" { + 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" -- 2.47.3