[PATCH v4 4/7] Convert hw/xfree86/parser code to use asprintf() calls

Alan Coopersmith alan.coopersmith at oracle.com
Sat Dec 4 12:14:38 PST 2010


Requires linking xprintf.c into libxf86config for those who build it.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 hw/xfree86/parser/Flags.c     |   13 ++++---------
 hw/xfree86/parser/Makefile.am |    1 +
 hw/xfree86/parser/Pointer.c   |   16 ++++++----------
 3 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/hw/xfree86/parser/Flags.c b/hw/xfree86/parser/Flags.c
index a9149c2..7a0794b 100644
--- a/hw/xfree86/parser/Flags.c
+++ b/hw/xfree86/parser/Flags.c
@@ -61,8 +61,8 @@
 #include "xf86Parser.h"
 #include "xf86tokens.h"
 #include "Configint.h"
-#include <math.h>
 #include <X11/Xfuncproto.h>
+#include "Xprintf.h"
 
 extern LexRec val;
 
@@ -143,9 +143,8 @@ xf86parseFlagsSection (void)
 							} else {
 								if (tokentype != NUMBER)
 									Error (NUMBER_MSG, tmp);
-								valstr = malloc(16);
-								if (valstr)
-									sprintf(valstr, "%d", val.num);
+								if (asprintf(&valstr, "%d", val.num) == -1)
+									valstr = NULL;
 							}
 						}
 						ptr->flg_option_lst = xf86addNewOption
@@ -426,13 +425,9 @@ char *
 xf86uLongToString(unsigned long i)
 {
 	char *s;
-	int l;
 
-	l = ceil(log10((double)i) + 2.5);
-	s = malloc(l);
-	if (!s)
+	if (asprintf(&s, "%lu", i) == -1)
 		return NULL;
-	sprintf(s, "%lu", i);
 	return s;
 }
 
diff --git a/hw/xfree86/parser/Makefile.am b/hw/xfree86/parser/Makefile.am
index caf7079..a5adc01 100644
--- a/hw/xfree86/parser/Makefile.am
+++ b/hw/xfree86/parser/Makefile.am
@@ -31,6 +31,7 @@ libxf86config_internal_la_SOURCES = \
 	$(INTERNAL_SOURCES)
 
 libxf86config_a_SOURCES = \
+	$(TOP_SRCDIR)/os/xprintf.c \
 	$(INTERNAL_SOURCES)
 libxf86config_a_CFLAGS = $(AM_CFLAGS)
 
diff --git a/hw/xfree86/parser/Pointer.c b/hw/xfree86/parser/Pointer.c
index 56a26ba..65397cd 100644
--- a/hw/xfree86/parser/Pointer.c
+++ b/hw/xfree86/parser/Pointer.c
@@ -62,6 +62,7 @@
 #include "xf86Parser.h"
 #include "xf86tokens.h"
 #include "Configint.h"
+#include "Xprintf.h"
 
 extern LexRec val;
 
@@ -99,8 +100,8 @@ static xf86ConfigSymTabRec ZMapTab[] =
 XF86ConfInputPtr
 xf86parsePointerSection (void)
 {
-	char *s, *s1, *s2;
-	int l;
+	char *s;
+	unsigned long val1;
 	int token;
 	parsePrologue (XF86ConfInputPtr, XF86ConfInputRec)
 
@@ -184,17 +185,12 @@ xf86parsePointerSection (void)
 			case NUMBER:
 				if (val.num < 0)
 					Error (ZAXISMAPPING_MSG, NULL);
-				s1 = xf86uLongToString(val.num);
+				val1 = val.num;
 				if (xf86getSubToken (&(ptr->inp_comment)) != NUMBER || val.num < 0) {
-					free(s1);
 					Error (ZAXISMAPPING_MSG, NULL);
 				}
-				s2 = xf86uLongToString(val.num);
-				l = strlen(s1) + 1 + strlen(s2) + 1;
-				s = malloc(l);
-				sprintf(s, "%s %s", s1, s2);
-				free(s1);
-				free(s2);
+				if (asprintf(&s, "%ul %ul", val1, val.num) == -1)
+				    s = NULL;
 				break;
 			case XAXIS:
 				s = strdup("x");
-- 
1.7.3.2



More information about the xorg-devel mailing list