]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virInterfaceObjListAssignDef: Transfer definition ownership
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 2 Nov 2021 10:02:27 +0000 (11:02 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 24 Nov 2021 12:12:20 +0000 (13:12 +0100)
Upon successful return from virInterfaceObjListAssignDef() the
virInterfaceObj is the owner of secret definition. To make this
ownership transfer even more visible, lets pass the definition as
a double pointer and use g_steal_pointer().

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/conf/virinterfaceobj.c
src/conf/virinterfaceobj.h
src/test/test_driver.c

index c5dfa6c7f55126354bf160d4f78a7ebd29d95a4c..daac74e88c09372361c6507f9ad8809ab03addbe 100644 (file)
@@ -377,9 +377,8 @@ virInterfaceObjListCloneCb(void *payload,
         goto error;
     VIR_FREE(xml);
 
-    if (!(obj = virInterfaceObjListAssignDef(data->dest, backup)))
+    if (!(obj = virInterfaceObjListAssignDef(data->dest, &backup)))
         goto error;
-    backup = NULL;
     virInterfaceObjEndAPI(&obj);
 
     virObjectUnlock(srcObj);
@@ -420,25 +419,39 @@ virInterfaceObjListClone(virInterfaceObjList *interfaces)
 }
 
 
+/**
+ * virInterfaceObjListAssignDef:
+ * @interfaces: virInterface object list
+ * @def: new definition
+ *
+ * Assigns new definition to either an existing or newly created
+ * virInterface object. Upon successful return the virInterface
+ * object is the owner of @def and callers should use
+ * virInterfaceObjGetDef() if they need to access the definition
+ * as @def is set to NULL.
+ *
+ * Returns: a virInterface object instance on success, or
+ *          NULL on error.
+ */
 virInterfaceObj *
 virInterfaceObjListAssignDef(virInterfaceObjList *interfaces,
-                             virInterfaceDef *def)
+                             virInterfaceDef **def)
 {
     virInterfaceObj *obj;
 
     virObjectRWLockWrite(interfaces);
-    if ((obj = virInterfaceObjListFindByNameLocked(interfaces, def->name))) {
+    if ((obj = virInterfaceObjListFindByNameLocked(interfaces, (*def)->name))) {
         virInterfaceDefFree(obj->def);
     } else {
         if (!(obj = virInterfaceObjNew()))
             goto error;
 
-        if (virHashAddEntry(interfaces->objsName, def->name, obj) < 0)
+        if (virHashAddEntry(interfaces->objsName, (*def)->name, obj) < 0)
             goto error;
         virObjectRef(obj);
     }
 
-    obj->def = def;
+    obj->def = g_steal_pointer(def);
     virObjectRWUnlock(interfaces);
 
     return obj;
index d60faa95f2cf18cf9394691e8cb9ccd5c8441208..59274841676fe99cd7b33fa364ab346bf384f2cf 100644 (file)
@@ -59,7 +59,7 @@ virInterfaceObjListClone(virInterfaceObjList *interfaces);
 
 virInterfaceObj *
 virInterfaceObjListAssignDef(virInterfaceObjList *interfaces,
-                             virInterfaceDef *def);
+                             virInterfaceDef **def);
 
 void
 virInterfaceObjListRemove(virInterfaceObjList *interfaces,
index e45748c5fc5cfc5e1a157ef70f56b5faaa61b1b3..92c0462170e928e38a837538dbe9b961e88a5725 100644 (file)
@@ -1141,9 +1141,8 @@ testParseInterfaces(testDriver *privconn,
         if (!def)
             return -1;
 
-        if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, def)))
+        if (!(obj = virInterfaceObjListAssignDef(privconn->ifaces, &def)))
             return -1;
-        def = NULL;
 
         virInterfaceObjSetActive(obj, true);
         virInterfaceObjEndAPI(&obj);
@@ -6203,9 +6202,8 @@ testInterfaceDefineXML(virConnectPtr conn,
     if ((def = virInterfaceDefParseString(xmlStr, flags)) == NULL)
         goto cleanup;
 
-    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL)
+    if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, &def)) == NULL)
         goto cleanup;
-    def = NULL;
     objdef = virInterfaceObjGetDef(obj);
 
     ret = virGetInterface(conn, objdef->name, objdef->mac);