[libvirt] [PATCH 4/5] util: Get rid of "no_memory" labels

Fabiano Fidêncio fidencio at redhat.com
Fri Dec 20 12:43:14 UTC 2019


As pointed out by Ján Tomko, "no_memory seems suspicious in the times of
abort()".

As libvirt decided to take the path to not report OOM and simply abort
when it happens, let's get rid of the no_memory labels and simplify the
code around them.

Mind that virfirewall.c was not touched and still contains no_memory
labels. The reason those are left behind, at least for now, is because
the conversion seems to be slightly more complicated than the rest, as
some other places are relying on firewall->err being set to ENOMEM.

Signed-off-by: Fabiano Fidêncio <fidencio at redhat.com>
---
 src/util/virsysinfo.c | 64 ++++++++++++++++---------------------------
 src/util/virsysinfo.h |  2 ++
 src/util/viruri.c     | 28 +++++++------------
 3 files changed, 35 insertions(+), 59 deletions(-)

diff --git a/src/util/virsysinfo.c b/src/util/virsysinfo.c
index 8132ab7a07..e5af4f25e0 100644
--- a/src/util/virsysinfo.c
+++ b/src/util/virsysinfo.c
@@ -302,33 +302,27 @@ virSysinfoParsePPCProcessor(const char *base, virSysinfoDefPtr ret)
 virSysinfoDefPtr
 virSysinfoReadPPC(void)
 {
-    virSysinfoDefPtr ret = NULL;
-    char *outbuf = NULL;
+    g_auto(virSysinfoDefPtr) ret = NULL;
+    g_autofree char *outbuf = NULL;
 
     if (VIR_ALLOC(ret) < 0)
-        goto no_memory;
+        return NULL;
 
     if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to open %s"), CPUINFO);
-        goto no_memory;
+        return NULL;
     }
 
     ret->nprocessor = 0;
     ret->processor = NULL;
     if (virSysinfoParsePPCProcessor(outbuf, ret) < 0)
-        goto no_memory;
+        return NULL;
 
     if (virSysinfoParsePPCSystem(outbuf, &ret->system) < 0)
-        goto no_memory;
-
-    VIR_FREE(outbuf);
-    return ret;
+        return NULL;
 
- no_memory:
-    VIR_FREE(outbuf);
-    virSysinfoDefFree(ret);
-    return NULL;
+    return g_steal_pointer(&ret);
 }
 
 
@@ -433,13 +427,13 @@ virSysinfoParseARMProcessor(const char *base, virSysinfoDefPtr ret)
 virSysinfoDefPtr
 virSysinfoReadARM(void)
 {
-    virSysinfoDefPtr ret = NULL;
-    char *outbuf = NULL;
+    g_auto(virSysinfoDefPtr) ret = NULL;
+    g_autofree char *outbuf = NULL;
 
     /* Some ARM systems have DMI tables available. */
     if ((ret = virSysinfoReadDMI())) {
         if (!virSysinfoDefIsEmpty(ret))
-            return ret;
+            return g_steal_pointer(&ret);
         virSysinfoDefFree(ret);
     }
 
@@ -447,29 +441,23 @@ virSysinfoReadARM(void)
     virResetLastError();
 
     if (VIR_ALLOC(ret) < 0)
-        goto no_memory;
+        return NULL;
 
     if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to open %s"), CPUINFO);
-        goto no_memory;
+        return NULL;
     }
 
     ret->nprocessor = 0;
     ret->processor = NULL;
     if (virSysinfoParseARMProcessor(outbuf, ret) < 0)
-        goto no_memory;
+        return NULL;
 
     if (virSysinfoParseARMSystem(outbuf, &ret->system) < 0)
-        goto no_memory;
-
-    VIR_FREE(outbuf);
-    return ret;
+        return NULL;
 
- no_memory:
-    VIR_FREE(outbuf);
-    virSysinfoDefFree(ret);
-    return NULL;
+    return g_steal_pointer(&ret);
 }
 
 static char *
