]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: use glib string allocation/formatting functions
authorDaniel P. Berrangé <berrange@redhat.com>
Fri, 6 Sep 2019 12:10:24 +0000 (13:10 +0100)
committerDaniel P. Berrangé <berrange@redhat.com>
Mon, 14 Oct 2019 09:54:42 +0000 (10:54 +0100)
Convert the string duplication APIs to use the g_strdup family of APIs.

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.

Reviewed-by: Ján Tomko <jtomko@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
bootstrap.conf
docs/hacking.html.in
src/util/virstring.c
src/util/virstring.h

index 7d73584809dde98b94484b509cfd9a1889c7b34b..7e264b63ad44725db409c1b10573287a33e2d33a 100644 (file)
@@ -80,8 +80,6 @@ snprintf
 socket
 stat-time
 strchrnul
-strdup-posix
-strndup
 strerror
 strerror_r-posix
 strptime
@@ -96,7 +94,6 @@ ttyname_r
 uname
 unsetenv
 usleep
-vasprintf
 verify
 vsnprintf
 waitpid
index 807279631264ed5c5d694e073ac11b69bfd43662..3f1542b6dece7cf49537c07babbf214d5688aa23 100644 (file)
@@ -1020,6 +1020,14 @@ BAD:
         classic libvirt memory allocation APIs and GLib APIs within
         a single method. Keep the style consistent, converting existing
         code to GLib style in a separate, prior commit.</dd>
+
+      <dt>VIR_STRDUP, VIR_STRNDUP</dt>
+      <dd>Prefer the GLib APIs g_strdup and g_strndup.</dd>
+
+      <dt>virAsprintf, virVasprintf</dt>
+      <dd>The GLib APIs g_strdup_printf / g_strdup_vprint should be used
+        instead. Don't use g_vasprintf unless having the string length
+        returned is unavoidable.</dd>
     </dl>
 
     <h2><a id="file_handling">File handling</a></h2>
index a4cc7e9c0a553293319e7c52eb60823bfeb8f427..6b2b6ed24efd76e87faa052830e111c1c2fbc462 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <config.h>
 
+#include <glib/gprintf.h>
 #include <regex.h>
 #include <locale.h>
 
@@ -730,10 +731,21 @@ virVasprintfInternal(char **strp,
                      const char *fmt,
                      va_list list)
 {
+    char *str = NULL;
     int ret;
 
-    if ((ret = vasprintf(strp, fmt, list)) == -1)
+    ret = g_vasprintf(&str, fmt, list);
+
+    /* GLib is supposed to abort() on OOM, but a mistake meant
+     * it did not. Delete this once our min glib is at 2.64.0
+     * which includes the fix:
+     *   https://gitlab.gnome.org/GNOME/glib/merge_requests/1145
+     */
+#if !GLIB_CHECK_VERSION(2, 64, 0)
+    if (!str)
         abort();
+#endif
+    *strp = str;
 
     return ret;
 }
@@ -743,11 +755,17 @@ virAsprintfInternal(char **strp,
                     const char *fmt, ...)
 {
     va_list ap;
+    char *str = NULL;
     int ret;
 
     va_start(ap, fmt);
-    ret = virVasprintfInternal(strp, fmt, ap);
+    ret = g_vasprintf(&str, fmt, ap);
     va_end(ap);
+
+    if (!*str)
+        abort();
+    *strp = str;
+
     return ret;
 }
 
@@ -936,8 +954,7 @@ virStrdup(char **dest,
     *dest = NULL;
     if (!src)
         return 0;
-    if (!(*dest = strdup(src)))
-        abort();
+    *dest = g_strdup(src);
 
     return 1;
 }
@@ -965,8 +982,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;
 }
index f537f3472ef5099d967a5afb20fb46ebb46cd4a2..3ffe51f7b8f6543bae5e6f44e71e21b5145ad828 100644 (file)
@@ -145,6 +145,8 @@ int virVasprintfInternal(char **strp, const char *fmt, va_list list)
  * @dst: variable to hold result (char*, not char**)
  * @src: string to duplicate
  *
+ * DEPRECATED: use g_strdup instead
+ *
  * Duplicate @src string and store it into @dst.
  *
  * This macro is safe to use on arguments with side effects.
@@ -158,6 +160,8 @@ int virVasprintfInternal(char **strp, const char *fmt, va_list list)
  * @dst: variable to hold result (char*, not char**)
  * @src: string to duplicate
  *
+ * DEPRECATED: use g_strdup instead
+ *
  * Duplicate @src string and store it into @dst.
  *
  * This macro is safe to use on arguments with side effects.
@@ -172,6 +176,8 @@ int virVasprintfInternal(char **strp, const char *fmt, va_list list)
  * @src: string to duplicate
  * @n: the maximum number of bytes to copy
  *
+ * DEPRECATED: use g_strndup instead
+ *
  * Duplicate @src string and store it into @dst. If @src is longer than @n,
  * only @n bytes are copied and terminating null byte '\0' is added. If @n
  * is a negative number, then the whole @src string is copied. That is,
@@ -189,6 +195,8 @@ int virVasprintfInternal(char **strp, const char *fmt, va_list list)
  * @src: string to duplicate
  * @n: the maximum number of bytes to copy
  *
+ * DEPRECATED: use g_strndup instead
+ *
  * Duplicate @src string and store it into @dst. If @src is longer than @n,
  * only @n bytes are copied and terminating null byte '\0' is added. If @n
  * is a negative number, then the whole @src string is copied. That is,