summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2016-04-25 21:36:05 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2016-04-25 21:36:05 +0000
commitaae5c70806945fa9a95252b2361000f19575e1fd (patch)
tree6b91902efd74c0a447c450b6165a67af74dcee95
parent7e2b7905c90fc7a824c346d83af69d3b3cf68920 (diff)
Allow setenv(3) and putenv(3) to operate on a NULL environ pointer.
The getenv(3) and unsetenv(3) functions already support this. This will make it easier to emulate the glibc clearenv() function in ports. Based on a diff from and OK jca@
-rw-r--r--lib/libc/stdlib/setenv.c26
1 files changed, 15 insertions, 11 deletions
diff --git a/lib/libc/stdlib/setenv.c b/lib/libc/stdlib/setenv.c
index 1182abdaa34..60c2cd46a65 100644
--- a/lib/libc/stdlib/setenv.c
+++ b/lib/libc/stdlib/setenv.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: setenv.c,v 1.17 2016/03/13 18:34:21 guenther Exp $ */
+/* $OpenBSD: setenv.c,v 1.18 2016/04/25 21:36:04 millert Exp $ */
/*
* Copyright (c) 1987 Regents of the University of California.
* All rights reserved.
@@ -43,7 +43,7 @@ int
putenv(char *str)
{
char **P, *cp;
- size_t cnt;
+ size_t cnt = 0;
int offset = 0;
for (cp = str; *cp && *cp != '='; ++cp)
@@ -65,13 +65,15 @@ putenv(char *str)
}
/* create new slot for string */
- for (P = environ; *P != NULL; P++)
- ;
- cnt = P - environ;
+ if (environ != NULL) {
+ for (P = environ; *P != NULL; P++)
+ ;
+ cnt = P - environ;
+ }
P = reallocarray(lastenv, cnt + 2, sizeof(char *));
if (!P)
return (-1);
- if (lastenv != environ)
+ if (lastenv != environ && environ != NULL)
memcpy(P, environ, cnt * sizeof(char *));
lastenv = environ = P;
environ[cnt] = str;
@@ -122,15 +124,17 @@ setenv(const char *name, const char *value, int rewrite)
break;
}
} else { /* create new slot */
- size_t cnt;
+ size_t cnt = 0;
- for (P = environ; *P != NULL; P++)
- ;
- cnt = P - environ;
+ if (environ != NULL) {
+ for (P = environ; *P != NULL; P++)
+ ;
+ cnt = P - environ;
+ }
P = reallocarray(lastenv, cnt + 2, sizeof(char *));
if (!P)
return (-1);
- if (lastenv != environ)
+ if (lastenv != environ && environ != NULL)
memcpy(P, environ, cnt * sizeof(char *));
lastenv = environ = P;
offset = cnt;