[PATCH 5/8] Convert to using asprintf for describe_format.

Eric Anholt eric at anholt.net
Mon Apr 21 11:56:19 PDT 2014


This simplifies the manual strcatting mess and avoids potential overflow
issues.
---
 Makefile.am   |  1 +
 main.c        | 23 ++++++++++++-----------
 rendercheck.h |  2 +-
 t_blend.c     |  6 ++++--
 t_fill.c      |  7 ++++---
 tests.c       | 27 +++++++--------------------
 6 files changed, 29 insertions(+), 37 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index b19ae4a..9afbed5 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -20,6 +20,7 @@ rendercheck_SOURCES = \
 	t_triangles.c
 
 AM_CFLAGS = $(RC_CFLAGS) $(CWARNFLAGS)
+AM_CPPFLAGS = -D_GNU_SOURCE
 rendercheck_LDADD = $(RC_LIBS)
 
 MAINTAINERCLEANFILES = ChangeLog INSTALL
diff --git a/main.c b/main.c
index 241c03d..853cc12 100644
--- a/main.c
+++ b/main.c
@@ -54,12 +54,15 @@ bit_count(int i)
 
 /* This is not complete, but decent enough for now.*/
 void
-describe_format(char *desc, int len, XRenderPictFormat *format)
+describe_format(char **desc, const char *prefix, XRenderPictFormat *format)
 {
 	char ad[4] = "", rd[4] = "", gd[4] = "", bd[4] = "";
 	int ac, rc, gc, bc;
 	int ashift;
 
+	if (!prefix)
+	    prefix = "";
+
 	ac = bit_count(format->direct.alphaMask);
 	rc = bit_count(format->direct.redMask);
 	gc = bit_count(format->direct.greenMask);
@@ -89,14 +92,14 @@ describe_format(char *desc, int len, XRenderPictFormat *format)
 
 	if (ashift > format->direct.red) {
 		if (format->direct.red > format->direct.blue)
-			snprintf(desc, len, "%s%s%s%s", ad, rd, gd, bd);
+			asprintf(desc, "%s%s%s%s%s", prefix, ad, rd, gd, bd);
 		else
-			snprintf(desc, len, "%s%s%s%s", ad, bd, gd, rd);
+			asprintf(desc, "%s%s%s%s%s", prefix, ad, bd, gd, rd);
 	} else {
 		if (format->direct.red > format->direct.blue)
-			snprintf(desc, len, "%s%s%s%s", rd, gd, bd, ad);
+			asprintf(desc, "%s%s%s%s%s", prefix, rd, gd, bd, ad);
 		else
-			snprintf(desc, len, "%s%s%s%s", bd, gd, rd, ad);
+			asprintf(desc, "%s%s%s%s%s", prefix, bd, gd, rd, ad);
 	}
 }
 
@@ -277,12 +280,10 @@ int main(int argc, char **argv)
 	window.format = XRenderFindVisualFormat(dpy, a.visual);
 	window.pict = XRenderCreatePicture(dpy, window.d,
 	    window.format, 0, NULL);
-	window.name = (char *)malloc(20);
-	if (window.name == NULL)
-		errx(1, "malloc error");
-	describe_format(window.name, 20, window.format);
-	printf("Window format: %s\n", window.name);
-	strncat(window.name, " window", 20);
+	describe_format(&format, NULL, window.format);
+	printf("Window format: %s\n", format);
+	asprintf(&window.name, "%s window", format);
+	free(format);
 	XSelectInput(dpy, window.d, ExposureMask);
 	XMapWindow(dpy, window.d);
 
diff --git a/rendercheck.h b/rendercheck.h
index 7da59a5..ad30dd6 100644
--- a/rendercheck.h
+++ b/rendercheck.h
@@ -96,7 +96,7 @@ extern int num_colors;
 
 /* main.c */
 void
-describe_format(char *desc, int len, XRenderPictFormat *format);
+describe_format(char **desc, const char *prefix, XRenderPictFormat *format);
 
 int
 bit_count(int i);
diff --git a/t_blend.c b/t_blend.c
index 57b2f2d..b436fe4 100644
--- a/t_blend.c
+++ b/t_blend.c
@@ -21,6 +21,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "rendercheck.h"
 
