[PATCH 6/7] Replace alloc+strcpy+strcat with asprintf() & XNFasprintf() calls

Alan Coopersmith alan.coopersmith at oracle.com
Fri Dec 3 21:09:23 PST 2010


Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 dix/devices.c                       |   17 +++++++++++------
 hw/xfree86/common/xf86Config.c      |    4 +---
 hw/xfree86/common/xf86Option.c      |    5 +----
 hw/xfree86/common/xf86ShowOpts.c    |    7 ++-----
 hw/xfree86/dixmods/extmod/modinit.c |    5 +----
 hw/xfree86/loader/loadmod.c         |    6 ++----
 hw/xfree86/modes/xf86Crtc.c         |    8 ++------
 7 files changed, 20 insertions(+), 32 deletions(-)

diff --git a/dix/devices.c b/dix/devices.c
index 708860a..6c0dc42 100644
--- a/dix/devices.c
+++ b/dix/devices.c
@@ -2524,9 +2524,11 @@ AllocDevicePair (ClientPtr client, char* name,
     if (!pointer)
         return BadAlloc;
 
-    pointer->name = calloc(strlen(name) + strlen(" pointer") + 1, sizeof(char));
-    strcpy(pointer->name, name);
-    strcat(pointer->name, " pointer");
+    if (asprintf(&pointer->name, "%s pointer", name) == -1) {
+        pointer->name = NULL;
+        RemoveDevice(pointer, FALSE);
+        return BadAlloc;
+    }
 
     pointer->public.processInputProc = ProcessOtherEvent;
     pointer->public.realInputProc = ProcessOtherEvent;
@@ -2547,9 +2549,12 @@ AllocDevicePair (ClientPtr client, char* name,
         return BadAlloc;
     }
 
-    keyboard->name = calloc(strlen(name) + strlen(" keyboard") + 1, sizeof(char));
-    strcpy(keyboard->name, name);
-    strcat(keyboard->name, " keyboard");
+    if (asprintf(&keyboard->name, "%s keyboard", name) == -1) {
+        keyboard->name = NULL;
+        RemoveDevice(keyboard, FALSE);
+        RemoveDevice(pointer, FALSE);
+        return BadAlloc;
+    }
 
     keyboard->public.processInputProc = ProcessOtherEvent;
     keyboard->public.realInputProc = ProcessOtherEvent;
diff --git a/hw/xfree86/common/xf86Config.c b/hw/xfree86/common/xf86Config.c
index 94300c2..5fa42a3 100644
--- a/hw/xfree86/common/xf86Config.c
+++ b/hw/xfree86/common/xf86Config.c
@@ -208,9 +208,7 @@ xf86ValidateFontPath(char *path)
 	continue;
       }
       else {
-	p1 = xnfalloc(strlen(dir_elem)+strlen(DIR_FILE)+1);
-	strcpy(p1, dir_elem);
-	strcat(p1, DIR_FILE);
+	XNFasprintf(&p1, "%s%s", dir_elem, DIR_FILE);
 	flag = stat(p1, &stat_buf);
 	if (flag == 0)
 	  if (!S_ISREG(stat_buf.st_mode))
diff --git a/hw/xfree86/common/xf86Option.c b/hw/xfree86/common/xf86Option.c
index af39b2b..d49aa31 100644
--- a/hw/xfree86/common/xf86Option.c
+++ b/hw/xfree86/common/xf86Option.c
@@ -638,13 +638,10 @@ ParseOptionValue(int scrnIndex, pointer options, OptionInfoPtr p,
 	    newn = n + 2;
 	} else {
 	    free(n);
-	    n = malloc(strlen(p->name) + 2 + 1);
-	    if (!n) {
+	    if (asprintf(&n, "No%s", p->name) == -1) {
 		p->found = FALSE;
 		return FALSE;
 	    }
-	    strcpy(n, "No");
-	    strcat(n, p->name);
 	    newn = n;
 	}
 	if ((s = xf86findOptionValue(options, newn)) != NULL) {
diff --git a/hw/xfree86/common/xf86ShowOpts.c b/hw/xfree86/common/xf86ShowOpts.c
index ce86090..c0fa80a 100644
--- a/hw/xfree86/common/xf86ShowOpts.c
+++ b/hw/xfree86/common/xf86ShowOpts.c
@@ -97,11 +97,8 @@ void DoShowOptions (void) {
 				);
 				continue;                                                       
 			}
-			pSymbol = malloc(
-				strlen(xf86DriverList[i]->driverName) + strlen("ModuleData") + 1
-			);
-			strcpy (pSymbol, xf86DriverList[i]->driverName);
-			strcat (pSymbol, "ModuleData");
+			XNFasprintf(&pSymbol, "%sModuleData",
+				    xf86DriverList[i]->driverName);
 			initData = LoaderSymbol (pSymbol);
 			if (initData) {
 				XF86ModuleVersionInfo *vers = initData->vers;
diff --git a/hw/xfree86/dixmods/extmod/modinit.c b/hw/xfree86/dixmods/extmod/modinit.c
index f4e922c..168795d 100644
--- a/hw/xfree86/dixmods/extmod/modinit.c
+++ b/hw/xfree86/dixmods/extmod/modinit.c
@@ -146,11 +146,8 @@ extmodSetup(pointer module, pointer opts, int *errmaj, int *errmin)
     for (i = 0; extensionModules[i].name != NULL; i++) {
 	if (opts) {
 	    char *s;
-	    s = (char *)malloc(strlen(extensionModules[i].name) + 5);
-	    if (s) {
+	    if (Xasprinf(&s, "omit%s", extensionModules[i].name) != -1) {
 		pointer o;
-		strcpy(s, "omit");
-		strcat(s, extensionModules[i].name);
 		o = xf86FindOption(opts, s);
 		free(s);
 		if (o) {
diff --git a/hw/xfree86/loader/loadmod.c b/hw/xfree86/loader/loadmod.c
index 1f6933a..3b3511c 100644
--- a/hw/xfree86/loader/loadmod.c
+++ b/hw/xfree86/loader/loadmod.c
@@ -933,16 +933,14 @@ doLoadModule(const char *module, const char *path, const char **subdirlist,
      * now check if the special data object <modulename>ModuleData is
      * present.
      */
-    p = malloc(strlen(name) + strlen("ModuleData") + 1);
-    if (!p) {
+    if (asprintf(&p, "%sModuleData", name) == -1) {
+	p = NULL;
 	if (errmaj)
 	    *errmaj = LDR_NOMEM;
 	if (errmin)
 	    *errmin = 0;
 	goto LoadModule_fail;
     }
-    strcpy(p, name);
-    strcat(p, "ModuleData");
     initdata = LoaderSymbol(p);
     if (initdata) {
 	ModuleSetupProc setup;
diff --git a/hw/xfree86/modes/xf86Crtc.c b/hw/xfree86/modes/xf86Crtc.c
index b94bc09..59a8680 100644
--- a/hw/xfree86/modes/xf86Crtc.c
+++ b/hw/xfree86/modes/xf86Crtc.c
@@ -480,7 +480,6 @@ static void
 xf86OutputSetMonitor (xf86OutputPtr output)
 {
     char    *option_name;
-    static const char monitor_prefix[] = "monitor-";
     char    *monitor;
 
     if (!output->name)
@@ -490,11 +489,8 @@ xf86OutputSetMonitor (xf86OutputPtr output)
 
     output->options = xnfalloc (sizeof (xf86OutputOptions));
     memcpy (output->options, xf86OutputOptions, sizeof (xf86OutputOptions));
-    
-    option_name = xnfalloc (strlen (monitor_prefix) +
-			    strlen (output->name) + 1);
-    strcpy (option_name, monitor_prefix);
-    strcat (option_name, output->name);
+
+    XNFasprintf(&option_name, "monitor-%s", output->name);
     monitor = xf86findOptionValue (output->scrn->options, option_name);
     if (!monitor)
 	monitor = output->name;
-- 
1.7.3.2



More information about the xorg-devel mailing list