[PATCH 7/7] util: Avoid Coverity FORWARD_NULL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 use multiple loops to first count
the number of matches, perform one allocation, and then again search
for matches.  It's a 'n' string comparisons and allocations versus
2*'n' string comparisons and one allocation.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/util/virtypedparam.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index 106403c..79c7cab 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -501,7 +501,7 @@ 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);
@@ -510,12 +510,16 @@ virTypedParamsFilter(virTypedParameterPtr params,
     *ret = NULL;
 
     for (i = 0; i < nparams; i++) {
-        if (STREQ(params[i].field, name)) {
-            if (VIR_RESIZE_N(*ret, alloc, n, 1) < 0)
-                goto error;
+        if (STREQ(params[i].field, name))
+            n++;
+    }
 
-            (*ret)[n] = &params[i];
+    if (VIR_ALLOC_N(*ret, n) < 0)
+        goto error;
 
+    for (n = 0, i = 0; i < nparams; i++) {
+        if (STREQ(params[i].field, name)) {
+            (*ret)[n] = &params[i];
             n++;
         }
     }
-- 
2.1.0

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



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]