From a79b4ff59a5a49fdc8e0647d5cdecf2260a8f67e Mon Sep 17 00:00:00 2001
From: Mark Grondona <mgrondona@llnl.gov>
Date: Tue, 9 Dec 2003 19:17:46 +0000
Subject: [PATCH]  o fix some small memory management bugs in setenvpf() code.

---
 src/slurmd/setenvpf.c | 24 ++++++++++--------------
 1 file changed, 10 insertions(+), 14 deletions(-)

diff --git a/src/slurmd/setenvpf.c b/src/slurmd/setenvpf.c
index fe9cdc91732..46340ac787a 100644
--- a/src/slurmd/setenvpf.c
+++ b/src/slurmd/setenvpf.c
@@ -70,39 +70,37 @@ _find_name_in_env(char **env, const char *name)
 
 /*
  *  Extend memory allocation for env by 1 entry. Make last entry == NULL.
+ *   return pointer to last env entry;
  */
-static void
+static char **
 _extend_env(char ***envp)
 {
-	size_t newsize = xsize (*envp) + sizeof (char *);
-	xrealloc (*envp, newsize);
-	(*envp)[newsize - 1] = NULL;
-	return;
+	size_t newcnt = (xsize (*envp) / sizeof (char *)) + 1;
+	*envp = xrealloc (*envp, newcnt * sizeof (char *));
+	(*envp)[newcnt - 1] = NULL;
+	return (&((*envp)[newcnt - 2]));
 }
 
 int 
 setenvpf(char ***envp, const char *name, const char *fmt, ...)
 {
-	char *str = NULL;
 	char buf[BUFSIZ];
 	char **ep = NULL;
+	char *str = NULL;
 	va_list ap;
 
-	str = xstrdup (name);
-	xstrcatchar (str, '=');
-
 	va_start(ap, fmt);
 	vsnprintf (buf, BUFSIZ, fmt, ap);
 	va_end(ap);
 
-	xstrcat (str, buf);
+	xstrfmtcat (str, "%s=%s", name, buf);
 
 	ep = _find_name_in_env (*envp, name);
 
 	if (*ep != NULL) 
 		xfree (*ep);
 	else
-		_extend_env (envp);
+		ep = _extend_env (envp);
 		
 	*ep = str;
 
@@ -124,16 +122,14 @@ unsetenvp(char **env, const char *name)
 	ep = env;
 	while ((ep = _find_name_in_env (ep, name)) && (*ep != NULL)) {
 		char **dp = ep;
-
+		xfree (*ep);
 		do 
 			dp[0] = dp[1];
 		while (*dp++);
 
 		/*  Continue loop in case `name' appears again. */
-
 		++ep;
 	}
-
 	return;
 }
 
-- 
GitLab