]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Reduce cache_effective_user was leaking $HOME memory
authorAlex Rousskov <rousskov@measurement-factory.com>
Sun, 13 Jul 2014 03:10:16 +0000 (21:10 -0600)
committerAmos Jeffries <squid3@treenet.co.nz>
Sun, 13 Jul 2014 03:10:16 +0000 (21:10 -0600)
putenv() leaks memory by design but there is no need to use it at all
when the path is unchanged.

src/cache_cf.cc

index 38ac506212330823aa333e8af1a933dd717a9179..eb7e069ce81164b4d22c48461aa2c04ff3784b3a 100644 (file)
@@ -893,16 +893,18 @@ configDoConfigure(void)
             Config2.effectiveGroupID = pwd->pw_gid;
 
 #if HAVE_PUTENV
-
-            if (pwd->pw_dir && *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);
+             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 = strlen(pwd->pw_dir) + 6;
+                    char *env_str = (char *)xcalloc(len, 1);
+                    snprintf(env_str, len, "HOME=%s", pwd->pw_dir);
+                    putenv(env_str);
+                }
             }
-
 #endif
-
         }
     } else {
         Config2.effectiveUserID = geteuid();