Current-Users archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Some more setenv(3) update
Hi,
Following the recent setenv(3) update, i made the attached patch which
aims the following :
- make it fails with EINVAL for bad name values, just like our
unsetenv(3); following the opengroup online function description.
http://www.opengroup.org/onlinepubs/009695399/functions/setenv.html
Likewise do reject NULL value.
- Do not strip (anymore) a leading `=' in provided value, mostly to
give consistent results with putenv("var==val"). In both case getenv
now return `=val'.
The patch do include the corresponding testcases, and an updated
version of the man page.
Comments ?
--
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 11 Oct 2010 21:22:02 -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 11 Oct 2010 21:22:02 -0000
@@ -60,13 +60,18 @@ 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 ||
+ *name == '\0' || strchr(name, '=') != NULL) {
+ errno = EINVAL;
+ return -1;
+ }
+
if (rwlock_wrlock(&__environ_lock) != 0)
return -1;
@@ -76,8 +81,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 +96,7 @@ setenv(const char *name, const char *val
goto copy;
}
}
- for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
- continue;
- size = cc - name;
+ size = strlen(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 11 Oct 2010 21:22:02 -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)
Home |
Main Index |
Thread Index |
Old Index