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>
socket
stat-time
strchrnul
-strdup-posix
-strndup
strerror
strerror_r-posix
strptime
uname
unsetenv
usleep
-vasprintf
verify
vsnprintf
waitpid
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>
#include <config.h>
+#include <glib/gprintf.h>
#include <regex.h>
#include <locale.h>
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;
}
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;
}
*dest = NULL;
if (!src)
return 0;
- if (!(*dest = strdup(src)))
- abort();
+ *dest = g_strdup(src);
return 1;
}
return 0;
if (n < 0)
n = strlen(src);
- if (!(*dest = strndup(src, n)))
- abort();
+ *dest = g_strndup(src, n);
return 1;
}
* @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.
* @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.
* @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,
* @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,