[libvirt] [libvirt-glib] Fix *_new_from_xml

Christophe Fergeau cfergeau at redhat.com
Wed Jul 18 13:16:03 UTC 2012


For objects with a subtype 'type' attribute, when the _new_from_xml
function was called, the 'type' attribute was forcefully set to the
right value rather than checking that the passed-in value matches
the type of the subclass we are trying to instantiate. This commit
changes this, and returns NULL when the value of the 'type' attribute
of the passed-in XML document does not match the expected type.
---
 libvirt-gconfig/libvirt-gconfig-domain-address-pci.c             |    2 +-
 libvirt-gconfig/libvirt-gconfig-domain-address-usb.c             |    2 +-
 libvirt-gconfig/libvirt-gconfig-domain-chardev-source-pty.c      |    2 +-
 libvirt-gconfig/libvirt-gconfig-domain-chardev-source-spicevmc.c |    2 +-
 libvirt-gconfig/libvirt-gconfig-domain-controller-usb.c          |    2 +-
 libvirt-gconfig/libvirt-gconfig-domain-graphics-sdl.c            |    4 +---
 libvirt-gconfig/libvirt-gconfig-domain-graphics-spice.c          |    4 +---
 libvirt-gconfig/libvirt-gconfig-domain-graphics-vnc.c            |    4 +---
 libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c        |    4 +---
 libvirt-gconfig/libvirt-gconfig-domain-interface-network.c       |    4 +---
 libvirt-gconfig/libvirt-gconfig-domain-interface-user.c          |    4 +---
 libvirt-gconfig/libvirt-gconfig-domain-timer-pit.c               |    2 +-
 libvirt-gconfig/libvirt-gconfig-domain-timer-rtc.c               |    2 +-
 13 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/libvirt-gconfig/libvirt-gconfig-domain-address-pci.c b/libvirt-gconfig/libvirt-gconfig-domain-address-pci.c
