This is the mail archive of the cygwin-patches mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

fix setenv


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

I noticed that cygwin setenv differs from Linux and from POSIX
requirements.  STC:

#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
int
main (void)
{
  /* Test overwriting.  */
  assert (setenv ("a", "=", -1) == 0);
  assert (setenv ("a", "2", 0) == 0);
  assert (strcmp (getenv ("a"), "=") == 0); // fails here

  /* Required to fail with EINVAL.  */
  errno = 0;
  assert (setenv ("", "", 1) == -1); // and here
  assert (errno == EINVAL);
  errno = 0;
  assert (setenv ("a=b", "", 0) == -1); // and here
  assert (errno == EINVAL);
  errno = 0;
  assert (setenv (NULL, "", 0) == -1);
  assert (errno == EINVAL); // and here
  return 0;
}


2009-11-16  Eric Blake  <ebb9@byu.net>

	* environ.cc (setenv): Detect invalid argument.
	(unsetenv): Distinguish EFAULT from EINVAL.

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

Eric Blake             ebb9@byu.net
-----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/

iEYEARECAAYFAksBWj4ACgkQ84KuGfSFAYCwngCdG+FVRKgMjTzXnn0AKhRzPCh3
OxsAn35wV0l/J8Q4AAKrAyqPvMmfyGQB
=LaHp
-----END PGP SIGNATURE-----
diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index bc11303..4935bc8 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -413,10 +413,11 @@ setenv (const char *name, const char *value, int overwrite)
   myfault efault;
   if (efault.faulted (EFAULT))
     return -1;
-  if (!*name)
-    return 0;
-  if (*value == '=')
-    value++;
+  if (!name || !*name || strchr (name, '='))
+    {
+      set_errno (EINVAL);
+      return -1;
+    }
   return _addenv (name, value, !!overwrite);
 }

@@ -427,7 +428,9 @@ unsetenv (const char *name)
   register char **e;
   int offset;
   myfault efault;
-  if (efault.faulted () || *name == '\0' || strchr (name, '='))
+  if (efault.faulted (EFAULT))
+    return -1;
+  if (!name || *name == '\0' || strchr (name, '='))
     {
       set_errno (EINVAL);
       return -1;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]