From 37a2b6f0c2d21f1a615859125b74e703ecefc706 Mon Sep 17 00:00:00 2001 From: christos Date: Sun, 22 Apr 2012 15:55:41 +0000 Subject: [PATCH] use setenv so that we don't leak memory. --- lib/libc/compat/stdlib/compat_putenv.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/libc/compat/stdlib/compat_putenv.c b/lib/libc/compat/stdlib/compat_putenv.c index 31a8f3a8de18..a234f82928af 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 #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 -#include +#include #include +#include #include +#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 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'; - return __putenv50(copy); + rv = setenv(copy, copy + l_name, 1); + + free(copy); + return rv; }