fix setenv
Christopher Faylor
cgf-use-the-mailinglist-please@cygwin.com
Mon Nov 16 16:09:00 GMT 2009
On Mon, Nov 16, 2009 at 06:57:19AM -0700, Eric Blake wrote:
>-----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;
Looks ok. Please check in.
Thanks.
cgf
More information about the Cygwin-patches
mailing list