[libvirt] [PATCH 05/11] util: use glib string allocation/formatting functions

Daniel P. Berrangé berrange at redhat.com
Fri Sep 27 17:17:27 UTC 2019


Convert the string duplication APIs to use the g_strdup family of APIs.

Annoyingly our virVasprintf/virAsprintf functions return the character
count, even though 90% of our usage doesn't need it. To retain compat
with these semantics we have a call to strlen which costs CPU time.

We previously used the 'strdup-posix' gnulib module because mingw does
not set errno to ENOMEM on failure

We previously used the 'strndup' gnulib module because this function
does not exist on mingw.

We previously used the 'vasprintf' gnulib module because of many GNU
supported format specifiers not working on non-Linux platforms. glib's
own equivalent standardizes on GNU format specifiers too.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 bootstrap.conf       |  3 ---
 src/util/virstring.c | 19 +++++++------------
 2 files changed, 7 insertions(+), 15 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 549d18c6d4..b6b75f9301 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -100,8 +100,6 @@ stat-time
 stdarg
 stpcpy
 strchrnul
-strdup-posix
-strndup
 strerror
 strerror_r-posix
 strptime
@@ -117,7 +115,6 @@ uname
 unsetenv
 useless-if-before-free
 usleep
-vasprintf
 verify
 vc-list-files
 vsnprintf
diff --git a/src/util/virstring.c b/src/util/virstring.c
index a4cc7e9c0a..c8c888b2a0 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -730,12 +730,9 @@ virVasprintfInternal(char **strp,
                      const char *fmt,
                      va_list list)
 {
-    int ret;
+    *strp = g_strdup_vprintf(fmt, list);
 
-    if ((ret = vasprintf(strp, fmt, list)) == -1)
-        abort();
-
-    return ret;
+    return strlen(*strp);
 }
 
 int
@@ -743,12 +740,12 @@ virAsprintfInternal(char **strp,
                     const char *fmt, ...)
 {
     va_list ap;
-    int ret;
 
     va_start(ap, fmt);
-    ret = virVasprintfInternal(strp, fmt, ap);
+    *strp = g_strdup_vprintf(fmt, ap);
     va_end(ap);
-    return ret;
+
+    return strlen(*strp);
 }
 
 /**
@@ -936,8 +933,7 @@ virStrdup(char **dest,
     *dest = NULL;
     if (!src)
         return 0;
-    if (!(*dest = strdup(src)))
-        abort();
+    *dest = g_strdup(src);
 
     return 1;
 }
@@ -965,8 +961,7 @@ virStrndup(char **dest,
         return 0;
     if (n < 0)
         n = strlen(src);
-    if (!(*dest = strndup(src, n)))
-        abort();
+    *dest = g_strndup(src, n);
 
     return 1;
 }
-- 
2.21.0




More information about the libvir-list mailing list