Current-Users archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: Some more setenv(3) update
On Wed, Oct 13, 2010 at 04:02:48PM +0100, Matthias Scheler wrote:
> On Wed, Oct 13, 2010 at 03:49:41PM +0200, Nicolas Joly wrote:
> > I plan to commit all of this in a few days unless someone objects.
>
> Can you please publish a final diff?
Sure, attached.
> > And then have a look to putenv which require some care too.
>
> What's wrong with "putenv"? Lack of argument checking?
Exactly. Just try with NULL string, or something like "=foobar", and
it doesn't set errno upon failure.
--
Nicolas Joly
Biological Software and Databanks.
Institut Pasteur, Paris.
Index: lib/libc/stdlib/getenv.3
===================================================================
RCS file: /cvsroot/src/lib/libc/stdlib/getenv.3,v
retrieving revision 1.22
diff -u -p -r1.22 getenv.3
--- lib/libc/stdlib/getenv.3 1 Oct 2010 20:57:50 -0000 1.22
+++ lib/libc/stdlib/getenv.3 13 Oct 2010 15:09:16 -0000
@@ -62,16 +62,14 @@ These functions set, unset and fetch env
host
.Em environment list .
For compatibility with differing environment conventions,
-the given arguments
+the
+.Fn getenv
+or
+.Fn getenv_r
+given argument
.Ar name
-and
-.Ar value
-may be appended and prepended,
-respectively,
-with an equal sign
-.Dq Li \&= ,
-except for
-.Fn unsetenv .
+may be appended with an equal sign
+.Dq Li \&= .
.Pp
The
.Fn getenv
@@ -160,11 +158,18 @@ is successful, the string returned shoul
The
.Fa name
argument to
+.Fn setenv
+or
.Fn unsetenv
is a null pointer, points to an empty string, or points to a string
containing an
.Dq Li \&=
character.
+The
+.Fa value
+argument to
+.Fn setenv
+is a null pointer.
.It Bq Er ENOMEM
The function
.Fn setenv
Index: lib/libc/stdlib/setenv.c
===================================================================
RCS file: /cvsroot/src/lib/libc/stdlib/setenv.c,v
retrieving revision 1.40
diff -u -p -r1.40 setenv.c
--- lib/libc/stdlib/setenv.c 2 Oct 2010 16:56:03 -0000 1.40
+++ lib/libc/stdlib/setenv.c 13 Oct 2010 15:09:16 -0000
@@ -60,13 +60,23 @@ int
setenv(const char *name, const char *value, int rewrite)
{
char *c;
- const char *cc;
size_t l_value, size;
int offset;
_DIAGASSERT(name != NULL);
_DIAGASSERT(value != NULL);
+ if (name == NULL || value == NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ size = strcspn(name, "=");
+ if (size == 0 || name[size] != '\0') {
+ errno = EINVAL;
+ return -1;
+ }
+
if (rwlock_wrlock(&__environ_lock) != 0)
return -1;
@@ -76,8 +86,6 @@ setenv(const char *name, const char *val
if (__allocenv(offset) == -1)
goto bad;
- if (*value == '=') /* no `=' in value */
- ++value;
l_value = strlen(value);
if (c != NULL) {
@@ -93,9 +101,6 @@ setenv(const char *name, const char *val
goto copy;
}
}
- for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
- continue;
- size = cc - name;
/* name + `=' + value */
if ((c = malloc(size + l_value + 2)) == NULL)
goto bad;
Index: tests/lib/libc/stdlib/t_environment.c
===================================================================
RCS file: /cvsroot/src/tests/lib/libc/stdlib/t_environment.c,v
retrieving revision 1.2
diff -u -p -r1.2 t_environment.c
--- tests/lib/libc/stdlib/t_environment.c 1 Oct 2010 20:12:20 -0000
1.2
+++ tests/lib/libc/stdlib/t_environment.c 13 Oct 2010 15:09:16 -0000
@@ -35,6 +35,7 @@
__RCSID("$NetBSD: t_environment.c,v 1.2 2010/10/01 20:12:20 christos Exp $");
#include <atf-c.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -71,6 +72,14 @@ ATF_TC_BODY(t_setenv, tc)
unsetenv(name);
ATF_CHECK(getenv(name) == NULL);
}
+
+ ATF_CHECK_ERRNO(EINVAL, setenv(NULL, "val", 1) == -1);
+ ATF_CHECK_ERRNO(EINVAL, setenv("", "val", 1) == -1);
+ ATF_CHECK_ERRNO(EINVAL, setenv("v=r", "val", 1) == -1);
+ ATF_CHECK_ERRNO(EINVAL, setenv("var", NULL, 1) == -1);
+
+ ATF_CHECK(setenv("var", "=val", 1) == 0);
+ ATF_CHECK_STREQ(getenv("var"), "=val");
}
ATF_TC_BODY(t_putenv, tc)
Index: usr.bin/env/env.c
===================================================================
RCS file: /cvsroot/src/usr.bin/env/env.c,v
retrieving revision 1.17
diff -u -p -r1.17 env.c
--- usr.bin/env/env.c 21 Jul 2008 14:19:22 -0000 1.17
+++ usr.bin/env/env.c 13 Oct 2010 15:09:16 -0000
@@ -54,7 +54,7 @@ extern char **environ;
int
main(int argc, char **argv)
{
- char **ep, *p;
+ char **ep;
char *cleanenv[1];
int ch;
@@ -72,8 +72,8 @@ main(int argc, char **argv)
usage();
}
- for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv)
- (void)setenv(*argv, ++p, 1);
+ for (argv += optind; *argv && strchr(*argv, '=') != NULL; ++argv)
+ (void)putenv(*argv);
if (*argv) {
/* return 127 if the command to be run could not be found; 126
Home |
Main Index |
Thread Index |
Old Index