[virt-tools-list] [PATCH 10/47] Remove more malloc failure checks

Daniel P. Berrange berrange at redhat.com
Wed Aug 25 19:37:05 UTC 2010


GLib will always abort() upon malloc failure, so there is
no need to check return value of any g_object_new, g_new
or g_strdup API call. Remove all this redundant ENOMEM
checking

* osinfo/osinfo_common.c, osinfo/osinfo_dataread.c,
  osinfo/osinfo_db.c, osinfo/osinfo_devicelist.c,
  osinfo/osinfo_entity.c, osinfo/osinfo_filter.c,
  osinfo/osinfo_hypervisor.c, osinfo/osinfo_hypervisorlist.c,
  osinfo/osinfo_list.c, osinfo/osinfo_os.c,
  osinfo/osinfo_oslist.c: Remove all ENOMEM checks
---
 osinfo/osinfo_common.c         |    2 --
 osinfo/osinfo_dataread.c       |   21 ++-------------------
 osinfo/osinfo_db.c             |   27 ---------------------------
 osinfo/osinfo_devicelist.c     |   12 ------------
 osinfo/osinfo_entity.c         |    8 --------
 osinfo/osinfo_filter.c         |   18 ------------------
 osinfo/osinfo_hypervisor.c     |    8 --------
 osinfo/osinfo_hypervisorlist.c |   12 ------------
 osinfo/osinfo_list.c           |   23 -----------------------
 osinfo/osinfo_os.c             |   14 +-------------
 osinfo/osinfo_oslist.c         |   12 ------------
 11 files changed, 3 insertions(+), 154 deletions(-)

