[libvirt] [PATCH 2/2] vsh: Replace vshPrint macro with function

Peter Krempa pkrempa at redhat.com
Fri Feb 12 13:17:59 UTC 2016


The macro would eat the first parameter. In some cases the format string
for vshPrint was eaten. In other cases the calls referenced variables
which did not exist in the given context. Avoid errors by doing compile
time checking.
---
 tools/virsh-domain.c | 16 +++++++++-------
 tools/vsh.c          | 15 +++++++++++++++
 tools/vsh.h          |  5 ++---
 3 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c
index bf65a60..6dd75e2 100644
--- a/tools/virsh-domain.c
+++ b/tools/virsh-domain.c
@@ -6360,7 +6360,9 @@ static const vshCmdOptDef opts_vcpupin[] = {
  * Helper function to print vcpupin info.
  */
 static bool
-virshPrintPinInfo(unsigned char *cpumap, size_t cpumaplen)
+virshPrintPinInfo(vshControl *ctl,
+                  unsigned char *cpumap,
+                  size_t cpumaplen)
 {
     char *str = NULL;

@@ -6497,7 +6499,7 @@ cmdVcpuPin(vshControl *ctl, const vshCmd *cmd)
                     continue;

                 vshPrint(ctl, "%4zu: ", i);
-                ret = virshPrintPinInfo(VIR_GET_CPUMAP(cpumap, cpumaplen, i),
+                ret = virshPrintPinInfo(ctl, VIR_GET_CPUMAP(cpumap, cpumaplen, i),
                                         cpumaplen);
                 vshPrint(ctl, "\n");
                 if (!ret)
@@ -6606,7 +6608,7 @@ cmdEmulatorPin(vshControl *ctl, const vshCmd *cmd)
             vshPrintExtra(ctl, "%s %s\n", _("emulator:"), _("CPU Affinity"));
             vshPrintExtra(ctl, "----------------------------------\n");
             vshPrintExtra(ctl, "       *: ");
-            ret = virshPrintPinInfo(cpumap, cpumaplen);
+            ret = virshPrintPinInfo(ctl, cpumap, cpumaplen);
             vshPrint(ctl, "\n");
         }
         goto cleanup;
@@ -6782,7 +6784,7 @@ cmdIOThreadInfo(vshControl *ctl, const vshCmd *cmd)
     for (i = 0; i < niothreads; i++) {

         vshPrint(ctl, " %-15u ", info[i]->iothread_id);
-        ignore_value(virshPrintPinInfo(info[i]->cpumap, info[i]->cpumaplen));
+        ignore_value(virshPrintPinInfo(ctl, info[i]->cpumap, info[i]->cpumaplen));
         vshPrint(ctl, "\n");
         virDomainIOThreadInfoFree(info[i]);
     }
@@ -7875,9 +7877,9 @@ cmdMetadata(vshControl *ctl, const vshCmd *cmd)
             goto cleanup;

         if (rem)
-            vshPrint("%s\n", _("Metadata removed"));
+            vshPrint(ctl, "%s\n", _("Metadata removed"));
         else
-            vshPrint("%s\n", _("Metadata modified"));
+            vshPrint(ctl, "%s\n", _("Metadata modified"));
     } else if (edit) {
 #define EDIT_GET_XML \
         virshDomainGetEditMetadata(ctl, dom, uri, flags)
@@ -7893,7 +7895,7 @@ cmdMetadata(vshControl *ctl, const vshCmd *cmd)
                               key, uri, flags) == 0)
 #include "virsh-edit.c"

-        vshPrint("%s\n", _("Metadata modified"));
+        vshPrint(ctl, "%s\n", _("Metadata modified"));
     } else {
         char *data;
         /* get */
diff --git a/tools/vsh.c b/tools/vsh.c
index 91e3106..4467445 100644
--- a/tools/vsh.c
+++ b/tools/vsh.c
@@ -1720,6 +1720,21 @@ vshPrintExtra(vshControl *ctl, const char *format, ...)
 }


+void
+vshPrint(vshControl *ctl ATTRIBUTE_UNUSED, const char *format, ...)
+{
+    va_list ap;
+    char *str;
+
+    va_start(ap, format);
+    if (virVasprintfQuiet(&str, format, ap) < 0)
+        vshErrorOOM();
+    va_end(ap);
+    fputs(str, stdout);
+    VIR_FREE(str);
+}
+
+
 bool
 vshTTYIsInterruptCharacter(vshControl *ctl ATTRIBUTE_UNUSED,
                            const char chr ATTRIBUTE_UNUSED)
diff --git a/tools/vsh.h b/tools/vsh.h
index 0c5abdc..f6e40e6 100644
--- a/tools/vsh.h
+++ b/tools/vsh.h
@@ -300,6 +300,8 @@ const vshCmdOpt *vshCommandOptArgv(vshControl *ctl, const vshCmd *cmd,
 bool vshCommandArgvParse(vshControl *ctl, int nargs, char **argv);
 int vshCommandOptTimeoutToMs(vshControl *ctl, const vshCmd *cmd, int *timeout);

+void vshPrint(vshControl *ctl, const char *format, ...)
+    ATTRIBUTE_FMT_PRINTF(2, 3);
 void vshPrintExtra(vshControl *ctl, const char *format, ...)
     ATTRIBUTE_FMT_PRINTF(2, 3);
 bool vshInit(vshControl *ctl, const vshCmdGrp *groups, const vshCmdDef *set);
@@ -308,9 +310,6 @@ void vshDeinit(vshControl *ctl);
 void vshDebug(vshControl *ctl, int level, const char *format, ...)
     ATTRIBUTE_FMT_PRINTF(3, 4);

-/* XXX: add batch support */
-# define vshPrint(_ctl, ...)   vshPrintExtra(NULL, __VA_ARGS__)
-
 /* User visible sort, so we want locale-specific case comparison.  */
 # define vshStrcasecmp(S1, S2) strcasecmp(S1, S2)
 int vshNameSorter(const void *a, const void *b);
-- 
2.6.2




More information about the libvir-list mailing list