cache_effective_user was leaking $HOME memory more than it had to.

Alex.

cache_effective_user was leaking $HOME memory more than it had to

=== modified file 'src/cache_cf.cc'
--- src/cache_cf.cc	2014-04-12 17:32:34 +0000
+++ src/cache_cf.cc	2014-04-24 20:23:43 +0000
@@ -874,44 +874,49 @@ configDoConfigure(void)
                  * Andres Kroonmaa <an...@online.ee>:
                  * Some getpwnam() implementations (Solaris?) require
                  * an available FD < 256 for opening a FILE* to the
                  * passwd file.
                  * DW:
                  * This should be safe at startup, but might still fail
                  * during reconfigure.
                  */
                 fatalf("getpwnam failed to find userid for effective user '%s'",
                        Config.effectiveUser);
                 return;
             }
 
             Config2.effectiveUserID = pwd->pw_uid;
 
             Config2.effectiveGroupID = pwd->pw_gid;
 
 #if HAVE_PUTENV
 
             if (pwd->pw_dir && *pwd->pw_dir) {
+                // putenv() leaks by design; avoid leaks when nothing changes
+                static String lastDir;
+                if (!lastDir.size() || lastDir != pwd->pw_dir) {
+                    lastDir = pwd->pw_dir;
                 int len;
                 char *env_str = (char *)xcalloc((len = strlen(pwd->pw_dir) + 6), 1);
                 snprintf(env_str, len, "HOME=%s", pwd->pw_dir);
                 putenv(env_str);
+                }
             }
 
 #endif
 
         }
     } else {
         Config2.effectiveUserID = geteuid();
         Config2.effectiveGroupID = getegid();
     }
 
     if (NULL != Config.effectiveGroup) {
 
         struct group *grp = getgrnam(Config.effectiveGroup);
 
         if (NULL == grp) {
             fatalf("getgrnam failed to find groupid for effective group '%s'",
                    Config.effectiveGroup);
             return;
         }
 

Reply via email to