diff --git a/osinfo/osinfo_common.c b/osinfo/osinfo_common.c
index a94fbac..bb9b680 100644
--- a/osinfo/osinfo_common.c
+++ b/osinfo/osinfo_common.c
@@ -312,8 +312,6 @@ int __osinfoCheckRelationshipValid(osinfoRelationship relshp)
 gchar *__osinfoErrorToString(int err)
 {
     switch (err) {
-        case -ENOMEM:
-            return OSINFO_NO_MEM;
         default:
             return OSINFO_OTHER;
     }
diff --git a/osinfo/osinfo_dataread.c b/osinfo/osinfo_dataread.c
index e186e83..9974a79 100644
--- a/osinfo/osinfo_dataread.c
+++ b/osinfo/osinfo_dataread.c
@@ -191,11 +191,7 @@ static int __osinfoProcessTag(xmlTextReaderPtr reader, char** ptr_to_key, char**
     if (!node_name)
         goto error;
 
-    key = strdup(node_name);
-    if (!key) {
-        err = -ENOMEM;
-        goto error;
-    }
+    key = g_strdup(node_name);
 
     /* Advance to next node */
     ret = xmlTextReaderRead(reader);
@@ -212,11 +208,7 @@ static int __osinfoProcessTag(xmlTextReaderPtr reader, char** ptr_to_key, char**
     if (!xml_value)
         goto error;
 
-    val = strdup(xml_value);
-    if (!val) {
-        err = -ENOMEM;
-        goto error;
-    }
+    val = g_strdup(xml_value);
 
     /* Advance to the next node */
     ret = xmlTextReaderRead(reader);
@@ -449,8 +441,6 @@ static int __osinfoProcessOs(OsinfoDb *db,
 
     os = g_object_new(OSINFO_TYPE_OS, "id", id, "db", db, NULL);
     free(id);
-    if (!os)
-        return -ENOMEM;
 
     if (empty)
         goto finished;
@@ -570,9 +560,6 @@ static int __osinfoProcessHypervisor(OsinfoDb *db,
 
     hv = g_object_new(OSINFO_TYPE_HYPERVISOR, "id", id, "db", db, NULL);
     free(id);
-    if (!hv) {
-        return -ENOMEM;
-    }
 
     if (empty)
         goto finished;
@@ -680,10 +667,6 @@ static int __osinfoProcessDevice(OsinfoDb *db,
 
     dev = g_object_new(OSINFO_TYPE_DEVICE, "id", id, "db", db, NULL);
     free(id);
-    if (!dev) {
-        // TODO: How do errors in gobject creation manifest themselves?
-        return -ENOMEM;
-    }
 
     if (empty)
         goto finished;
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index 8f3bb03..5e7ad90 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -272,11 +272,6 @@ OsinfoOsList *osinfo_db_get_os_list(OsinfoDb *self, OsinfoFilter *filter, GError
 
     // Create list
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
-
     int ret;
     ret = __osinfoPopulateList(self->priv->oses, OSINFO_LIST (newList), filter, err);
     if (ret != 0) {
@@ -302,10 +297,6 @@ OsinfoHypervisorList *osinfo_db_get_hypervisor_list(OsinfoDb *self, OsinfoFilter
 
     // Create list
     OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
     ret = __osinfoPopulateList(self->priv->hypervisors, OSINFO_LIST (newList), filter, err);
@@ -332,10 +323,6 @@ OsinfoDeviceList *osinfo_db_get_device_list(OsinfoDb *self, OsinfoFilter *filter
 
     // Create list
     OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
     ret = __osinfoPopulateList(self->priv->devices, OSINFO_LIST (newList), filter, err);
@@ -396,10 +383,6 @@ static gboolean __osinfoFreeKeys(gpointer key, gpointer value, gpointer data)
 static GPtrArray *osinfo_db_unique_values_for_property_in_entity(GTree *entities, gchar *propName, GError **err)
 {
     GTree *values = g_tree_new(__osinfoStringCompareBase);
-    if (!values) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     struct __osinfoPopulateValuesArgs args = {err, 0, values, propName};
     g_tree_foreach(entities, osinfo_db_get_property_values_in_entity, &args);
@@ -413,12 +396,6 @@ static GPtrArray *osinfo_db_unique_values_for_property_in_entity(GTree *entities
 
     // For each key in tree, add to gptrarray
     GPtrArray *valuesList = g_ptr_array_sized_new(g_tree_nnodes(values));
-    if (!valuesList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        g_tree_foreach(values, __osinfoFreeKeys, NULL);
-        g_tree_destroy(values);
-        return NULL;
-    }
 
     g_tree_foreach(values, __osinfoPutKeysInList, valuesList);
     g_tree_destroy(values);
@@ -517,10 +494,6 @@ OsinfoOsList *osinfo_db_unique_values_for_os_relationship(OsinfoDb *self, osinfo
 
     // Create list
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     struct __osinfoOsCheckRelationshipArgs args = {OSINFO_LIST (newList), 0, err, relshp};
 
diff --git a/osinfo/osinfo_devicelist.c b/osinfo/osinfo_devicelist.c
index c3e9f95..ed44f9f 100644
--- a/osinfo/osinfo_devicelist.c
+++ b/osinfo/osinfo_devicelist.c
@@ -62,10 +62,6 @@ OsinfoDeviceList *osinfo_device_list_filter(OsinfoDeviceList *self, OsinfoFilter
 
     // For each element in self, if passes filter, add to new list.
     OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     __osinfo_list_filter(OSINFO_LIST (self), OSINFO_LIST (newList), filter);
     return newList;
@@ -79,10 +75,6 @@ OsinfoDeviceList *osinfo_device_list_intersect(OsinfoDeviceList *self, OsinfoDev
     }
 
     OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
 
@@ -104,10 +96,6 @@ OsinfoDeviceList *osinfo_device_list_union(OsinfoDeviceList *self, OsinfoDeviceL
     }
 
     OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
     ret = __osinfo_list_union(self, otherList, newList);
diff --git a/osinfo/osinfo_entity.c b/osinfo/osinfo_entity.c
index e9492db..fd3767f 100644
--- a/osinfo/osinfo_entity.c
+++ b/osinfo/osinfo_entity.c
@@ -194,10 +194,6 @@ GPtrArray *osinfo_entity_get_params(OsinfoEntity *self, GError **err)
     }
 
     GPtrArray *params = g_ptr_array_new();
-    if (!params) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     struct __osinfoPtrArrayErr arrayErr = {params, 0};
     g_tree_foreach(self->priv->params, osinfo_get_keys, &arrayErr);
@@ -267,10 +263,6 @@ GPtrArray *osinfo_entity_get_param_all_values(OsinfoEntity *self, gchar *key, GE
     GPtrArray *srcArray, *retArray;
 
     retArray = g_ptr_array_new();
-    if (!retArray) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     found = g_tree_lookup_extended(self->priv->params, key, &origKey, &value);
     if (!found)
diff --git a/osinfo/osinfo_filter.c b/osinfo/osinfo_filter.c
index b7f22d0..4dd2dc5 100644
--- a/osinfo/osinfo_filter.c
+++ b/osinfo/osinfo_filter.c
@@ -122,8 +122,6 @@ gint osinfo_filter_add_relation_constraint(OsinfoFilter *self, osinfoRelationshi
     found = g_tree_lookup_extended(self->priv->relationshipConstraints, (gpointer) relshp, &origKey, &foundValue);
     if (!found) {
         valueArray = g_ptr_array_new();
-        if (!valueArray)
-            goto error_nomem;
 
         g_tree_insert(self->priv->relationshipConstraints, (gpointer) relshp, valueArray);
     }
@@ -133,10 +131,6 @@ gint osinfo_filter_add_relation_constraint(OsinfoFilter *self, osinfoRelationshi
     // Add to the array
     g_ptr_array_add(valueArray, os);
     return 0;
-
-error_nomem:
-    g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-    return -ENOMEM;
 }
 
 void osinfo_filter_clear_constraint(OsinfoFilter *self, gchar *propName)
@@ -174,10 +168,6 @@ GPtrArray *osinfo_filter_get_constraint_keys(OsinfoFilter *self, GError **err)
     }
 
     GPtrArray *constraints = g_ptr_array_new();
-    if (!constraints) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     struct __osinfoPtrArrayErr arrayErr = {constraints, 0};
     g_tree_foreach(self->priv->propertyConstraints, osinfo_get_keys, &arrayErr);
@@ -216,10 +206,6 @@ GPtrArray *osinfo_filter_get_constraint_values(OsinfoFilter *self, gchar *propNa
     GPtrArray *srcArray, *retArray;
 
     retArray = g_ptr_array_new();
-    if (!retArray) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     found = g_tree_lookup_extended(self->priv->propertyConstraints, propName, &origKey, &value);
     if (!found)
@@ -258,10 +244,6 @@ OsinfoOsList *osinfo_filter_get_relationship_constraint_value(OsinfoFilter *self
 
     // Create our list
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     GPtrArray *relatedOses = NULL;
     relatedOses = g_tree_lookup(self->priv->relationshipConstraints, (gpointer) relshp);
diff --git a/osinfo/osinfo_hypervisor.c b/osinfo/osinfo_hypervisor.c
index 3ac12cc..2eba6c2 100644
--- a/osinfo/osinfo_hypervisor.c
+++ b/osinfo/osinfo_hypervisor.c
@@ -65,10 +65,6 @@ GPtrArray *osinfo_hypervisor_get_device_types(OsinfoHypervisor *self, GError **e
     }
 
     GPtrArray *deviceTypes = g_ptr_array_sized_new(g_tree_nnodes(self->priv->sections));
-    if (!deviceTypes) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     // For each key in our tree of device sections, dup and add to the array
     struct __osinfoPtrArrayErr arrayErr = {deviceTypes, 0};
@@ -98,10 +94,6 @@ OsinfoDeviceList *osinfo_hypervisor_get_devices_by_type(OsinfoHypervisor *self,
 
     // Create our device list
     OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     // If section does not exist, return empty list
     GPtrArray *sectionList = NULL;
diff --git a/osinfo/osinfo_hypervisorlist.c b/osinfo/osinfo_hypervisorlist.c
index 0874904..b2aa511 100644
--- a/osinfo/osinfo_hypervisorlist.c
+++ b/osinfo/osinfo_hypervisorlist.c
@@ -62,10 +62,6 @@ OsinfoHypervisorList *osinfo_hypervisor_list_filter(OsinfoHypervisorList *self,
 
     // For each element in self, if passes filter, add to new list.
     OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     __osinfo_list_filter(OSINFO_LIST (self), OSINFO_LIST (newList), filter);
     return newList;
@@ -79,10 +75,6 @@ OsinfoHypervisorList *osinfo_hypervisor_list_intersect(OsinfoHypervisorList *sel
     }
 
     OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
 
@@ -104,10 +96,6 @@ OsinfoHypervisorList *osinfo_hypervisor_list_union(OsinfoHypervisorList *self, O
     }
 
     OsinfoHypervisorList *newList = g_object_new(OSINFO_TYPE_HYPERVISORLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
     ret = __osinfo_list_union(self, otherList, newList);
diff --git a/osinfo/osinfo_list.c b/osinfo/osinfo_list.c
index 53e8bd4..35bcb24 100644
--- a/osinfo/osinfo_list.c
+++ b/osinfo/osinfo_list.c
@@ -81,10 +81,6 @@ OsinfoList *osinfo_list_filter(OsinfoList *self, OsinfoFilter *filter, GError **
 
     // For each element in self, if passes filter, add to new list.
     OsinfoList *newList = g_object_new(OSINFO_TYPE_LIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     __osinfo_list_filter(self, newList, filter);
     return newList;
@@ -96,14 +92,7 @@ int __osinfo_list_intersect(OsinfoList *src1, OsinfoList *src2, OsinfoList *dst)
 
     // Make set representation of otherList and newList
     GTree *otherSet = g_tree_new(__osinfoStringCompareBase);
-    if (!otherSet)
-        return -ENOMEM;
-
     GTree *newSet = g_tree_new(__osinfoStringCompareBase);
-    if (!newSet) {
-        g_tree_destroy(otherSet);
-        return -ENOMEM;
-    }
 
     // Add all from otherList to otherSet
     len = osinfo_list_get_length(src2);
@@ -139,11 +128,6 @@ OsinfoList *osinfo_list_intersect(OsinfoList *self, OsinfoList *otherList, GErro
     }
 
     OsinfoList *newList = g_object_new(OSINFO_TYPE_LIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
-
     int ret;
 
     ret = __osinfo_list_intersect(self, otherList, newList);
@@ -160,8 +144,6 @@ int __osinfo_list_union(OsinfoList *src1, OsinfoList *src2, OsinfoList *dst)
 {
     // Make set version of new list
     GTree *newSet = g_tree_new(__osinfoStringCompareBase);
-    if (!newSet)
-        return -ENOMEM;
 
     // Add all from other list to new list
     int i, len;
@@ -197,11 +179,6 @@ OsinfoList *osinfo_list_union(OsinfoList *self, OsinfoList *otherList, GError **
     }
 
     OsinfoList *newList = g_object_new(OSINFO_TYPE_LIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
-
     int ret;
     ret = __osinfo_list_union(self, otherList, newList);
     if (ret != 0) {
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index 8ab6dd1..8543e2f 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -94,8 +94,6 @@ static int __osinfoAddOsRelationshipByType(OsinfoOs *self,
     found = g_tree_lookup_extended(self->priv->relationshipsByType, (gpointer) relshp, &origKey, &foundValue);
     if (!found) {
         relationshipsForType = g_ptr_array_new();
-        if (!relationshipsForType)
-            return -ENOMEM;
 
         g_tree_insert(self->priv->relationshipsByType, (gpointer) relshp, relationshipsForType);
     }
@@ -137,9 +135,7 @@ int __osinfoAddOsRelationship (OsinfoOs *self, gchar *otherOsId, osinfoRelations
         return -EINVAL;
 
     struct __osinfoOsLink *osLink = NULL;
-    osLink = g_malloc(sizeof(*osLink));
-    if (!osLink)
-        return -ENOMEM;
+    osLink = g_new0(struct __osinfoOsLink, 1);
 
     osLink->subjectOs = self;
     osLink->verb = rel;
@@ -297,10 +293,6 @@ OsinfoOsList *osinfo_os_get_related(OsinfoOs *self, osinfoRelationship relshp, G
 
     // Create our list
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     GPtrArray *relatedOses = NULL;
     relatedOses = g_tree_lookup(self->priv->relationshipsByType, (gpointer) relshp);
@@ -345,10 +337,6 @@ OsinfoDeviceList *osinfo_os_get_devices(OsinfoOs *self, OsinfoHypervisor *hv, gc
 
     // Create our device list
     OsinfoDeviceList *newList = g_object_new(OSINFO_TYPE_DEVICELIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     if (hv) {
         struct __osinfoHvSection *hvSection = NULL;
diff --git a/osinfo/osinfo_oslist.c b/osinfo/osinfo_oslist.c
index 145db13..5f28a3b 100644
--- a/osinfo/osinfo_oslist.c
+++ b/osinfo/osinfo_oslist.c
@@ -62,10 +62,6 @@ OsinfoOsList *osinfo_os_list_filter(OsinfoOsList *self, OsinfoFilter *filter, GE
 
     // For each element in self, if passes filter, add to new list.
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     __osinfo_list_filter(OSINFO_LIST (self), OSINFO_LIST (newList), filter);
     return newList;
@@ -79,10 +75,6 @@ OsinfoOsList *osinfo_os_list_intersect(OsinfoOsList *self, OsinfoOsList *otherLi
     }
 
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
 
@@ -104,10 +96,6 @@ OsinfoOsList *osinfo_os_list_union(OsinfoOsList *self, OsinfoOsList *otherList,
     }
 
     OsinfoOsList *newList = g_object_new(OSINFO_TYPE_OSLIST, NULL);
-    if (!newList) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     int ret;
     ret = __osinfo_list_union(self, otherList, newList);
-- 
1.7.2.1




More information about the virt-tools-list mailing list