commit def68f5cb3a999cca196a439d4885a360ec6586a
parent 042b01bf1eb4597412a52b4aafb2b134e996dce9
Author: Roberto Vargas <roberto.vargas@arm.com>
Date: Thu, 22 Nov 2018 18:43:12 +0000
[target/hosted] Fix putenv() implementation
Change-Id: I2ead9ebb188e4b5964f4eb51386dd2736c9162a0
Diffstat:
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/target/hosted/putenv.c b/target/hosted/putenv.c
@@ -8,7 +8,8 @@ int
putenv(char *name)
{
char **p, *s;
- size_t len, cnt;
+ size_t siz, len, cnt;
+ static char **lastenv;
if ((s = strchr(name, '=')) == NULL) {
errno = EINVAL;
@@ -23,11 +24,15 @@ putenv(char *name)
}
}
- cnt = (_environ) ? p - _environ : 0;
- p = realloc(_environ, (cnt + 2) * sizeof(char **));
- if (!p)
+ cnt = p - _environ;
+ siz = (cnt + 2) * sizeof(char **);
+
+ if ((p = realloc(lastenv, siz)) == NULL)
return -1;
- _environ = p;
+ if (!lastenv)
+ memcpy(p, _environ, cnt * sizeof(char **));
+ lastenv = _environ = p;
+
p[cnt] = name;
p[cnt+1] = NULL;