[PATCH v2 6/6] xenapi: Resolve Coverity FORWARD_NULL

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

 



Coverity flagged that if the input 'mask'parameter was NULL, then the
strtok_r call would have a NULL first and third parameter, which would
cause an issue.

Although the caller doesn't call getCpuBitMapfromString with a non-NULL
mask value, Coverity doesn't check that. A fix could have been to check
the 'mask' in the function and keep Coverity happy; however, it was noted
in a previous review that virBitmap* functions could be used instead.

So this patch modifies xenapiDomainGetVcpus to use virBitmap* functions
and removes getCpuBitMapfromString in favor of those.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
---
 src/xenapi/xenapi_driver.c | 12 +++++++-----
 src/xenapi/xenapi_utils.c  | 21 ---------------------
 src/xenapi/xenapi_utils.h  |  2 --
 3 files changed, 7 insertions(+), 28 deletions(-)

diff --git a/src/xenapi/xenapi_driver.c b/src/xenapi/xenapi_driver.c
index 3045c5a..4246c67 100644
--- a/src/xenapi/xenapi_driver.c
+++ b/src/xenapi/xenapi_driver.c
@@ -1269,7 +1269,7 @@ xenapiDomainGetVcpus(virDomainPtr dom,
     virNodeInfo nodeInfo;
     virVcpuInfoPtr ifptr;
     xen_session *session = ((struct _xenapiPrivate *)(dom->conn->privateData))->session;
-    char *mask = NULL;
+    virBitmapPtr mask = NULL;
     if (cpumaps != NULL && maplen < 1)
         return -1;
     if (xenapiDomainGetInfo(dom, &domInfo) == 0) {
@@ -1304,7 +1304,8 @@ xenapiDomainGetVcpus(virDomainPtr dom,
     }
     for (i = 0; i < vcpu_params->size; i++) {
         if (STREQ(vcpu_params->contents[i].key, "mask")) {
-            if (VIR_STRDUP(mask, vcpu_params->contents[i].val) < 0) {
+            if (virBitmapParse(vcpu_params->contents[i].val, 0,
+                               &mask, maplen * CHAR_BIT) < 0) {
                  xen_vm_set_free(vms);
                  xen_string_string_map_free(vcpu_params);
                  return -1;
@@ -1318,10 +1319,11 @@ xenapiDomainGetVcpus(virDomainPtr dom,
         ifptr->state = VIR_VCPU_RUNNING;
         ifptr->cpuTime = 0;
         ifptr->cpu = 0;
-        if (mask != NULL)
-            getCpuBitMapfromString(mask, VIR_GET_CPUMAP(cpumaps, maplen, i), maplen);
+        if (mask && cpumaps)
+            virBitmapToDataBuf(mask, VIR_GET_CPUMAP(cpumaps, maplen, i),
+                               maplen);
     }
-    VIR_FREE(mask);
+    virBitmapFree(mask);
     xen_vm_set_free(vms);
     return i;
 }
diff --git a/src/xenapi/xenapi_utils.c b/src/xenapi/xenapi_utils.c
index a80e084..5c910c8 100644
--- a/src/xenapi/xenapi_utils.c
+++ b/src/xenapi/xenapi_utils.c
@@ -293,27 +293,6 @@ mapDomainPinVcpu(unsigned char *cpumap, int maplen)
     return ret;
 }
 
-/* obtains the CPU bitmap from the string passed */
-void
-getCpuBitMapfromString(char *mask, unsigned char *cpumap, int maplen)
-{
-    int pos;
-    int max_bits = maplen * 8;
-    char *num = NULL, *bp = NULL;
-    bzero(cpumap, maplen);
-    num = strtok_r(mask, ",", &bp);
-    while (num != NULL) {
-        if (virStrToLong_i(num, NULL, 10, &pos) < 0)
-            return;
-        if (pos < 0 || pos > max_bits - 1)
-            VIR_WARN("number in str %d exceeds cpumap's max bits %d", pos, max_bits);
-        else
-            (cpumap)[pos / 8] |= (1 << (pos % 8));
-        num = strtok_r(NULL, ",", &bp);
-    }
-}
-
-
 /* mapping XenServer power state to Libvirt power state */
 virDomainState
 mapPowerState(enum xen_vm_power_state state)
diff --git a/src/xenapi/xenapi_utils.h b/src/xenapi/xenapi_utils.h
index 26e1ac2..9bd49bb 100644
--- a/src/xenapi/xenapi_utils.h
+++ b/src/xenapi/xenapi_utils.h
@@ -59,8 +59,6 @@ xenapiNormalExitEnum2virDomainLifecycle(enum xen_on_normal_exit action);
 virDomainLifecycleCrashAction
 xenapiCrashExitEnum2virDomainLifecycle(enum xen_on_crash_behaviour action);
 
-void getCpuBitMapfromString(char *mask, unsigned char *cpumap, int maplen);
-
 int getStorageVolumeType(char *type);
 
 char *returnErrorFromSession(xen_session *session);
-- 
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]