@@ -109,10 +110,10 @@ blend_test(Display *dpy, picture_info *win, picture_info *dst,
 					    color_correct(dst, &expected);
 
 					    if (eval_diff(&acc, &expected, &tested) > 3.) {
-						    char srcformat[20];
+						    char *srcformat;
 
 						    snprintf(testname, 20, "%s blend", ops[op[i]].name);
-						    describe_format(srcformat, 20, src_color[j]->format);
+						    describe_format(&srcformat, NULL, src_color[j]->format);
 						    print_fail(testname, &expected, &tested, 0, 0,
 							       eval_diff(&acc, &expected, &tested));
 						    printf("src color: %.2f %.2f %.2f %.2f (%s)\n"
@@ -125,6 +126,7 @@ blend_test(Display *dpy, picture_info *win, picture_info *dst,
 							   dst_color[k]->color.b,
 							   dst_color[k]->color.a);
 						    printf("src: %s, dst: %s\n", src_color[j]->name, dst->name);
+						    free(srcformat);
 						    return FALSE;
 					    }
 				    }
diff --git a/t_fill.c b/t_fill.c
index 4dd80a6..7e071c0 100644
--- a/t_fill.c
+++ b/t_fill.c
@@ -21,6 +21,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 
 #include "rendercheck.h"
@@ -32,16 +33,16 @@ Bool
 fill_test(Display *dpy, picture_info *win, picture_info *src)
 {
 	color4d tested;
-	char name[20];
+	char *name;
 
 	get_pixel(dpy, src, 0, 0, &tested);
 	copy_pict_to_win(dpy, src, win, win_width, win_height);
 
 	if (eval_diff(&src->format->direct, &src->color, &tested) > 2.) {
-	    strcpy(name, "fill ");
-	    describe_format(name, 20 - strlen(name), src->format);
+	    describe_format(&name, "fill ", src->format);
 	    print_fail(name, &src->color, &tested, 0, 0,
 		       eval_diff(&src->format->direct, &src->color, &tested));
+	    free(name);
 	    return FALSE;
 	}
 
diff --git a/tests.c b/tests.c
index e2ae645..4dacb5b 100644
--- a/tests.c
+++ b/tests.c
@@ -252,7 +252,7 @@ create_formats_list(Display *dpy)
 
     argb32index = -1;
     for (i = 0; ; i++) {
-	char name[20];
+	char *name;
 	int alphabits, redbits;
 
 	if (nformats + 1 == nformats_allocated) {
@@ -280,7 +280,7 @@ create_formats_list(Display *dpy)
 	    continue;
 	}
 
-	describe_format(name, 20, format_list[nformats]);
+	describe_format(&name, NULL, format_list[nformats]);
 
 	if (format_whitelist_len != 0) {
 	    Bool ok = FALSE;
@@ -342,10 +342,7 @@ do_tests(Display *dpy, picture_info *win)
 		dests[i].pict = XRenderCreatePicture(dpy, dests[i].d,
 		    dests[i].format, 0, NULL);
 
-		dests[i].name = (char *)malloc(20);
-		if (dests[i].name == NULL)
-			errx(1, "malloc error");
-		describe_format(dests[i].name, 20, dests[i].format);
+		describe_format(&dests[i].name, NULL, dests[i].format);
 	}
 
 	pictures_1x1 = (picture_info *)malloc(num_colors * nformats *
@@ -365,13 +362,8 @@ do_tests(Display *dpy, picture_info *win)
 		pictures_1x1[i].pict = XRenderCreatePicture(dpy,
 		    pictures_1x1[i].d, pictures_1x1[i].format, CPRepeat, &pa);
 
-		pictures_1x1[i].name = (char *)malloc(20);
-		if (pictures_1x1[i].name == NULL)
-			errx(1, "malloc error");
-		sprintf(pictures_1x1[i].name, "1x1R ");
-		describe_format(pictures_1x1[i].name +
-		    strlen(pictures_1x1[i].name), 20 -
-		    strlen(pictures_1x1[i].name), pictures_1x1[i].format);
+		describe_format(&pictures_1x1[i].name, "1x1R ",
+		    pictures_1x1[i].format);
 
 		argb_fill(dpy, &pictures_1x1[i], 0, 0, 1, 1,
 		    c->a, c->r, c->g, c->b);
@@ -399,13 +391,8 @@ do_tests(Display *dpy, picture_info *win)
 		pictures_10x10[i].pict = XRenderCreatePicture(dpy,
 		    pictures_10x10[i].d, pictures_10x10[i].format, 0, NULL);
 
-		pictures_10x10[i].name = (char *)malloc(20);
-		if (pictures_10x10[i].name == NULL)
-			errx(1, "malloc error");
-		sprintf(pictures_10x10[i].name, "10x10 ");
-		describe_format(pictures_10x10[i].name +
-		    strlen(pictures_10x10[i].name), 20 -
-		    strlen(pictures_10x10[i].name), pictures_10x10[i].format);
+		describe_format(&pictures_10x10[i].name, "10x10 ",
+		    pictures_10x10[i].format);
 
 		argb_fill(dpy, &pictures_10x10[i], 0, 0, 10, 10,
 		    c->a, c->r, c->g, c->b);
-- 
1.9.2



More information about the xorg-devel mailing list