[Patch] Fix memory leaks in setString()

Tim Hentenaar <tim.hentenaar-o7tR/nIX9Vg/[email protected]> Fri, 22 Sep 2006 18:16:49 -0400
Newsgroups gmane.comp.security.zebedee.general
Organization Security Confidence
Message-ID <1158963409.9623.52.camel@localhost>
Hi all,

This patch fixes potential memory leaks which I observed while running
zebedee 2.5.3 in setString():

 void
 setString(char *value, char **resultP)
 {
    if ((*resultP = (char *)malloc(strlen(value) + 1)) == NULL)
    {

 	message(0, errno, "failed allocating space for string value '%s'",
value);
 	exit(EXIT_FAILURE);
    }
    /* strdup() above instead of malloc() would save code. */
    strcpy(*resultP, value);
 }

strlen() might cause a segfault if value is NULL. A segfault would also
occur if resultP is NULL. *resultP might have been previously allocated
and thus never gets free()'d resulting in a memory leak. 


-- 
Tim Hentenaar
Security Confidence Corporation

E-Mail: Tim.Hentenaar-Evk0FUv1mq+dWhLBjz8/[email protected]
Tel:    +1 (513) 388-4500 Ext. 102
Web:    http://www.SecurityConfidence.com

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
Zebedee-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/zebedee-talk
zebedee-2.5.3.patch (text/x-patch, 1.6 KB)
diff -ru zebedee-2.5.3/zebedee.c zebedee-2.5.3/zebedee.c
--- zebedee-2.5.3/zebedee.c	2005-09-02 18:20:23.000000000 -0400
+++ zebedee-2.5.3/zebedee.c	2006-09-22 17:51:01.000000000 -0400
@@ -477,7 +477,7 @@
 EndPtList_t *ClientPorts = NULL;	/* Ports on which client listens */
 EndPtList_t *TargetPorts = NULL;	/* Target port to which to tunnel */
 char *ServerHost = NULL;	/* Name of host on which server runs */
-char *TargetHost = "localhost";	/* Default host to which tunnels are targeted */
+char *TargetHost = NULL;	/* Default host to which tunnels are targeted */
 char *IdentityFile = NULL;	/* Name of identity file to check, if any */
 EndPtList_t *AllowedTargets = NULL; /* List of allowed target hosts/ports */
 EndPtList_t *AllowedDefault = NULL; /* List of default allowed redirection ports */
@@ -7412,13 +7412,12 @@
 void
 setString(char *value, char **resultP)
 {
-    if ((*resultP = (char *)malloc(strlen(value) + 1)) == NULL)
-    {
+    if (!value || !resultP) return;
+    if (*resultP != NULL) free(*resultP);
+    if (strlen(value) > 0 && (*resultP = strdup(value)) == NULL) {
 	message(0, errno, "failed allocating space for string value '%s'", value);
 	exit(EXIT_FAILURE);
-    }
-
-    strcpy(*resultP, value);
+    } 
 }
 
 /*
@@ -8471,10 +8470,11 @@
 	** Sanity check the default target. This must be a "pure" hostname
 	** without an address mask.
 	*/
-
+	if (!TargetHost) TargetHost = strdup("localhost");
 	if (strchr(TargetHost, '/') != NULL)
 	{
 	    message(0, 0, "default target host (%s) must not have an address mask", TargetHost);
+	    free(TargetHost);
 	    exit(EXIT_FAILURE);
 	}
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQBFFGDRK3VK1q6QiPARAqg0AKDhMaFunSqKxAwjjr5JHtd3NUi3mQCg9bO/
Stm5zEjk1TUjg8OfpS+K5NY=
=hTUT
-----END PGP SIGNATURE-----