summaryrefslogtreecommitdiff
path: root/lib/libc/compat/stdlib
diff options
context:
space:
mode:
authorchristos <christos@NetBSD.org>2012-04-22 15:55:41 +0000
committerchristos <christos@NetBSD.org>2012-04-22 15:55:41 +0000
commit806609d596b45a26cdacc7f49c8fcfa68e61dd32 (patch)
tree31bf6c749e8fa421a30a07d0aa6cd2cd9157d4f0 /lib/libc/compat/stdlib
parent67313c3626102d535fc9406c93d4a0cb0a39ebb0 (diff)
use setenv so that we don't leak memory.
Diffstat (limited to 'lib/libc/compat/stdlib')
-rw-r--r--lib/libc/compat/stdlib/compat_putenv.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/libc/compat/stdlib/compat_putenv.c b/lib/libc/compat/stdlib/compat_putenv.c
index 31a8f3a8de1..a234f82928a 100644
--- a/lib/libc/compat/stdlib/compat_putenv.c
+++ b/lib/libc/compat/stdlib/compat_putenv.c
@@ -1,4 +1,4 @@
-/* $NetBSD: compat_putenv.c,v 1.1 2012/04/20 17:31:30 christos Exp $ */
+/* $NetBSD: compat_putenv.c,v 1.2 2012/04/22 15:55:41 christos Exp $ */
/*-
* Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -30,17 +30,19 @@
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: compat_putenv.c,v 1.1 2012/04/20 17:31:30 christos Exp $");
+__RCSID("$NetBSD: compat_putenv.c,v 1.2 2012/04/22 15:55:41 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#define __LIBC12_SOURCE__
#include "namespace.h"
#include <assert.h>
-#include <string.h>
+#include <errno.h>
#include <stdlib.h>
+#include <string.h>
#include <compat/include/stdlib.h>
+#include "env.h"
#include "reentrant.h"
#include "local.h"
@@ -48,23 +50,35 @@ __RCSID("$NetBSD: compat_putenv.c,v 1.1 2012/04/20 17:31:30 christos Exp $");
__weak_alias(putenv,_putenv)
#endif
-__warn_references(unsetenv,
+__warn_references(putenv,
"warning: reference to compatibility putenv();"
" include <stdlib.h> for correct reference")
/*
* putenv(name) --
- * This version copies the string for compatibility.
+ * This version implicitly copies the string for compatibility.
*/
int
putenv(char *name)
{
+ size_t l_name;
char *copy;
+ int rv;
_DIAGASSERT(name != NULL);
+ l_name = __envvarnamelen(name, true);
+ if (l_name == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
if ((copy = strdup(name)) == NULL)
return -1;
+ copy[l_name++] = '\0';
+
+ rv = setenv(copy, copy + l_name, 1);
- return __putenv50(copy);
+ free(copy);
+ return rv;
}