[libvirt] [[libvirt-glib PATCHv2] 1/4] Add proper error reporting to GVirStorageVol getters

Christophe Fergeau cfergeau at redhat.com
Tue Jan 31 11:00:20 UTC 2012


gvir_storage_vol_get_name and gvir_storage_vol_get_path currently
call g_error when an error occurs. Since g_error trigger a coredump,
calling it in a library is harmful. Replace this with proper GError
error reporting.
---
 libvirt-gobject/libvirt-gobject-storage-pool.c |   10 +++++++---
 libvirt-gobject/libvirt-gobject-storage-vol.c  |   14 ++++++++++----
 libvirt-gobject/libvirt-gobject-storage-vol.h  |    4 ++--
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/libvirt-gobject/libvirt-gobject-storage-pool.c b/libvirt-gobject/libvirt-gobject-storage-pool.c
index bf25641..a88699e 100644
--- a/libvirt-gobject/libvirt-gobject-storage-pool.c
+++ b/libvirt-gobject/libvirt-gobject-storage-pool.c
@@ -528,6 +528,7 @@ GVirStorageVol *gvir_storage_pool_create_volume
     const gchar *xml;
     virStorageVolPtr handle;
     GVirStoragePoolPrivate *priv = pool->priv;
+    const char *name;
 
     xml = gvir_config_object_to_xml(GVIR_CONFIG_OBJECT(conf));
 
@@ -545,11 +546,14 @@ GVirStorageVol *gvir_storage_pool_create_volume
     volume = GVIR_STORAGE_VOL(g_object_new(GVIR_TYPE_STORAGE_VOL,
                                            "handle", handle,
                                            NULL));
+    name = gvir_storage_vol_get_name(volume, err);
+    if (name == NULL) {
+        g_object_unref(G_OBJECT(volume));
+        return NULL;
+    }
 
     g_mutex_lock(priv->lock);
-    g_hash_table_insert(priv->volumes,
-                        g_strdup(gvir_storage_vol_get_name(volume)),
-                        volume);
+    g_hash_table_insert(priv->volumes, g_strdup(name), volume);
     g_mutex_unlock(priv->lock);
 
     return g_object_ref(volume);
diff --git a/libvirt-gobject/libvirt-gobject-storage-vol.c b/libvirt-gobject/libvirt-gobject-storage-vol.c
index 5b18877..c427e42 100644
--- a/libvirt-gobject/libvirt-gobject-storage-vol.c
+++ b/libvirt-gobject/libvirt-gobject-storage-vol.c
@@ -172,25 +172,31 @@ gvir_storage_vol_info_free(GVirStorageVolInfo *info)
 G_DEFINE_BOXED_TYPE(GVirStorageVolInfo, gvir_storage_vol_info,
                     gvir_storage_vol_info_copy, gvir_storage_vol_info_free)
 
-const gchar *gvir_storage_vol_get_name(GVirStorageVol *vol)
+const gchar *gvir_storage_vol_get_name(GVirStorageVol *vol, GError **error)
 {
     GVirStorageVolPrivate *priv = vol->priv;
     const char *name;
 
     if (!(name = virStorageVolGetName(priv->handle))) {
-        g_error("Failed to get storage_vol name on %p", priv->handle);
+        gvir_set_error(error, GVIR_STORAGE_VOL_ERROR, 0,
+                       "Failed to get storage_vol name on %p",
+                       priv->handle);
+        return NULL;
     }
 
     return name;
 }
 
-const gchar *gvir_storage_vol_get_path(GVirStorageVol *vol)
+const gchar *gvir_storage_vol_get_path(GVirStorageVol *vol, GError **error)
 {
     GVirStorageVolPrivate *priv = vol->priv;
     const char *path;
 
     if (!(path = virStorageVolGetPath(priv->handle))) {
-        g_error("Failed to get storage_vol path on %p", priv->handle);
+        gvir_set_error(error, GVIR_STORAGE_VOL_ERROR, 0,
+                       "Failed to get storage_vol path on %p",
+                       priv->handle);
+        return NULL;
     }
 
     return path;
diff --git a/libvirt-gobject/libvirt-gobject-storage-vol.h b/libvirt-gobject/libvirt-gobject-storage-vol.h
index db63865..fd2be80 100644
--- a/libvirt-gobject/libvirt-gobject-storage-vol.h
+++ b/libvirt-gobject/libvirt-gobject-storage-vol.h
@@ -77,8 +77,8 @@ GType gvir_storage_vol_get_type(void);
 GType gvir_storage_vol_info_get_type(void);
 GType gvir_storage_vol_handle_get_type(void);
 
-const gchar *gvir_storage_vol_get_name(GVirStorageVol *vol);
-const gchar *gvir_storage_vol_get_path(GVirStorageVol *vol);
+const gchar *gvir_storage_vol_get_name(GVirStorageVol *vol, GError **error);
+const gchar *gvir_storage_vol_get_path(GVirStorageVol *vol, GError **error);
 
 gboolean gvir_storage_vol_delete(GVirStorageVol *vol,
                                  guint flags,
-- 
1.7.7.6




More information about the libvir-list mailing list