[libvirt] [PATCH V2] virsh: forbid negative vcpu argument to vcpupin.

Jincheng Miao jmiao at redhat.com
Thu May 29 03:34:40 UTC 2014


vcpupin will allow argument --vcpu as a signed number,
and pass it to virDomainPinVcpu directlly without
checking if this value is positive(valid).

> virsh vcpupin r7 -1 0
error: numerical overflow: input too large: 4294967295

This message is inaccurate, and the negative vcpu is
non-valuable. So forbid vcpu argument as a negative.

After patching, the result likes:
> virsh vcpupin r6 -1
error: vcpupin: Invalid vCPU number.

> virsh vcpupin r6 --cpulist 0-1
error: vcpupin: Missing vCPU number in pin mode.

> virsh vcpupin r6 --vcpu ABC
error: vcpupin: Invalid vCPU number in query mode.

Signed-off-by: Jincheng Miao <jmiao at redhat.com>
---
 tools/virsh-domain.c |   42 ++++++++++++++++++++++++------------------
 1 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index 84a6706..e302459 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -5797,7 +5797,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
 {
     virDomainInfo info;
     virDomainPtr dom;
-    int vcpu = -1;
+    unsigned int vcpu;
     const char *cpulist = NULL;
     bool ret = false;
     unsigned char *cpumap = NULL;
@@ -5809,6 +5809,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
     bool live = vshCommandOptBool(cmd, "live");
     bool current = vshCommandOptBool(cmd, "current");
     bool query = false; /* Query mode if no cpulist */
+    int get_vcpu;
     unsigned int flags = VIR_DOMAIN_AFFECT_CURRENT;
 
     VSH_EXCLUSIVE_OPTIONS_VAR(current, live);
@@ -5830,29 +5831,34 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
 
     query = !cpulist;
 
-    /* In query mode, "vcpu" is optional */
-    if (vshCommandOptInt(cmd, "vcpu", &vcpu) < !query) {
-        vshError(ctl, "%s",
-                 _("vcpupin: Invalid or missing vCPU number."));
-        virDomainFree(dom);
-        return false;
-    }
-
-    if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) {
-        virDomainFree(dom);
-        return false;
+    get_vcpu = vshCommandOptUInt(cmd, "vcpu", &vcpu);
+    if (get_vcpu <= 0) {
+        /* In query mode, "vcpu" is optional */
+        if (query && get_vcpu < 0) {
+            vshError(ctl, "%s",
+                     _("vcpupin: Invalid vCPU number in query mode."));
+            goto cleanup;
+        }
+        /* In pin mode, "vcpu" is necessary */
+        if (!query) {
+            vshError(ctl, "%s",
+                     _("vcpupin: Missing vCPU number in pin mode."));
+            goto cleanup;
+        }
     }
 
     if (virDomainGetInfo(dom, &info) != 0) {
         vshError(ctl, "%s", _("vcpupin: failed to get domain information."));
-        virDomainFree(dom);
-        return false;
+        goto cleanup;
     }
 
     if (vcpu >= info.nrVirtCpu) {
         vshError(ctl, "%s", _("vcpupin: Invalid vCPU number."));
-        virDomainFree(dom);
-        return false;
+        goto cleanup;
+    }
+
+    if ((maxcpu = vshNodeGetCPUCount(ctl->conn)) < 0) {
+        goto cleanup;
     }
 
     cpumaplen = VIR_CPU_MAPLEN(maxcpu);
@@ -5871,7 +5877,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
             vshPrintExtra(ctl, "%s %s\n", _("VCPU:"), _("CPU Affinity"));
             vshPrintExtra(ctl, "----------------------------------\n");
             for (i = 0; i < ncpus; i++) {
-               if (vcpu != -1 && i != vcpu)
+               if (get_vcpu > 0 && i != vcpu)
                    continue;
 
                vshPrint(ctl, "%4zu: ", i);
@@ -5880,8 +5886,8 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
                if (!ret)
                    break;
             }
-
         }
+
         VIR_FREE(cpumaps);
         goto cleanup;
     }
-- 
1.7.1




More information about the libvir-list mailing list