[libvirt] [PATCH] Add test suite for viralloc APIs

Nehal J Wani nehaljw.kkd1 at gmail.com
Thu Apr 24 20:25:21 UTC 2014


>>> +static int
>>> +testAllocScalar(const void *opaque ATTRIBUTE_UNUSED)
>>> +{
>>> +    testDummyStruct *t;
>>> +    int ret = -1;
>>> +
>>> +    if (VIR_ALLOC(t) < 0)
>>> +        return -1;
>>> +
>>> +    if (t == NULL) {
>>> +        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
>>> +        goto cleanup;
>>> +    }
>>
>> Just out of curiosity, why don't we have this check after
>> VIR_REALLOC_N, VIR_EXPAND_N, VIR_SHRINK_N and VIR_RESIZE_N ?

As this patch hasn't been pushed yet, maybe the following can be squashed in?

diff --git a/tests/viralloctest.c b/tests/viralloctest.c
index abdd871..d5818c7 100644
--- a/tests/viralloctest.c
+++ b/tests/viralloctest.c
@@ -33,6 +33,17 @@ typedef struct testDummyStruct {
 } testDummyStruct;

 static int
+testCheckNonNull(void *t)
+{
+    if (t == NULL) {
+        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int
 testAllocScalar(const void *opaque ATTRIBUTE_UNUSED)
 {
     testDummyStruct *t;
@@ -41,10 +52,8 @@ testAllocScalar(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_ALLOC(t) < 0)
         return -1;

-    if (t == NULL) {
-        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+    if (testCheckNonNull(t) < 0)
         goto cleanup;
-    }

     if (t->a != 0 ||
         t->b != 0) {
@@ -76,10 +85,8 @@ testAllocArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_ALLOC_N(t, nt) < 0)
         return -1;

-    if (t == NULL) {
-        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+    if (testCheckNonNull(t) < 0)
         goto cleanup;
-    }

     for (i = 0; i < nt; i++) {
         if (t[i].a != 0 ||
@@ -113,10 +120,8 @@ testReallocArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_ALLOC_N(t, nt) < 0)
         return -1;

-    if (t == NULL) {
-        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+    if (testCheckNonNull(t) < 0)
         goto cleanup;
-    }

     for (i = 0; i < nt; i++) {
         t[i].a = 10;
@@ -126,6 +131,9 @@ testReallocArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_REALLOC_N(t, nt + 5) < 0)
         goto cleanup;

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     for (i = 0; i < nt; i++) {
         if (t[i].a != 10 ||
             t[i].b != 20) {
@@ -137,6 +145,9 @@ testReallocArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_REALLOC_N(t, nt) < 0)
         goto cleanup;

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     for (i = 0; i < nt; i++) {
         if (t[i].a != 10 ||
             t[i].b != 20) {
@@ -148,6 +159,9 @@ testReallocArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_REALLOC_N(t, nt - 5) < 0)
         goto cleanup;

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     for (i = 0; i < (nt - 5); i++) {
         if (t[i].a != 10 ||
             t[i].b != 20) {
@@ -180,10 +194,8 @@ testExpandArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_ALLOC_N(t, nt) < 0)
         return -1;

-    if (t == NULL) {
-        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+    if (testCheckNonNull(t) < 0)
         goto cleanup;
-    }

     for (i = 0; i < nt; i++) {
         t[i].a = 10;
@@ -193,6 +205,9 @@ testExpandArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_EXPAND_N(t, nt, 5) < 0)
         goto cleanup;

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     for (i = 0; i < (nt - 5); i++) {
         if (t[i].a != 10 ||
             t[i].b != 20) {
@@ -211,6 +226,9 @@ testExpandArray(const void *opaque ATTRIBUTE_UNUSED)

     VIR_SHRINK_N(t, nt, 5);

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     for (i = 0; i < nt; i++) {
         if (t[i].a != 10 ||
             t[i].b != 20) {
@@ -221,6 +239,9 @@ testExpandArray(const void *opaque ATTRIBUTE_UNUSED)

     VIR_SHRINK_N(t, nt, 5);

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     for (i = 0; i < nt; i++) {
         if (t[i].a != 10 ||
             t[i].b != 20) {
@@ -255,10 +276,8 @@ testResizeArray(const void *opaque ATTRIBUTE_UNUSED)

     at = nt;

-    if (t == NULL) {
-        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+    if (testCheckNonNull(t) < 0)
         goto cleanup;
-    }

     for (i = 0; i < nt; i++) {
         t[i].a = 10;
@@ -268,6 +287,9 @@ testResizeArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_RESIZE_N(t, at, nt, 8) < 0)
         goto cleanup;

+    if (testCheckNonNull(t) < 0)
+        goto cleanup;
+
     if (at != 18) {
         fprintf(stderr, "Expected allocation of 16 not %zu\n", at);
         goto cleanup;
@@ -314,10 +336,8 @@ testInsertArray(const void *opaque ATTRIBUTE_UNUSED)
     if (VIR_ALLOC_N(t, nt) < 0)
         return -1;

-    if (t == NULL) {
-        fprintf(stderr, "Allocation succeeded by pointer is NULL\n");
+    if (testCheckNonNull(t) < 0)
         goto cleanup;
-    }

     for (i = 0; i < nt; i++)
         t[i] = (void*)0x50;


-- 
Nehal J Wani




More information about the libvir-list mailing list