@@ -606,21 +594,21 @@ virSysinfoParseS390Processor(const char *base, virSysinfoDefPtr ret)
 virSysinfoDefPtr
 virSysinfoReadS390(void)
 {
-    virSysinfoDefPtr ret = NULL;
-    char *outbuf = NULL;
+    g_auto(virSysinfoDefPtr) ret = NULL;
+    g_autofree char *outbuf = NULL;
 
     if (VIR_ALLOC(ret) < 0)
-        goto no_memory;
+        return NULL;
 
     /* Gather info from /proc/cpuinfo */
     if (virFileReadAll(CPUINFO, CPUINFO_FILE_LEN, &outbuf) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to open %s"), CPUINFO);
-        goto no_memory;
+        return NULL;
     }
 
     if (virSysinfoParseS390Processor(outbuf, ret) < 0)
-        goto no_memory;
+        return NULL;
 
     /* Free buffer before reading next file */
     VIR_FREE(outbuf);
@@ -629,19 +617,13 @@ virSysinfoReadS390(void)
     if (virFileReadAll(SYSINFO, 8192, &outbuf) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to open %s"), SYSINFO);
-        goto no_memory;
+        return NULL;
     }
 
     if (virSysinfoParseS390System(outbuf, &ret->system) < 0)
-        goto no_memory;
-
-    VIR_FREE(outbuf);
-    return ret;
+        return NULL;
 
- no_memory:
-    virSysinfoDefFree(ret);
-    VIR_FREE(outbuf);
-    return NULL;
+    return g_steal_pointer(&ret);
 }
 
 
diff --git a/src/util/virsysinfo.h b/src/util/virsysinfo.h
index f30809294b..3ce936205c 100644
--- a/src/util/virsysinfo.h
+++ b/src/util/virsysinfo.h
@@ -144,6 +144,8 @@ void virSysinfoChassisDefFree(virSysinfoChassisDefPtr def);
 void virSysinfoOEMStringsDefFree(virSysinfoOEMStringsDefPtr def);
 void virSysinfoDefFree(virSysinfoDefPtr def);
 
+G_DEFINE_AUTO_CLEANUP_FREE_FUNC(virSysinfoDefPtr, virSysinfoDefFree, NULL);
+
 int virSysinfoFormat(virBufferPtr buf, virSysinfoDefPtr def)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
 
diff --git a/src/util/viruri.c b/src/util/viruri.c
index 11163a2b40..1b848bd336 100644
--- a/src/util/viruri.c
+++ b/src/util/viruri.c
@@ -71,7 +71,8 @@ virURIParseParams(virURIPtr uri)
         return 0;
 
     while (*query) {
-        char *name = NULL, *value = NULL;
+        g_autofree char *name = NULL;
+        g_autofree char *value = NULL;
 
         /* Find the next separator, or end of the string. */
         end = strchr(query, '&');
@@ -92,13 +93,15 @@ virURIParseParams(virURIPtr uri)
              * and consistent with CGI.pm we assume value is "".
              */
             name = xmlURIUnescapeString(query, end - query, NULL);
-            if (!name) goto no_memory;
+            if (!name)
+                return -1;
         } else if (eq+1 == end) {
             /* Or if we have "name=" here (works around annoying
              * problem when calling xmlURIUnescapeString with len = 0).
              */
             name = xmlURIUnescapeString(query, eq - query, NULL);
-            if (!name) goto no_memory;
+            if (!name)
+                return -1;
         } else if (query == eq) {
             /* If the '=' character is at the beginning then we have
              * "=value" and consistent with CGI.pm we _ignore_ this.
@@ -108,22 +111,15 @@ virURIParseParams(virURIPtr uri)
             /* Otherwise it's "name=value". */
             name = xmlURIUnescapeString(query, eq - query, NULL);
             if (!name)
-                goto no_memory;
+                return -1;
             value = xmlURIUnescapeString(eq+1, end - (eq+1), NULL);
-            if (!value) {
-                VIR_FREE(name);
-                goto no_memory;
-            }
+            if (!value)
+                return -1;
         }
 
         /* Append to the parameter set. */
-        if (virURIParamAppend(uri, name, NULLSTR_EMPTY(value)) < 0) {
-            VIR_FREE(name);
-            VIR_FREE(value);
+        if (virURIParamAppend(uri, name, NULLSTR_EMPTY(value)) < 0)
             return -1;
-        }
-        VIR_FREE(name);
-        VIR_FREE(value);
 
     next:
         query = end;
@@ -131,10 +127,6 @@ virURIParseParams(virURIPtr uri)
     }
 
     return 0;
-
- no_memory:
-    virReportOOMError();
-    return -1;
 }
 
 /**
-- 
2.24.1




More information about the libvir-list mailing list