mirror of https://gitee.com/openkylin/libvirt.git
util: use glib string allocation/formatting functions
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>
This commit is contained in:
parent
e85e34f3af
commit
bb9a1a14e2
|
@ -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
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue