[PATCH 3/6] util: json: Don't copy string in virJSONValueNewString

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

 



With 'g_strdup' not needing error handling we can ask callers to pass a
copy of the string which will be adopted by the JSON value.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/virjson.c  | 17 +++++++----------
 src/util/virjson.h  |  2 +-
 tests/virjsontest.c |  4 ++--
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index dbc5b7840c..37301bb87e 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -412,7 +412,7 @@ virJSONValueHashFree(void *opaque)


 virJSONValue *
-virJSONValueNewString(const char *data)
+virJSONValueNewString(char *data)
 {
     virJSONValue *val;

@@ -422,7 +422,7 @@ virJSONValueNewString(const char *data)
     val = g_new0(virJSONValue, 1);

     val->type = VIR_JSON_TYPE_STRING;
-    val->data.string = g_strdup(data);
+    val->data.string = data;

     return val;
 }
@@ -608,12 +608,9 @@ virJSONValueObjectInsertString(virJSONValue *object,
                                const char *value,
                                bool prepend)
 {
-    virJSONValue *jvalue = virJSONValueNewString(value);
-    if (!jvalue)
-        return -1;
-    if (virJSONValueObjectInsert(object, key, &jvalue, prepend) < 0)
-        return -1;
-    return 0;
+    g_autoptr(virJSONValue) jvalue = virJSONValueNewString(g_strdup(value));
+
+    return virJSONValueObjectInsert(object, key, &jvalue, prepend);
 }


@@ -775,7 +772,7 @@ int
 virJSONValueArrayAppendString(virJSONValue *object,
                               const char *value)
 {
-    g_autoptr(virJSONValue) jvalue = virJSONValueNewString(value);
+    g_autoptr(virJSONValue) jvalue = virJSONValueNewString(g_strdup(value));

     if (virJSONValueArrayAppend(object, &jvalue) < 0)
         return -1;
@@ -1438,7 +1435,7 @@ virJSONValueCopy(const virJSONValue *in)

     /* No need to error out in the following cases */
     case VIR_JSON_TYPE_STRING:
-        out = virJSONValueNewString(in->data.string);
+        out = virJSONValueNewString(g_strdup(in->data.string));
         break;
     case VIR_JSON_TYPE_NUMBER:
         out = virJSONValueNewNumber(g_strdup(in->data.number));
diff --git a/src/util/virjson.h b/src/util/virjson.h
index 361b0190e8..71b88183fe 100644
--- a/src/util/virjson.h
+++ b/src/util/virjson.h
@@ -50,7 +50,7 @@ int virJSONValueObjectAddVArgs(virJSONValue **objptr, va_list args)
     ATTRIBUTE_NONNULL(1);


-virJSONValue *virJSONValueNewString(const char *data);
+virJSONValue *virJSONValueNewString(char *data);
 virJSONValue *virJSONValueNewStringLen(const char *data, size_t length);
 virJSONValue *virJSONValueNewNumberInt(int data);
 virJSONValue *virJSONValueNewNumberUint(unsigned int data);
diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index ddb93803b7..78283b632a 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -441,8 +441,8 @@ testJSONObjectFormatSteal(const void *opaque G_GNUC_UNUSED)
     g_autoptr(virJSONValue) t1 = NULL;
     g_autoptr(virJSONValue) t2 = NULL;

-    if (!(a1 = virJSONValueNewString("test")) ||
-        !(a2 = virJSONValueNewString("test"))) {
+    if (!(a1 = virJSONValueNewString(g_strdup("test"))) ||
+        !(a2 = virJSONValueNewString(g_strdup("test")))) {
         VIR_TEST_VERBOSE("Failed to create json object");
     }

-- 
2.31.1




[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