]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Reduce cache_effective_user was leaking $HOME memory
authorAlex Rousskov <rousskov@measurement-factory.com>
Mon, 2 Jun 2014 06:23:12 +0000 (23:23 -0700)
committerAmos Jeffries <squid3@treenet.co.nz>
Mon, 2 Jun 2014 06:23:12 +0000 (23:23 -0700)
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 95bea4b0ebde68d2fba6806c64afad56f6a882d0..4899fb583b8209d2d96ec3432f686ca65c375f58 100644 (file)
@@ -892,16 +892,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);
+                // putenv() leaks by design; avoid leaks when nothing changes
+                static SBuf lastDir;
+                if (lastDir.isEmpty() || !lastDir.cmp(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();