[PATCH 23/25] virJSONParserHandle*: Refactor memory cleanup and drop NULL checks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



virJSONValueNew* won't return error nowadays so NULL checks are not
necessary. The memory can be cleared via g_autoptr.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/virjson.c | 68 ++++++++++++++++------------------------------
 1 file changed, 24 insertions(+), 44 deletions(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index 65215f03c5..de83441e27 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1604,17 +1604,13 @@ static int
 virJSONParserHandleNull(void *ctx)
 {
     virJSONParserPtr parser = ctx;
-    virJSONValuePtr value = virJSONValueNewNull();
+    g_autoptr(virJSONValue) value = virJSONValueNewNull();

     VIR_DEBUG("parser=%p", parser);

-    if (!value)
+    if (virJSONParserInsertValue(parser, value) < 0)
         return 0;
-
-    if (virJSONParserInsertValue(parser, value) < 0) {
-        virJSONValueFree(value);
-        return 0;
-    }
+    value = NULL;

     return 1;
 }
@@ -1625,17 +1621,13 @@ virJSONParserHandleBoolean(void *ctx,
                            int boolean_)
 {
     virJSONParserPtr parser = ctx;
-    virJSONValuePtr value = virJSONValueNewBoolean(boolean_);
+    g_autoptr(virJSONValue) value = virJSONValueNewBoolean(boolean_);

     VIR_DEBUG("parser=%p boolean=%d", parser, boolean_);

-    if (!value)
-        return 0;
-
-    if (virJSONParserInsertValue(parser, value) < 0) {
-        virJSONValueFree(value);
+    if (virJSONParserInsertValue(parser, value) < 0)
         return 0;
-    }
+    value = NULL;

     return 1;
 }
@@ -1647,22 +1639,14 @@ virJSONParserHandleNumber(void *ctx,
                           size_t l)
 {
     virJSONParserPtr parser = ctx;
-    char *str;
-    virJSONValuePtr value;
-
-    str = g_strndup(s, l);
-    value = virJSONValueNewNumber(str);
-    VIR_FREE(str);
+    g_autofree char *str = g_strndup(s, l);
+    g_autoptr(virJSONValue) value = virJSONValueNewNumber(str);

     VIR_DEBUG("parser=%p str=%s", parser, str);

-    if (!value)
+    if (virJSONParserInsertValue(parser, value) < 0)
         return 0;
-
-    if (virJSONParserInsertValue(parser, value) < 0) {
-        virJSONValueFree(value);
-        return 0;
-    }
+    value = NULL;

     return 1;
 }
@@ -1674,18 +1658,14 @@ virJSONParserHandleString(void *ctx,
                           size_t stringLen)
 {
     virJSONParserPtr parser = ctx;
-    virJSONValuePtr value = virJSONValueNewStringLen((const char *)stringVal,
-                                                     stringLen);
+    g_autoptr(virJSONValue) value = virJSONValueNewStringLen((const char *)stringVal,
+                                                             stringLen);

     VIR_DEBUG("parser=%p str=%p", parser, (const char *)stringVal);

-    if (!value)
-        return 0;
-
-    if (virJSONParserInsertValue(parser, value) < 0) {
-        virJSONValueFree(value);
+    if (virJSONParserInsertValue(parser, value) < 0)
         return 0;
-    }
+    value = NULL;

     return 1;
 }
@@ -1716,21 +1696,21 @@ static int
 virJSONParserHandleStartMap(void *ctx)
 {
     virJSONParserPtr parser = ctx;
-    virJSONValuePtr value = virJSONValueNewObject();
+    g_autoptr(virJSONValue) value = virJSONValueNewObject();
+    virJSONValuePtr tmp = value;

     VIR_DEBUG("parser=%p", parser);

-    if (virJSONParserInsertValue(parser, value) < 0) {
-        virJSONValueFree(value);
+    if (virJSONParserInsertValue(parser, value) < 0)
         return 0;
-    }
+    value = NULL;

     if (VIR_REALLOC_N(parser->state,
                       parser->nstate + 1) < 0) {
         return 0;
     }

-    parser->state[parser->nstate].value = value;
+    parser->state[parser->nstate].value = tmp;
     parser->state[parser->nstate].key = NULL;
     parser->nstate++;

@@ -1765,20 +1745,20 @@ static int
 virJSONParserHandleStartArray(void *ctx)
 {
     virJSONParserPtr parser = ctx;
-    virJSONValuePtr value = virJSONValueNewArray();
+    g_autoptr(virJSONValue) value = virJSONValueNewArray();
+    virJSONValuePtr tmp = value;

     VIR_DEBUG("parser=%p", parser);

-    if (virJSONParserInsertValue(parser, value) < 0) {
-        virJSONValueFree(value);
+    if (virJSONParserInsertValue(parser, value) < 0)
         return 0;
-    }
+    value = NULL;

     if (VIR_REALLOC_N(parser->state,
                       parser->nstate + 1) < 0)
         return 0;

-    parser->state[parser->nstate].value = value;
+    parser->state[parser->nstate].value = tmp;
     parser->state[parser->nstate].key = NULL;
     parser->nstate++;

-- 
2.29.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux