[virt-tools-list] [PATCH 04/47] Remove all redundant malloc failure checks

Daniel P. Berrange berrange at redhat.com
Wed Aug 25 19:36:59 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_db.c,
  osinfo/osinfo_device.c, osinfo/osinfo_entity.c,
  osinfo/osinfo_filter.c, osinfo/osinfo_os.c: Remove
  all ENOMEM checking
---
 osinfo/osinfo_common.c |   31 +------------------------------
 osinfo/osinfo_db.c     |    5 -----
 osinfo/osinfo_device.c |    4 ----
 osinfo/osinfo_entity.c |   33 ++-------------------------------
 osinfo/osinfo_filter.c |   16 ----------------
 osinfo/osinfo_os.c     |   23 -----------------------
 6 files changed, 3 insertions(+), 109 deletions(-)

diff --git a/osinfo/osinfo_common.c b/osinfo/osinfo_common.c
index 15eb8cd..a94fbac 100644
--- a/osinfo/osinfo_common.c
+++ b/osinfo/osinfo_common.c
@@ -16,14 +16,6 @@ static int __osinfoAddDeviceToList(GTree *allSectionsAsList,
     if (!found) {
         sectionList = g_ptr_array_new();
         sectionNameDup = g_strdup(sectionName);
-
-        if (!sectionList)
-            goto error_free;
-        if (!sectionNameDup) {
-            g_ptr_array_free(sectionList, TRUE);
-            goto error_free;
-        }
-
         g_tree_insert(allSectionsAsList, sectionNameDup, sectionList);
     }
     else
@@ -31,10 +23,6 @@ static int __osinfoAddDeviceToList(GTree *allSectionsAsList,
 
     g_ptr_array_add(sectionList, deviceLink);
     return 0;
-
-error_free:
-    g_free(sectionNameDup);
-    return -ENOMEM;
 }
 
 int __osinfoAddDeviceToSection(GTree *allSections, GTree *allSectionsAsList, gchar *sectionName, gchar *id, gchar *driver)
@@ -51,23 +39,13 @@ int __osinfoAddDeviceToSection(GTree *allSections, GTree *allSectionsAsList, gch
 
     idDup = g_strdup(id);
     driverDup = g_strdup(driver);
-    deviceLink = g_malloc(sizeof(*deviceLink));
-
-    if (!idDup || g_strcmp0(driverDup, driver) != 0 || !deviceLink)
-        goto error_free;
+    deviceLink = g_new0(struct __osinfoDeviceLink, 1);
 
     found = g_tree_lookup_extended(allSections, sectionName, &origKey, &foundValue);
     if (!found) {
         section = g_tree_new_full(__osinfoStringCompare, NULL, g_free, __osinfoFreeDeviceLink);
         sectionNameDup = g_strdup(sectionName);
 
-        if (!section)
-            goto error_free;
-        if (!sectionNameDup) {
-            g_tree_destroy(section);
-            goto error_free;
-        }
-
         g_tree_insert(allSections, sectionNameDup, section);
     }
     else
@@ -81,13 +59,6 @@ int __osinfoAddDeviceToSection(GTree *allSections, GTree *allSectionsAsList, gch
         ret = __osinfoAddDeviceToList(allSectionsAsList, sectionName, deviceLink);
 
     return ret;
-
-error_free:
-    g_free(sectionNameDup);
-    g_free(idDup);
-    g_free(driverDup);
-    g_free(deviceLink);
-    return -ENOMEM;
 }
 
 void __osinfoClearDeviceSection(GTree *allSections, GTree *allSectionsAsList, gchar *section)
diff --git a/osinfo/osinfo_db.c b/osinfo/osinfo_db.c
index e669f78..0e92527 100644
--- a/osinfo/osinfo_db.c
+++ b/osinfo/osinfo_db.c
@@ -362,11 +362,6 @@ gboolean __osinfoGetPropertyValuesInEntity(gpointer key, gpointer value, gpointe
         if (test)
             continue;
         gchar *dupValue = g_strdup(currValue);
-        if (!dupValue) {
-            g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-            args->errcode = -ENOMEM;
-            return TRUE;
-        }
 
         // Add to tree with dummy value
         g_tree_insert(values, dupValue, (gpointer) 1);
diff --git a/osinfo/osinfo_device.c b/osinfo/osinfo_device.c
index d43c405..41b6738 100644
--- a/osinfo/osinfo_device.c
+++ b/osinfo/osinfo_device.c
@@ -85,10 +85,6 @@ gchar *osinfoGetDeviceDriver(OsinfoDevice *self,
         return NULL;
 
     driver = g_strdup(deviceLink->driver);
-    if (!driver) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     return driver;
 }
diff --git a/osinfo/osinfo_entity.c b/osinfo/osinfo_entity.c
index cbf33e0..5aab00f 100644
--- a/osinfo/osinfo_entity.c
+++ b/osinfo/osinfo_entity.c
@@ -125,21 +125,11 @@ int __osinfoAddParam(OsinfoEntity *self, gchar *key, gchar *value)
 
     valueDup = g_strdup(value);
 
-    if (!valueDup)
-        goto error_free;
-
     found = g_tree_lookup_extended(self->priv->params, key, &origKey, &foundValue);
     if (!found) {
         keyDup = g_strdup(key);
         valueArray = g_ptr_array_new_with_free_func(g_free);
 
-        if (!valueArray)
-            goto error_free;
-        if (!keyDup) {
-            g_ptr_array_free(valueArray, TRUE);
-            goto error_free;
-        }
-
         g_tree_insert(self->priv->params, keyDup, valueArray);
     }
     else
@@ -148,11 +138,6 @@ int __osinfoAddParam(OsinfoEntity *self, gchar *key, gchar *value)
     // Add a copy of the value to the array
     g_ptr_array_add(valueArray, valueDup);
     return 0;
-
-error_free:
-    g_free(keyDup);
-    g_free(valueDup);
-    return -ENOMEM;
 }
 
 void __osinfoClearParam(OsinfoEntity *self, gchar *key)
@@ -166,10 +151,6 @@ gboolean __osinfoGetKeys(gpointer key, gpointer value, gpointer data)
     GPtrArray *results = arrayErr->array;
     gchar *keyDup = g_strdup(key);
 
-    if (!keyDup) {
-        arrayErr->err = -ENOMEM;
-        return TRUE;
-    }
     g_ptr_array_add(results, keyDup);
     return FALSE; // Continue iterating
 }
@@ -183,10 +164,7 @@ void __osinfoDupArray(gpointer data, gpointer user_data)
         return;
 
     gchar *valueDup = g_strdup((gchar *)data);
-    if (!valueDup) {
-        arrayErr->err = -ENOMEM;
-        return;
-    }
+
     g_ptr_array_add(results, valueDup);
 }
 
