diff --git a/bootstrap.conf b/bootstrap.conf index 7d73584809dde98b94484b509cfd9a1889c7b34b..7e264b63ad44725db409c1b10573287a33e2d33a 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 807279631264ed5c5d694e073ac11b69bfd43662..3f1542b6dece7cf49537c07babbf214d5688aa23 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. + +
VIR_STRDUP, VIR_STRNDUP
+
Prefer the GLib APIs g_strdup and g_strndup.
+ +
virAsprintf, virVasprintf
+
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.

File handling

diff --git a/src/util/virstring.c b/src/util/virstring.c index a4cc7e9c0a553293319e7c52eb60823bfeb8f427..6b2b6ed24efd76e87faa052830e111c1c2fbc462 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -18,6 +18,7 @@ #include +#include #include #include @@ -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 f537f3472ef5099d967a5afb20fb46ebb46cd4a2..3ffe51f7b8f6543bae5e6f44e71e21b5145ad828 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,