]> xenbits.xensource.com Git - libvirt.git/commitdiff
bhyve: use g_auto() for all virBuffers
authorLaine Stump <laine@redhat.com>
Thu, 2 Jul 2020 22:03:45 +0000 (18:03 -0400)
committerLaine Stump <laine@redhat.com>
Wed, 8 Jul 2020 20:32:17 +0000 (16:32 -0400)
In most cases this eliminates one or more calls to
virBufferClearAndReset(), but even when it doesn't it's better because:

1) it makes the code more consistent, making it more likely that new
   contributors who are "learning by example" will to the right thing.

2) it protects against future modifications that might have otherwise
   needed to add a virBufferClearAndReset()

3) Currently some functions don't call virBufferClearAndReset() only
   because they're relying on some subordinate function to call it for
   them (e.g. bhyveConnectGetSysinfo() in this patch relies on
   virSysinfoFormat() to clear out the buffer when there is an
   error). I think this is sloppy behavior, and that the toplevel
   function that defines and initializes the buffer should be the
   function clearing it at the end.

Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/bhyve/bhyve_command.c
src/bhyve/bhyve_driver.c

index 5b1d80083a81b0cc4b8e86993be22f2516208d97..9649c2d2a247c53134d34d1902db9e8e13662a11 100644 (file)
@@ -166,14 +166,15 @@ bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
                                bhyveConnPtr driver,
                                virCommandPtr cmd)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
-    virBuffer device = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     const char *disk_source;
     size_t i;
     int ret = -1;
 
     for (i = 0; i < def->ndisks; i++) {
+        g_auto(virBuffer) device = VIR_BUFFER_INITIALIZER;
         virDomainDiskDefPtr disk = def->disks[i];
+
         if (disk->bus != VIR_DOMAIN_DISK_BUS_SATA)
             continue;
 
@@ -221,7 +222,6 @@ bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
             goto error;
         }
         virBufferAddBuffer(&buf, &device);
-        virBufferFreeAndReset(&device);
     }
 
     virCommandAddArg(cmd, "-s");
@@ -231,7 +231,6 @@ bhyveBuildAHCIControllerArgStr(const virDomainDef *def,
 
     ret = 0;
  error:
-    virBufferFreeAndReset(&buf);
     return ret;
 }
 
@@ -378,7 +377,7 @@ bhyveBuildGraphicsArgStr(const virDomainDef *def,
                          virCommandPtr cmd,
                          bool dryRun)
 {
-    virBuffer opt = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) opt = VIR_BUFFER_INITIALIZER;
     virDomainGraphicsListenDefPtr glisten = NULL;
     bool escapeAddr;
     unsigned short port;
@@ -478,7 +477,6 @@ bhyveBuildGraphicsArgStr(const virDomainDef *def,
     return 0;
 
  error:
-    virBufferFreeAndReset(&opt);
     return -1;
 }
 
@@ -765,7 +763,6 @@ virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
                                  char **devicesmap_out)
 {
     virDomainDiskDefPtr hdd, cd, userdef, diskdef;
-    virBuffer devicemap;
     virCommandPtr cmd;
     unsigned int best_idx = UINT_MAX;
     size_t i;
@@ -773,8 +770,6 @@ virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
     if (def->os.bootloaderArgs != NULL)
         return virBhyveProcessBuildCustomLoaderCmd(def);
 
-    devicemap = (virBuffer)VIR_BUFFER_INITIALIZER;
-
     /* Search disk list for CD or HDD device. We'll respect <boot order=''> if
      * present and otherwise pick the first CD or failing that HDD we come
      * across. */
@@ -809,6 +804,8 @@ virBhyveProcessBuildGrubbhyveCmd(virDomainDefPtr def,
     VIR_DEBUG("grub-bhyve with default arguments");
 
     if (devicesmap_out != NULL) {
+        g_auto(virBuffer) devicemap = VIR_BUFFER_INITIALIZER;
+
         /* Grub device.map (just for boot) */
         if (userdef != NULL) {
             virBhyveFormatGrubDevice(&devicemap, userdef);
index b6204c7fb9653a4012bd10c024e274a45f878f8d..daa20bad4057be34f784c47002f9d0a4391e32af 100644 (file)
@@ -244,7 +244,7 @@ static char *
 bhyveConnectGetSysinfo(virConnectPtr conn, unsigned int flags)
 {
     bhyveConnPtr privconn = conn->privateData;
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
     virCheckFlags(0, NULL);
 
@@ -678,7 +678,7 @@ bhyveConnectDomainXMLToNative(virConnectPtr conn,
                               const char *xmlData,
                               unsigned int flags)
 {
-    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
     bhyveConnPtr privconn = conn->privateData;
     virDomainDefPtr def = NULL;
     virCommandPtr cmd = NULL, loadcmd = NULL;