[libvirt] [PATCH 2/9] tests: json: Validate that attribute values are properly stolen

Peter Krempa posted 9 patches 7 years, 1 month ago
[libvirt] [PATCH 2/9] tests: json: Validate that attribute values are properly stolen
Posted by Peter Krempa 7 years, 1 month ago
Make sure that the 'a' and 'A' modifiers for virJSONValueObjectAddVArgs
behave correctly.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
---
 tests/virjsontest.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index 685df7276e..fe72b84340 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -429,6 +429,51 @@ testJSONEscapeObj(const void *data ATTRIBUTE_UNUSED)
 }


+static int
+testJSONObjectFormatSteal(const void *opaque ATTRIBUTE_UNUSED)
+{
+    virJSONValuePtr a1 = NULL;
+    virJSONValuePtr a2 = NULL;
+    virJSONValuePtr t1 = NULL;
+    virJSONValuePtr t2 = NULL;
+    int ret = -1;
+
+    if (!(a1 = virJSONValueNewString("test")) ||
+        !(a2 = virJSONValueNewString("test"))) {
+        VIR_TEST_VERBOSE("Failed to create json object");
+    }
+
+    if (virJSONValueObjectCreate(&t1, "a:t", &a1, "s:f", NULL, NULL) != -1) {
+        VIR_TEST_VERBOSE("virJSONValueObjectCreate(t1) should have failed\n");
+        goto cleanup;
+    }
+
+    if (a1) {
+        VIR_TEST_VERBOSE("appended object a1 was not consumed\n");
+        goto cleanup;
+    }
+
+    if (virJSONValueObjectCreate(&t2, "s:f", NULL, "a:t", &a1, NULL) != -1) {
+        VIR_TEST_VERBOSE("virJSONValueObjectCreate(t2) should have failed\n");
+        goto cleanup;
+    }
+
+    if (!a2) {
+        VIR_TEST_VERBOSE("appended object a2 was consumed\n");
+        goto cleanup;
+    }
+
+    ret = 0;
+
+ cleanup:
+    virJSONValueFree(a1);
+    virJSONValueFree(a2);
+    virJSONValueFree(t1);
+    virJSONValueFree(t2);
+    return ret;
+}
+
+
 static int
 mymain(void)
 {
@@ -588,6 +633,8 @@ mymain(void)
                  NULL, true);
     DO_TEST_FULL("create object with nested json in attribute", EscapeObj,
                  NULL, NULL, true);
+    DO_TEST_FULL("stealing of attributes while creating objects",
+                 ObjectFormatSteal, NULL, NULL, true);

 #define DO_TEST_DEFLATTEN(name, pass) \
     DO_TEST_FULL(name, Deflatten, name, NULL, pass)
-- 
2.16.2

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 2/9] tests: json: Validate that attribute values are properly stolen
Posted by Peter Krempa 7 years, 1 month ago
On Fri, Mar 30, 2018 at 12:59:09 +0200, Peter Krempa wrote:
> Make sure that the 'a' and 'A' modifiers for virJSONValueObjectAddVArgs
> behave correctly.
> 
> Signed-off-by: Peter Krempa <pkrempa@redhat.com>
> ---

If you apply this patch without the fix first you get the following
error in valgrind:

$ ./run valgrind tests/virjsontest
==164888== Memcheck, a memory error detector
==164888== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==164888== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==164888== Command: /home/pipo/libvirt/tests/.libs/virjsontest
==164888== 
TEST: virjsontest
      ........................................ 40 
      .==164888== Invalid free() / delete / delete[] / realloc()
==164888==    at 0x4C2E1AB: free (vg_replace_malloc.c:530)
==164888==    by 0x4EBD808: virFree (viralloc.c:582)
==164888==    by 0x4EF6ACE: virJSONValueFree (virjson.c:382)
==164888==    by 0x4EF6AAC: virJSONValueFree (virjson.c:362)
==164888==    by 0x4EF88A1: virJSONValueObjectCreateVArgs (virjson.c:329)
==164888==    by 0x4EF894B: virJSONValueObjectCreate (virjson.c:344)
==164888==    by 0x10BDFB: testJSONObjectFormatSteal (virjsontest.c:446)
==164888==    by 0x10D74E: virTestRun (testutils.c:180)
==164888==    by 0x10B929: mymain (virjsontest.c:636)
==164888==    by 0x10E92F: virTestMain (testutils.c:1119)
==164888==    by 0x10CE58: main (virjsontest.c:656)
==164888==  Address 0x1ffefffa70 is on thread 1's stack
==164888==  in frame #6, created by testJSONObjectFormatSteal (virjsontest.c:434)
==164888== 
!..........                             52  FAIL

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 2/9] tests: json: Validate that attribute values are properly stolen
Posted by Ján Tomko 7 years, 1 month ago
On Fri, Mar 30, 2018 at 12:59:09PM +0200, Peter Krempa wrote:
>Make sure that the 'a' and 'A' modifiers for virJSONValueObjectAddVArgs
>behave correctly.
>
>Signed-off-by: Peter Krempa <pkrempa@redhat.com>
>---
> tests/virjsontest.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 47 insertions(+)
>

ACK

Jano
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list