]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: string: Add helper to free non-NULL terminated string arrays
authorPeter Krempa <pkrempa@redhat.com>
Mon, 19 May 2014 12:20:09 +0000 (14:20 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 24 Jun 2014 08:45:43 +0000 (10:45 +0200)
To free string lists with some strings stolen from the middle we need to
walk the complete array. Introduce a new helper that takes the string
list size to free such string lists.

src/libvirt_private.syms
src/util/virstring.c
src/util/virstring.h
tests/virstringtest.c

index 2a2b9c07977ef2ae58b42b4d20149013e6ff2af5..a5f9ede81016a6a6c04ed3dbf4bf25d27857068f 100644 (file)
@@ -1924,6 +1924,7 @@ virStrcpy;
 virStrdup;
 virStringArrayHasString;
 virStringFreeList;
+virStringFreeListCount;
 virStringJoin;
 virStringListLength;
 virStringReplace;
index 6dcc7a8accc35804ba488985fd8054d3447196e2..35b99a5899f977996b2f99158b1b38243c347adb 100644 (file)
@@ -187,6 +187,26 @@ void virStringFreeList(char **strings)
 }
 
 
+/**
+ * virStringFreeListCount:
+ * @strings: array of strings to free
+ * @count: number of elements in the array
+ *
+ * Frees a string array of @count length.
+ */
+void
+virStringFreeListCount(char **strings,
+                       size_t count)
+{
+    size_t i;
+
+    for (i = 0; i < count; i++)
+        VIR_FREE(strings[i]);
+
+    VIR_FREE(strings);
+}
+
+
 bool
 virStringArrayHasString(char **strings, const char *needle)
 {
index 6ddcff549b91454f12db4164a1c46f70f3548365..df254417bedbfda07d3b6008a88e81774c955e7d 100644 (file)
@@ -42,6 +42,7 @@ char *virStringJoin(const char **strings,
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
 void virStringFreeList(char **strings);
+void virStringFreeListCount(char **strings, size_t count);
 
 bool virStringArrayHasString(char **strings, const char *needle);
 
index 5277fc657af457d360dbad980271458ff52bd48b..7c25b22c6710c8457fec014303da194bde2d1f3c 100644 (file)
@@ -497,6 +497,26 @@ testStringToLong(const void *opaque)
 }
 
 
+/* The point of this test is to check whether all members of the array are
+ * freed. The test has to be checked using valgrind. */
+static int
+testVirStringFreeListCount(const void *opaque ATTRIBUTE_UNUSED)
+{
+    char **list;
+
+    if (VIR_ALLOC_N(list, 4) < 0)
+        return -1;
+
+    ignore_value(VIR_STRDUP(list[0], "test1"));
+    ignore_value(VIR_STRDUP(list[2], "test2"));
+    ignore_value(VIR_STRDUP(list[3], "test3"));
+
+    virStringFreeListCount(list, 4);
+
+    return 0;
+}
+
+
 static int
 mymain(void)
 {
@@ -701,6 +721,11 @@ mymain(void)
     TEST_STRTOL("-18446744073709551616", NULL, 0, -1, 0U, -1,
                 0LL, -1, 0ULL, -1);
 
+    /* test virStringFreeListCount */
+    if (virtTestRun("virStringFreeListCount", testVirStringFreeListCount,
+                    NULL) < 0)
+        ret = -1;
+
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }