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@xxxxxxxxxx> Signed-off-by: Daniel P. Berrangé <berrange@xxxxxxxxxx> --- bootstrap.conf | 3 --- docs/hacking.html.in | 8 ++++++++ src/util/virstring.c | 28 ++++++++++++++++++++++------ src/util/virstring.h | 8 ++++++++ 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/bootstrap.conf b/bootstrap.conf index 7d73584809..7e264b63ad 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -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 diff --git a/docs/hacking.html.in b/docs/hacking.html.in index 8072796312..3f1542b6de 100644 --- a/docs/hacking.html.in +++ b/docs/hacking.html.in @@ -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> diff --git a/src/util/virstring.c b/src/util/virstring.c index a4cc7e9c0a..6b2b6ed24e 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -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; } diff --git a/src/util/virstring.h b/src/util/virstring.h index f537f3472e..3ffe51f7b8 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -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, -- 2.21.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list