]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virbuffer: Simplify virBufferEscapeShell()
authorAndrea Bolognani <abologna@redhat.com>
Fri, 11 Feb 2022 16:14:52 +0000 (17:14 +0100)
committerAndrea Bolognani <abologna@redhat.com>
Mon, 14 Feb 2022 10:31:58 +0000 (11:31 +0100)
We can exit early when the input is an empty string, and we can
avoid storing the string length in a variable since we only use
that information once.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virbuffer.c

index a4834174a16f7cfed8f9354b7caff9cbddd76b4d..19cf775a8c07748a363a2c088b785bae0f345f67 100644 (file)
@@ -552,7 +552,6 @@ virBufferURIEncodeString(virBuffer *buf, const char *str)
 void
 virBufferEscapeShell(virBuffer *buf, const char *str)
 {
-    int len;
     g_autofree char *escaped = NULL;
     char *out;
     const char *cur;
@@ -560,21 +559,19 @@ virBufferEscapeShell(virBuffer *buf, const char *str)
     if ((buf == NULL) || (str == NULL))
         return;
 
-    /* Only quote if str includes shell metacharacters. */
-    if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
-        virBufferAdd(buf, str, -1);
+    if (!*str) {
+        virBufferAddLit(buf, "''");
         return;
     }
 
-    if (*str) {
-        len = strlen(str);
-
-        escaped = g_malloc0_n(len + 1, 4);
-    } else {
-        virBufferAddLit(buf, "''");
+    /* Only quote if str includes shell metacharacters. */
+    if (!strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) {
+        virBufferAdd(buf, str, -1);
         return;
     }
 
+    escaped = g_malloc0_n(strlen(str) + 1, 4);
+
     cur = str;
     out = escaped;