[libvirt] [PATCHv2 7/7 util: Avoid Coverity FORWARD_NULL

John Ferlan jferlan at redhat.com
Wed Jul 1 16:41:23 UTC 2015


Avoid a false positive since Coverity find a path in virResizeN which
could return 0 prior to the allocation of memory and thus flags a
possible NULL dereference. Instead allocate the output buffer based
on 'nparams' and only fill it partially if need be - shouldn't be too
much a waste of space. Quicker than multiple VIR_RESIZE_N calls or
two loops of STREQ's sandwiched around a single VIR_ALLOC_N using
'n' matches from a first loop to generate the 'n' addresses to return

Signed-off-by: John Ferlan <jferlan at redhat.com>
---

Difference to v1: Use single VIR_ALLOC_N of nparams and one loop as
suggested in review

 src/util/virtypedparam.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 106403c..f3ce157 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -501,21 +501,18 @@ virTypedParamsFilter(virTypedParameterPtr params,
                      const char *name,
                      virTypedParameterPtr **ret)
 {
-    size_t i, alloc = 0, n = 0;
+    size_t i, n = 0;
 
     virCheckNonNullArgGoto(params, error);
     virCheckNonNullArgGoto(name, error);
     virCheckNonNullArgGoto(ret, error);
 
-    *ret = NULL;
+    if (VIR_ALLOC_N(*ret, nparams) < 0)
+        goto error;
 
     for (i = 0; i < nparams; i++) {
         if (STREQ(params[i].field, name)) {
-            if (VIR_RESIZE_N(*ret, alloc, n, 1) < 0)
-                goto error;
-
             (*ret)[n] = &params[i];
-
             n++;
         }
     }
-- 
2.1.0




More information about the libvir-list mailing list