[cairo] [PATCH 1/3] test: fix append_argv()

przanoni at gmail.com przanoni at gmail.com
Fri Sep 9 10:14:48 PDT 2011


From: Paulo Zanoni <paulo.r.zanoni at intel.com>

When I ran "CAIRO_TESTS=a1-bug make test", no test executed because of a
bug in append_argv(). The "olen" variable was assuming that we always
only append a single argument to argv and the resulting argc was also
wrong.

Signed-off-by: Paulo Zanoni <paulo.r.zanoni at intel.com>
---

I used the following program to test the function:
http://www.inf.ufpr.br/paulo/cairo_append_argv_test.c

We might consider adding a test system to test the functions of our
test system :D

 test/cairo-test-runner.c |   14 ++++++++++----
 1 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/test/cairo-test-runner.c b/test/cairo-test-runner.c
index 87c6db0..4afcafc 100644
--- a/test/cairo-test-runner.c
+++ b/test/cairo-test-runner.c
@@ -280,6 +280,7 @@ append_argv (int *argc, char ***argv, const char *str)
     int olen;
     int len;
     int i;
+    int args_to_add = 0;
 
     if (str == NULL)
 	return;
@@ -290,9 +291,9 @@ append_argv (int *argc, char ***argv, const char *str)
     doit = FALSE;
     do {
 	if (doit)
-	    *argv = xmalloc (sizeof (char *) * (1 + *argc) + olen);
+	    *argv = xmalloc (olen);
 
-	olen = sizeof (char *) * (1 + *argc);
+	olen = sizeof (char *) * (args_to_add + *argc);
 	for (i = 0; i < old_argc; i++) {
 	    len = strlen (old_argv[i]) + 1;
 	    if (doit) {
@@ -310,7 +311,10 @@ append_argv (int *argc, char ***argv, const char *str)
 		    (*argv)[i] = (char *) *argv + olen;
 		    memcpy ((*argv)[i], s, len);
 		    (*argv)[i][len] = '\0';
+		} else {
+		    olen += sizeof (char *);
 		}
+		args_to_add++;
 		olen += len + 1;
 		i++;
 	    }
@@ -321,13 +325,15 @@ append_argv (int *argc, char ***argv, const char *str)
 	    if (doit) {
 		(*argv)[i] = (char *) *argv + olen;
 		memcpy ((*argv)[i], s, len);
+	    } else {
+		olen += sizeof (char *);
 	    }
+	    args_to_add++;
 	    olen += len;
 	    i++;
 	}
     } while (doit++ == FALSE);
-    (*argv)[i] = NULL;
-    *argc += i;
+    *argc = i;
 }
 
 static void
-- 
1.7.4.1



More information about the cairo mailing list