On 10/05/2016 03:48 AM, Peter Krempa wrote:
On Tue, Oct 04, 2016 at 11:28:55 -0400, John Ferlan wrote:
> Provide the Steal API for any code paths that will desire to grab the
> entire array and then free it afterwards rather than relying to freeing
> the whole chain from the reply.
>
> Signed-off-by: John Ferlan <jferlan(a)redhat.com>
> ---
> src/libvirt_private.syms | 1 +
> src/util/virjson.c | 43 +++++++++++++++++++++++++++++++++++++++++++
> src/util/virjson.h | 2 ++
> 3 files changed, 46 insertions(+)
[...]
> diff --git a/src/util/virjson.c b/src/util/virjson.c
> index b6d9a34..e705f53 100644
> --- a/src/util/virjson.c
> +++ b/src/util/virjson.c
> @@ -770,6 +770,27 @@ virJSONValueObjectGet(virJSONValuePtr object,
> }
>
>
> +static virJSONValuePtr
> +virJSONValueObjectSteal(virJSONValuePtr object,
> + const char *key)
> +{
> + size_t i;
> + virJSONValuePtr obj = NULL;
> +
> + if (object->type != VIR_JSON_TYPE_OBJECT)
> + return NULL;
> +
> + for (i = 0; i < object->data.object.npairs; i++) {
> + if (STREQ(object->data.object.pairs[i].key, key)) {
> + VIR_STEAL_PTR(obj, object->data.object.pairs[i].value);
You steal the data but don't delete the key from the object which makes
the object invalid.
D'oh - squash this in?
diff --git a/src/util/virjson.c b/src/util/virjson.c
index e705f53..1d8e6d5 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -783,6 +783,9 @@ virJSONValueObjectSteal(virJSONValuePtr object,
for (i = 0; i < object->data.object.npairs; i++) {
if (STREQ(object->data.object.pairs[i].key, key)) {
VIR_STEAL_PTR(obj, object->data.object.pairs[i].value);
+ VIR_FREE(object->data.object.pairs[i].key);
+ VIR_DELETE_ELEMENT(object->data.object.pairs, i,
+ object->data.object.npairs);
break;
}
}
John
> + break;
> + }
> + }
> +
> + return obj;
> +}
Peter