[PATCH:xrdb 2/3 v2] Replace complex malloc calculations with asprintf()

Alan Coopersmith alan.coopersmith at oracle.com
Thu Jan 6 14:25:15 PST 2011


Includes simple local implemenation of asprintf if configure doesn't
find one in system libraries.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
Reviewed-by: Julien Cristau <jcristau at debian.org>
---

Modified to fix issues raised during review by Guillem Jover

 configure.ac |    3 +-
 xrdb.c       |   72 ++++++++++++++++++++++++++++++++++++---------------------
 2 files changed, 47 insertions(+), 28 deletions(-)

diff --git a/configure.ac b/configure.ac
index deeb9a1..bb97b00 100644
--- a/configure.ac
+++ b/configure.ac
@@ -26,6 +26,7 @@ AC_INIT([xrdb], [1.0.7],
         [https://bugs.freedesktop.org/enter_bug.cgi?product=xorg], [xrdb])
 AC_CONFIG_SRCDIR([Makefile.am])
 AC_CONFIG_HEADERS([config.h])
+AC_USE_SYSTEM_EXTENSIONS
 
 # Initialize Automake
 AM_INIT_AUTOMAKE([foreign dist-bzip2])
@@ -37,7 +38,7 @@ m4_ifndef([XORG_MACROS_VERSION],
 XORG_MACROS_VERSION(1.8)
 XORG_DEFAULT_OPTIONS
 
-AC_CHECK_FUNCS([mkstemp])
+AC_CHECK_FUNCS([mkstemp asprintf])
 
 # Find MAXHOSTNAMELEN definition
 # Common hidey holes:
diff --git a/xrdb.c b/xrdb.c
index 33547f8..1f21d82 100644
--- a/xrdb.c
+++ b/xrdb.c
@@ -148,6 +148,42 @@ static void Process ( int scrno, Bool doScreen, Bool execute );
 static void ShuffleEntries ( Entries *db, Entries *dbs, int num );
 static void ReProcess ( int scrno, Bool doScreen );
 
+#ifndef HAVE_ASPRINTF
+/* sprintf variant found in newer libc's which allocates string to print to */
+static int _X_ATTRIBUTE_PRINTF(2,3)
+asprintf(char ** ret, const char *format, ...)
+{
+    char buf[256];
+    int len;
+    va_list ap;
+
+    va_start(ap, format);
+    len = vsnprintf(buf, sizeof(buf), format, ap);
+    va_end(ap);
+
+    len += 1; /* snprintf doesn't count trailing '\0' */
+    if (len <= sizeof(buf))
+    {
+	*ret = strdup(buf);
+    }
+    else
+    {
+	*ret = malloc(len);
+	if (*ret != NULL)
+	{
+	    va_start(ap, format);
+	    len = vsnprintf(*ret, len, format, ap);
+	    va_end(ap);
+	}
+    }
+
+    if (*ret == NULL)
+	return -1;
+
+    return (len - 1);
+}
+#endif /* HAVE_ASPRINTF */
+
 static void 
 InitBuffer(Buffer *b)
 {
@@ -1162,13 +1198,9 @@ Process(int scrno, Bool doScreen, Bool execute)
 	    fprintf(input, "\n#include \"%s\"\n", filename);
 	    fclose(input);
 	    (void) mktemp(tmpname3);
-	    if((cmd = (char *)
-		malloc(strlen(cpp_program) + strlen(includes.val) +
-		       1 + strlen(tmpname2) + 3 + strlen(tmpname3) + 1)) ==
-	       NULL)
+	    if (asprintf(&cmd, "%s%s %s > %s", cpp_program, includes.val,
+			 tmpname2, tmpname3) == -1)
 		fatal("%s: Out of memory\n", ProgramName);
-	    sprintf(cmd, "%s%s %s > %s", cpp_program, includes.val,
-		    tmpname2, tmpname3);
 	    if (system(cmd) < 0)
 		fatal("%s: cannot run '%s'\n", ProgramName, cmd);
 	    free(cmd);
@@ -1181,11 +1213,8 @@ Process(int scrno, Bool doScreen, Bool execute)
 	    fprintf(stdin, "\n#include \"%s\"\n", filename);
 	    fflush(stdin);
 	    fseek(stdin, 0, 0);
-	    if((cmd = (char *)
-		malloc(strlen(cpp_program) + strlen(includes.val) + 1)) ==
-	       NULL)
+	    if (asprintf(&cmd, "%s%s", cpp_program, includes.val) == -1)
 		fatal("%s: Out of memory\n", ProgramName);
-	    sprintf(cmd, "%s%s", cpp_program, includes.val);
 	    if (!(input = popen(cmd, "r")))
 		fatal("%s: cannot run '%s'\n", ProgramName, cmd);
 	    free(cmd);
@@ -1199,31 +1228,20 @@ Process(int scrno, Bool doScreen, Bool execute)
 	if (cpp_program) {
 #ifdef WIN32
 	    (void) mktemp(tmpname3);
-	    if((cmd = (char *)
-		malloc(strlen(cpp_program) + strlen(includes.val) +
-		       1 + strlen(defines.val) + 1 +
-		       strlen(filename ? filename : "") + 3 +
-		       strlen(tmpname3) + 1)) ==
-	       NULL)
+	    if (asprintf(&cmd, "%s%s %s %s > %s", cpp_program,
+			 includes.val, defines.val,
+			 filename ? filename : "", tmpname3) == -1)
 		fatal("%s: Out of memory\n", ProgramName);
-	    sprintf(cmd, "%s%s %s %s > %s", cpp_program,
-		    includes.val, defines.val,
-		    filename ? filename : "", tmpname3);
 	    if (system(cmd) < 0)
 		fatal("%s: cannot run '%s'\n", ProgramName, cmd);
 	    free(cmd);
 	    if (!(input = fopen(tmpname3, "r")))
 		fatal("%s: can't open file '%s'\n", ProgramName, tmpname3);
 #else
-	    if((cmd = (char *)
-		malloc(strlen(cpp_program) + strlen(includes.val) + 1 +
-		       strlen(defines.val) + 1 +
-		       strlen(filename ? filename : "") + 1)) ==
-	       NULL)
+	    if (asprintf(&cmd, "%s%s %s %s", cpp_program,
+			 includes.val, defines.val,
+			 filename ? filename : "") == -1)
 		fatal("%s: Out of memory\n", ProgramName);
-	    sprintf(cmd, "%s%s %s %s", cpp_program,
-		    includes.val, defines.val,
-		    filename ? filename : "");
 	    if (!(input = popen(cmd, "r")))
 		fatal("%s: cannot run '%s'\n", ProgramName, cmd);
 	    free(cmd);
-- 
1.7.3.2



More information about the xorg-devel mailing list