[PATCH 05/33] virCommandAddArgBuffer: Simplify clearing of @buf

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

 



Get the buffer contents into a temporary variable with automatic
clearing so that the error branches don't have to reset the buffer.
Additionally handle the NULL string case before assignment.

Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---
 src/util/vircommand.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index b94ab615d5..f11caf0d6e 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -1550,21 +1550,21 @@ virCommandAddArg(virCommandPtr cmd, const char *val)
 void
 virCommandAddArgBuffer(virCommandPtr cmd, virBufferPtr buf)
 {
-    if (!cmd || cmd->has_error) {
-        virBufferFreeAndReset(buf);
+    g_autofree char *str = virBufferContentAndReset(buf);
+
+    if (!cmd || cmd->has_error)
         return;
-    }
+
+    if (!str)
+        str = g_strdup("");

     /* Arg plus trailing NULL. */
     if (VIR_RESIZE_N(cmd->args, cmd->maxargs, cmd->nargs, 1 + 1) < 0) {
         cmd->has_error = ENOMEM;
-        virBufferFreeAndReset(buf);
         return;
     }

-    cmd->args[cmd->nargs] = virBufferContentAndReset(buf);
-    if (!cmd->args[cmd->nargs])
-        cmd->args[cmd->nargs] = g_strdup("");
+    cmd->args[cmd->nargs] = g_steal_pointer(&str);
     cmd->nargs++;
 }

-- 
2.29.2




[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]

  Powered by Linux