[PATCH 5/6] buf: Simplify virBufferEscapeString

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

 



We are about to copy this function, so clean it up before we do.

Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 src/util/buf.c |   38 +++++++-------------------------------
 1 files changed, 7 insertions(+), 31 deletions(-)

diff --git a/src/util/buf.c b/src/util/buf.c
index 553e2a0..702bb10 100644
--- a/src/util/buf.c
+++ b/src/util/buf.c
@@ -282,7 +282,7 @@ err:
 void
 virBufferEscapeString(const virBufferPtr buf, const char *format, const char *str)
 {
-    int size, count, len, grow_size;
+    int len;
     char *escaped, *out;
     const char *cur;
 
@@ -293,6 +293,11 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st
         return;
 
     len = strlen(str);
+    if (strcspn(str, "<>&'\"") == len) {
+        virBufferVSprintf(buf, format, str);
+        return;
+    }
+
     if (VIR_ALLOC_N(escaped, 6 * len + 1) < 0) {
         virBufferNoMemory(buf);
         return;
@@ -345,36 +350,7 @@ virBufferEscapeString(const virBufferPtr buf, const char *format, const char *st
     }
     *out = 0;
 
-    if ((buf->use >= buf->size) &&
-        virBufferGrow(buf, 100) < 0) {
-        goto err;
-    }
-
-    size = buf->size - buf->use;
-    if ((count = snprintf(&buf->content[buf->use], size,
-                          format, (char *)escaped)) < 0) {
-            buf->error = 1;
-        goto err;
-    }
-
-    /* Grow buffer if necessary and retry */
-    if (count >= size) {
-        buf->content[buf->use] = 0;
-        grow_size = (count + 1 > 1000) ? count + 1 : 1000;
-        if (virBufferGrow(buf, grow_size) < 0) {
-            goto err;
-        }
-        size = buf->size - buf->use;
-
-        if ((count = snprintf(&buf->content[buf->use], size,
-                              format, (char *)escaped)) < 0) {
-            buf->error = 1;
-            goto err;
-        }
-    }
-    buf->use += count;
-
-err:
+    virBufferVSprintf(buf, format, escaped);
     VIR_FREE(escaped);
 }
 
-- 
1.7.3.2

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


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