mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-21 20:15:17 +00:00
util: json: Don't copy string in virJSONValueNewString
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@redhat.com> Reviewed-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Tim Wiederhake <twiederh@redhat.com>
This commit is contained in:
parent
f668e17bea
commit
a8b4a41731
@ -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));
|
||||
|
@ -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);
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user