bug-coreutils
[Top][All Lists]
Advanced

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

Re: env -u behavior


From: Eric Blake
Subject: Re: env -u behavior
Date: Mon, 26 Oct 2009 07:12:16 -0600
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Mike Frysinger on 10/24/2009 11:38 PM:
>> But right now we call putenv("a=b"), which puts "a=b" into the environment
>> instead of unsetting "a".
> 
> which is not fine

Glad you agree.  How about this patch?

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkrloDAACgkQ84KuGfSFAYD4cwCdGTkyCnyBrbFDRDAAxZXNI7Pz
wBkAoI7otTRhxe/w85mi0/3G2gFR87Iv
=faCr
-----END PGP SIGNATURE-----
>From 8c70f0e34111cfcd46787f5f989ea84a174b208b Mon Sep 17 00:00:00 2001
From: Eric Blake <address@hidden>
Date: Mon, 26 Oct 2009 07:10:51 -0600
Subject: [PATCH] env: reject bogus -u arguments

* src/env.c (main): Use unsetenv rather than putenv to remove
items from environ, and check for failure.
* tests/misc/env: Test this.
* NEWS: Document it.
---
 NEWS           |    3 +++
 src/env.c      |   12 +++++++++---
 tests/misc/env |   10 ++++++----
 3 files changed, 18 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index 5399229..abbeb27 100644
--- a/NEWS
+++ b/NEWS
@@ -8,6 +8,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   Even then, chcon may still be useful.
   [bug introduced in coreutils-8.0]

+  env -u A=B now fails, rather than silently adding A to the
+  environment.  [the bug dates back to the initial implementation]
+
   md5sum now prints checksums atomically so that concurrent
   processes will not intersperse their output.
   This also affected sum, sha1sum, sha224sum, sha384sum and sha512sum.
diff --git a/src/env.c b/src/env.c
index b69a29a..c021e3a 100644
--- a/src/env.c
+++ b/src/env.c
@@ -83,6 +83,7 @@

 #include "system.h"
 #include "error.h"
+#include "quote.h"

 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "env"
@@ -172,14 +173,19 @@ main (int argc, char **argv)

   optind = 0;                  /* Force GNU getopt to re-initialize. */
   while ((optc = getopt_long (argc, argv, "+iu:", longopts, NULL)) != -1)
-    if (optc == 'u')
-      putenv (optarg);         /* Requires GNU putenv. */
+    if (optc == 'u' && unsetenv (optarg))
+      error (EXIT_CANCELED, errno, _("cannot unset %s"), quote (optarg));

   if (optind < argc && STREQ (argv[optind], "-"))
     ++optind;

   while (optind < argc && strchr (argv[optind], '='))
-    putenv (argv[optind++]);
+    if (putenv (argv[optind++]))
+      {
+        char *name = argv[optind - 1];
+        *(strchr (name, '=')) = '\0';
+        error (EXIT_CANCELED, errno, _("cannot set %s"), quote (name));
+      }

   /* If no program is specified, print the environment and exit. */
   if (argc <= optind)
diff --git a/tests/misc/env b/tests/misc/env
index 1e0a22e..07dd9e4 100755
--- a/tests/misc/env
+++ b/tests/misc/env
@@ -48,7 +48,7 @@ env sh -c 'exit 2' # exit status propagation
 test $? = 2 || fail=2
 env . # invalid command
 test $? = 126 || fail=1
-env ... # no such command
+env no_such # no such command
 test $? = 127 || fail=1

 # Cygwin requires a minimal environment to launch new processes, so a
@@ -105,8 +105,10 @@ esac
 # test "x`env c=d echo fail`" = xfail || fail=1
 # test "x`env -- c=d echo fail`" = xpass || fail=1

-# FIXME - decide whether we like this behavior
-# test `env -i -u a=b` = a=b || fail=1
-# env -u '' true || fail=1
+# catch unsetenv failure, broken through coreutils 8.0
+env -u a=b true && fail=1
+test $? = 125 || fail=1
+env -u '' true && fail=1
+test $? = 125 || fail=1

 Exit $fail
-- 
1.6.5.rc1


reply via email to

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