index 48e3872..5199b8a 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-address-pci.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-address-pci.c
@@ -67,7 +67,7 @@ GVirConfigDomainAddressPci *gvir_config_domain_address_pci_new_from_xml(const gc
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_ADDRESS_PCI,
                                              "address", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "type", "pci", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "pci") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_ADDRESS_PCI(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-address-usb.c b/libvirt-gconfig/libvirt-gconfig-domain-address-usb.c
index 3da5811..c1305c2 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-address-usb.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-address-usb.c
@@ -67,7 +67,7 @@ GVirConfigDomainAddressUsb *gvir_config_domain_address_usb_new_from_xml(const gc
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_ADDRESS_USB,
                                              "address", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "type", "usb", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "usb") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_ADDRESS_USB(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-pty.c b/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-pty.c
index 201e123..097834a 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-pty.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-pty.c
@@ -75,7 +75,7 @@ GVirConfigDomainChardevSourcePty *gvir_config_domain_chardev_source_pty_new_from
      */
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_CHARDEV_SOURCE_PTY,
                                              "dummy", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "type", "pty", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "pty") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_CHARDEV_SOURCE_PTY(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-spicevmc.c b/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-spicevmc.c
index 22eabf5..bef17cb 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-spicevmc.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-chardev-source-spicevmc.c
@@ -75,6 +75,6 @@ GVirConfigDomainChardevSourceSpiceVmc *gvir_config_domain_chardev_source_spicevm
      */
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_CHARDEV_SOURCE_SPICE_VMC,
                                              "dummy", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "type", "spicevmc", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "spicevmc") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_CHARDEV_SOURCE_SPICE_VMC(object);
 }
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-controller-usb.c b/libvirt-gconfig/libvirt-gconfig-domain-controller-usb.c
index 1fd248c..0f8bca3 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-controller-usb.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-controller-usb.c
@@ -167,7 +167,7 @@ GVirConfigDomainControllerUsb *gvir_config_domain_controller_usb_new_from_xml(co
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_CONTROLLER_USB,
                                              "controller", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "type", "usb", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "usb") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_CONTROLLER_USB(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-graphics-sdl.c b/libvirt-gconfig/libvirt-gconfig-domain-graphics-sdl.c
index 7871ae5..dd2b60c 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-graphics-sdl.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-graphics-sdl.c
@@ -68,9 +68,7 @@ gvir_config_domain_graphics_sdl_new_from_xml(const gchar *xml,
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_GRAPHICS_SDL,
                                              "graphics", NULL, xml, error);
-    if (object == NULL)
-        return NULL;
-    gvir_config_object_set_attribute(object, "type", "sdl", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "sdl") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_GRAPHICS_SDL(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-graphics-spice.c b/libvirt-gconfig/libvirt-gconfig-domain-graphics-spice.c
index e60a778..e41abd2 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-graphics-spice.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-graphics-spice.c
@@ -68,9 +68,7 @@ gvir_config_domain_graphics_spice_new_from_xml(const gchar *xml,
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_GRAPHICS_SPICE,
                                              "graphics", NULL, xml, error);
-    if (object == NULL)
-        return NULL;
-    gvir_config_object_set_attribute(object, "type", "spice", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "spice") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_GRAPHICS_SPICE(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-graphics-vnc.c b/libvirt-gconfig/libvirt-gconfig-domain-graphics-vnc.c
index d9d1303..aeed7c6 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-graphics-vnc.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-graphics-vnc.c
@@ -68,9 +68,7 @@ gvir_config_domain_graphics_vnc_new_from_xml(const gchar *xml,
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_GRAPHICS_VNC,
                                              "graphics", NULL, xml, error);
-    if (object == NULL)
-        return NULL;
-    gvir_config_object_set_attribute(object, "type", "vnc", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "vnc") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_GRAPHICS_VNC(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c b/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c
index ea5eafa..e7e43d4 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-interface-bridge.c
@@ -70,9 +70,7 @@ GVirConfigDomainInterfaceBridge *gvir_config_domain_interface_bridge_new_from_xm
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_INTERFACE_BRIDGE,
                                              "interface", NULL, xml, error);
-    if (object == NULL)
-        return NULL;
-    gvir_config_object_set_attribute(object, "type", "bridge", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "bridge") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_INTERFACE_BRIDGE(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c b/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c
index 1a7bfad..7f19cc8 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-interface-network.c
@@ -69,9 +69,7 @@ GVirConfigDomainInterfaceNetwork *gvir_config_domain_interface_network_new_from_
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_INTERFACE_NETWORK,
                                              "interface", NULL, xml, error);
-    if (object == NULL)
-        return NULL;
-    gvir_config_object_set_attribute(object, "type", "network", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "network") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_INTERFACE_NETWORK(object);
 }
 
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-interface-user.c b/libvirt-gconfig/libvirt-gconfig-domain-interface-user.c
index a455a73..2d29abc 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-interface-user.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-interface-user.c
@@ -69,8 +69,6 @@ GVirConfigDomainInterfaceUser *gvir_config_domain_interface_user_new_from_xml(co
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_INTERFACE_USER,
                                              "interface", NULL, xml, error);
-    if (object == NULL)
-        return NULL;
-    gvir_config_object_set_attribute(object, "type", "user", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "user") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_INTERFACE_USER(object);
 }
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-timer-pit.c b/libvirt-gconfig/libvirt-gconfig-domain-timer-pit.c
index d75cd30..671a9dc 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-timer-pit.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-timer-pit.c
@@ -67,6 +67,6 @@ GVirConfigDomainTimerPit *gvir_config_domain_timer_pit_new_from_xml(const gchar
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_TIMER_PIT,
                                              "timer", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "name", "pit", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "pit") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_TIMER_PIT(object);
 }
diff --git a/libvirt-gconfig/libvirt-gconfig-domain-timer-rtc.c b/libvirt-gconfig/libvirt-gconfig-domain-timer-rtc.c
index b99c1e3..4b36dcd 100644
--- a/libvirt-gconfig/libvirt-gconfig-domain-timer-rtc.c
+++ b/libvirt-gconfig/libvirt-gconfig-domain-timer-rtc.c
@@ -67,6 +67,6 @@ GVirConfigDomainTimerRtc *gvir_config_domain_timer_rtc_new_from_xml(const gchar
 
     object = gvir_config_object_new_from_xml(GVIR_CONFIG_TYPE_DOMAIN_TIMER_RTC,
                                              "timer", NULL, xml, error);
-    gvir_config_object_set_attribute(object, "name", "rtc", NULL);
+    g_return_val_if_fail(g_strcmp0(gvir_config_object_get_attribute(object, NULL, "type"), "rtc") != 0, NULL);
     return GVIR_CONFIG_DOMAIN_TIMER_RTC(object);
 }
-- 
1.7.10.4




More information about the libvir-list mailing list