@@ -201,10 +179,7 @@ gchar *osinfoGetId(OsinfoEntity *self, GError **err)
     }
 
     gchar *dupId = g_strdup(self->priv->id);
-    if (!dupId) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
+
     return dupId;
 }
 
@@ -268,10 +243,6 @@ gchar *osinfoGetParamValue(OsinfoEntity *self, gchar *key, GError **err)
         return NULL;
 
     firstValueDup = g_strdup(g_ptr_array_index(array, 0));
-    if (!firstValueDup) {
-        g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-        return NULL;
-    }
 
     return firstValueDup;
 }
diff --git a/osinfo/osinfo_filter.c b/osinfo/osinfo_filter.c
index c8dae31..ed3bd6d 100644
--- a/osinfo/osinfo_filter.c
+++ b/osinfo/osinfo_filter.c
@@ -81,21 +81,11 @@ gint osinfoAddFilterConstraint(OsinfoFilter *self, gchar *propName, gchar *propV
 
     valueDup = g_strdup(propVal);
 
-    if (!valueDup)
-        goto error_free;
-
     found = g_tree_lookup_extended(self->priv->propertyConstraints, propName, &origKey, &foundValue);
     if (!found) {
         keyDup = g_strdup(propName);
         valueArray = g_ptr_array_new_with_free_func(g_free);
 
-        if (!valueArray)
-            goto error_free;
-        if (!keyDup) {
-            g_ptr_array_free(valueArray, TRUE);
-            goto error_free;
-        }
-
         g_tree_insert(self->priv->propertyConstraints, keyDup, valueArray);
     }
     else
@@ -104,12 +94,6 @@ gint osinfoAddFilterConstraint(OsinfoFilter *self, gchar *propName, gchar *propV
     // Add a copy of the value to the array
     g_ptr_array_add(valueArray, valueDup);
     return 0;
-
-error_free:
-    g_free(keyDup);
-    g_free(valueDup);
-    g_set_error_literal(err, g_quark_from_static_string("libosinfo"), -ENOMEM, OSINFO_NO_MEM);
-    return -ENOMEM;
 }
 
 // Only applicable to OSes, ignored by other types of objects
diff --git a/osinfo/osinfo_os.c b/osinfo/osinfo_os.c
index a3ba4fe..b43940f 100644
--- a/osinfo/osinfo_os.c
+++ b/osinfo/osinfo_os.c
@@ -74,12 +74,6 @@ static int __osinfoAddOsRelationshipByOs(OsinfoOs *self,
         otherOsIdDup = g_strdup(otherOsId);
         relationshipsForOs = g_ptr_array_new_with_free_func(__osinfoFreeOsLink);
 
-        if (!relationshipsForOs)
-            return -ENOMEM;
-        if (!otherOsIdDup) {
-            g_ptr_array_free(relationshipsForOs, TRUE);
-            return -ENOMEM;
-        }
         g_tree_insert(self->priv->relationshipsByOs, otherOsIdDup, relationshipsForOs);
     }
     else
@@ -205,23 +199,11 @@ struct __osinfoHvSection *__osinfoAddHypervisorSectionToOs(OsinfoOs *self, gchar
                                         g_free,
                                         __osinfoFreeDeviceSection);
 
-        if (!deviceSections)
-            goto error_free;
 
         deviceSectionsAsList = g_tree_new_full(__osinfoStringCompare,
                                                NULL,
                                                g_free,
                                                __osinfoFreePtrArray);
-        if (!deviceSectionsAsList) {
-            g_tree_destroy(deviceSections);
-            goto error_free;
-        }
-
-        if (!hvSection || !hvIdDup) {
-            g_tree_destroy(deviceSectionsAsList);
-            g_tree_destroy(deviceSections);
-            goto error_free;
-        }
 
         hvSection->os = self;
         // Will set hv link later
@@ -233,11 +215,6 @@ struct __osinfoHvSection *__osinfoAddHypervisorSectionToOs(OsinfoOs *self, gchar
     }
     else
         return (struct __osinfoHvSection *) foundValue;
-
-error_free:
-    g_free(hvSection);
-    g_free(hvIdDup);
-    return NULL;
 }
 
 void __osinfoRemoveHvSectionFromOs(OsinfoOs *self, gchar *hvId)
-- 
1.7.2.1




More information about the virt-tools-list mailing list