[PATCH:libxtrans 2/4] Replace malloc(strlen)+strcpy with strdup

Alan Coopersmith alan.coopersmith at oracle.com
Mon Dec 5 20:36:30 PST 2011


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 Xtrans.c    |   26 +++++++-------------------
 Xtranslcl.c |    8 ++------
 2 files changed, 9 insertions(+), 25 deletions(-)

diff --git a/Xtrans.c b/Xtrans.c
index 007b54f..97de39b 100644
--- a/Xtrans.c
+++ b/Xtrans.c
@@ -212,8 +212,7 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port)
 
     /* Copy the string so it can be changed */
 
-    tmpptr = mybuf = (char *) malloc (strlen (address) + 1);
-    strcpy (mybuf, address);
+    tmpptr = mybuf = strdup (address);
 
     /* Parse the string to get each component */
 
@@ -346,7 +345,7 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port)
      * string space for them.
      */
 
-    if ((*protocol = (char *) malloc(strlen (_protocol) + 1)) == NULL)
+    if ((*protocol = strdup (_protocol)) == NULL)
     {
 	/* Malloc failed */
 	*port = NULL;
@@ -355,10 +354,8 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port)
 	free (tmpptr);
 	return 0;
     }
-    else
-        strcpy (*protocol, _protocol);
 
-    if ((*host = (char *) malloc (strlen (_host) + 1)) == NULL)
+    if ((*host = strdup (_host)) == NULL)
     {
 	/* Malloc failed */
 	*port = NULL;
@@ -367,11 +364,9 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port)
 	*protocol = NULL;
 	free (tmpptr);
 	return 0;
-	}
-    else
-        strcpy (*host, _host);
+    }
 
-    if ((*port = (char *) malloc (strlen (_port) + 1)) == NULL)
+    if ((*port = strdup (_port)) == NULL)
     {
 	/* Malloc failed */
 	*port = NULL;
@@ -382,8 +377,6 @@ TRANS(ParseAddress) (char *address, char **protocol, char **host, char **port)
 	free (tmpptr);
 	return 0;
     }
-    else
-        strcpy (*port, _port);
 
     free (tmpptr);
 
@@ -523,15 +516,13 @@ TRANS(Reopen) (int type, int trans_id, int fd, char *port)
 	return NULL;
     }
 
-    if ((save_port = (char *) malloc (strlen (port) + 1)) == NULL)
+    if ((save_port = strdup (port)) == NULL)
     {
 	prmsg (1,"Reopen: Unable to malloc port string\n");
 
 	return NULL;
     }
 
-    strcpy (save_port, port);
-
     /* Get a new XtransConnInfo object */
 
     switch (type)
@@ -653,13 +644,10 @@ TRANS(GetReopenInfo) (XtransConnInfo ciptr,
 	    *trans_id = Xtransports[i].transport_id;
 	    *fd = ciptr->fd;
 
-	    if ((*port = (char *) malloc (strlen (ciptr->port) + 1)) == NULL)
+	    if ((*port = strdup (ciptr->port)) == NULL)
 		return 0;
 	    else
-	    {
-		strcpy (*port, ciptr->port);
 		return 1;
-	    }
 	}
 
     return 0;
diff --git a/Xtranslcl.c b/Xtranslcl.c
index 8a5e718..925ba01 100644
--- a/Xtranslcl.c
+++ b/Xtranslcl.c
@@ -1671,17 +1671,13 @@ TRANS(LocalInitTransports)(const char *protocol)
 
     if( strcmp(protocol,"local") && strcmp(protocol,"LOCAL") )
     {
-	workingXLOCAL=freeXLOCAL=(char *)malloc (strlen (protocol) + 1);
-	if (workingXLOCAL)
-	    strcpy (workingXLOCAL, protocol);
+	workingXLOCAL = freeXLOCAL = strdup (protocol);
     }
     else {
 	XLOCAL=(char *)getenv("XLOCAL");
 	if(XLOCAL==NULL)
 	    XLOCAL=DEF_XLOCAL;
-	workingXLOCAL=freeXLOCAL=(char *)malloc (strlen (XLOCAL) + 1);
-	if (workingXLOCAL)
-	    strcpy (workingXLOCAL, XLOCAL);
+	workingXLOCAL = freeXLOCAL = strdup (XLOCAL);
     }
 }
 
-- 
1.7.3.2



More information about the xorg-devel mailing list