]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
networkxml2xmltest: Dynamically allocate testInfo struct
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 17 Dec 2025 10:20:31 +0000 (11:20 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 29 Jan 2026 12:39:07 +0000 (13:39 +0100)
So far, the testInfo struct contained immutable data (from its
lifetime point of view). But that is about to change. For
instance, it will hold parsed network definition (virNetworkDef)
and in order to avoid leaking dynamically allocated data
corresponding free function must be introduced (or clear
function, doesn't really matter). At this point, the structure
might as well be dynamically allocated entirely.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
tests/networkxml2xmltest.c

index 2a537d77c06a0ce9403ffd30f501e5e7c1105114..e90b38088acb7e7db34f81d95934ea3c3165bf3a 100644 (file)
@@ -22,19 +22,33 @@ typedef enum {
     TEST_COMPARE_NET_XML2XML_RESULT_FAIL_COMPARE,
 } testCompareNetXML2XMLResult;
 
-struct testInfo {
+struct _testInfo {
     const char *name;
     unsigned int flags;
     testCompareNetXML2XMLResult expectResult;
-    virNetworkXMLOption *xmlopt;
-    const char *inxml;
-    const char *outxml;
+    virNetworkXMLOption *xmlopt; /* borrowed, immutable */
+    char *inxml;
+    char *outxml;
 };
 
+typedef struct _testInfo testInfo;
+void testInfoFree(testInfo *info);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(testInfo, testInfoFree);
+
+void testInfoFree(testInfo *info)
+{
+    if (!info)
+        return;
+
+    VIR_FREE(info->inxml);
+    VIR_FREE(info->outxml);
+    VIR_FREE(info);
+}
+
 static int
 testCompareXMLToXMLFiles(const void *data)
 {
-    struct testInfo *info = (void *) data;
+    testInfo *info = (void *) data;
     g_autofree char *actual = NULL;
     int ret;
     testCompareNetXML2XMLResult result = TEST_COMPARE_NET_XML2XML_RESULT_SUCCESS;
@@ -94,17 +108,16 @@ testRun(const char *name,
         unsigned int flags)
 {
     g_autofree char *name_xml2xml = g_strdup_printf("Network XML-2-XML %s", name);
-    struct testInfo info = { .name = name, .flags = flags, .expectResult = expectResult, .xmlopt = xmlopt };
-    g_autofree char *inxml = NULL;
-    g_autofree char *outxml = NULL;
-
-    inxml = g_strdup_printf("%s/networkxml2xmlin/%s.xml", abs_srcdir, name);
-    outxml = g_strdup_printf("%s/networkxml2xmlout/%s.xml", abs_srcdir, name);
+    g_autoptr(testInfo) info = g_new0(testInfo, 1);
 
-    info.inxml = inxml;
-    info.outxml = outxml;
+    info->name = name;
+    info->flags = flags;
+    info->expectResult = expectResult;
+    info->xmlopt = xmlopt;
+    info->inxml = g_strdup_printf("%s/networkxml2xmlin/%s.xml", abs_srcdir, name);
+    info->outxml = g_strdup_printf("%s/networkxml2xmlout/%s.xml", abs_srcdir, name);
 
-    virTestRunLog(ret, name_xml2xml, testCompareXMLToXMLFiles, &info);
+    virTestRunLog(ret, name_xml2xml, testCompareXMLToXMLFiles, info);
 }
 
 static int