Avoid read of free()d memory

Alex Kiernan alexk at demon.net
Thu Feb 15 17:40:12 UTC 2001



Russ Allbery <rra at stanford.edu> writes:

> Alex Kiernan <alexk at demon.net> writes:
> 
> > On solaris at least, this was causing a read of free()d memory inside
> > putenv:
> 
> We should just use setenv.  The fewer sprintfs we have floating around,
> the better, and setenv already knows not to free the memory it passes to
> putenv.
> 

Hows about this one instead (sorry there'll be a couple of lines of
skew):

Index: lib/getconfig.c
===================================================================
RCS file: /upstream-repositories/inn-cvs.isc.org/inn/lib/getconfig.c,v
retrieving revision 1.83
diff -u -r1.83 getconfig.c
--- lib/getconfig.c	2001/02/07 08:41:40	1.83
+++ lib/getconfig.c	2001/02/15 17:38:54
@@ -325,8 +327,7 @@
 int
 CheckInnConf(void)
 {
-    static char *tmpdir = NULL;
-    static unsigned int dirlen = 0;
+    char *tmpdir;
 
     if (GetFQDN(innconf->domain) == NULL) {
 	syslog(L_FATAL, "Hostname does not resolve or 'domain' in inn.conf is missing");
@@ -400,15 +401,14 @@
 	innconf->mailcmd = COPY(cpcatpath(innconf->pathbin, "innmail"));
     }
     /* Set the TMPDIR variable unconditionally and globally */
-    if (8 + strlen(innconf->pathtmp) > dirlen)
-	dirlen = 8 + strlen(innconf->pathtmp);
-    if (tmpdir == NULL)
-	tmpdir = NEW(char, dirlen);
-    else
-	RENEW(tmpdir, char, dirlen);
-    sprintf(tmpdir, "TMPDIR=%s", innconf->pathtmp);
-    putenv(tmpdir);
-    /* tmpdir should not be freed for some OS */
+    tmpdir = getenv("TMPDIR");
+    if (!tmpdir || strcmp(tmpdir, innconf->pathtmp) != 0) {
+	if (setenv("TMPDIR", innconf->pathtmp, true) != 0) {
+	    syslog(L_FATAL, "can't set TMPDIR in environment");
+	    (void)fprintf(stderr, "can't set TMPDIR in environment\n");
+	    return(-1);
+	}
+    }
     if (innconf->enableoverview && innconf->ovmethod == NULL) {
 	syslog(L_FATAL, "'ovmethod' must be defined in inn.conf if enableoverview is true");
 	(void)fprintf(stderr, "'ovmethod' must be defined in inn.conf if enableoverview is true\n");

-- 
Alex Kiernan, Principal Engineer, Development, Thus PLC


More information about the inn-patches mailing list