From 7242e103c3ca65b3f003c382301a3d65fe40846e Mon Sep 17 00:00:00 2001 From: millert <> Date: Mon, 23 Aug 2010 22:31:50 +0000 Subject: Check for duplicate variables in the environment when setting a value via setenv() or putenv(). OK miod@ --- src/lib/libc/stdlib/setenv.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src/lib/libc/stdlib/setenv.c') diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c index 89d1e88911..089ab92d38 100644 --- a/src/lib/libc/stdlib/setenv.c +++ b/src/lib/libc/stdlib/setenv.c @@ -1,4 +1,4 @@ -/* $OpenBSD: setenv.c,v 1.12 2010/06/29 04:09:34 naddy Exp $ */ +/* $OpenBSD: setenv.c,v 1.13 2010/08/23 22:31:50 millert Exp $ */ /* * Copyright (c) 1987 Regents of the University of California. * All rights reserved. @@ -47,7 +47,7 @@ putenv(char *str) { char **P, *cp; size_t cnt; - int offset; + int offset = 0; for (cp = str; *cp && *cp != '='; ++cp) ; @@ -57,7 +57,13 @@ putenv(char *str) } if (__findenv(str, (int)(cp - str), &offset) != NULL) { - environ[offset] = str; + environ[offset++] = str; + /* could be set multiple times */ + while (__findenv(str, (int)(cp - str), &offset)) { + for (P = &environ[offset];; ++P) + if (!(*P = *(P + 1))) + break; + } return (0); } @@ -84,9 +90,9 @@ putenv(char *str) int setenv(const char *name, const char *value, int rewrite) { - char *C; + char *C, **P; const char *np; - int l_value, offset; + int l_value, offset = 0; for (np = name; *np && *np != '='; ++np) ; @@ -99,6 +105,7 @@ setenv(const char *name, const char *value, int rewrite) l_value = strlen(value); if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) { + int tmpoff = offset + 1; if (!rewrite) return (0); #if 0 /* XXX - existing entry may not be writable */ @@ -108,9 +115,14 @@ setenv(const char *name, const char *value, int rewrite) return (0); } #endif + /* could be set multiple times */ + while (__findenv(name, (int)(np - name), &tmpoff)) { + for (P = &environ[tmpoff];; ++P) + if (!(*P = *(P + 1))) + break; + } } else { /* create new slot */ size_t cnt; - char **P; for (P = environ; *P != NULL; P++) ; @@ -143,7 +155,7 @@ unsetenv(const char *name) { char **P; const char *np; - int offset; + int offset = 0; if (!name || !*name) { errno = EINVAL; -- cgit v1.2.3-55-g6feb