Merge branch 'str-printf-abort-oom' into 'master'

Ensure that g_vasprintf will always abort on OOM and some docs fixes

Closes #1622

See merge request GNOME/glib!1145
This commit is contained in:
Philip Withnall 2019-10-07 17:21:32 +00:00
commit b229eed0e4
3 changed files with 31 additions and 9 deletions

View File

@ -20,6 +20,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include "gprintf.h" #include "gprintf.h"
#include "gprintfint.h" #include "gprintfint.h"
@ -294,8 +295,8 @@ g_vsnprintf (gchar *string,
/** /**
* g_vasprintf: * g_vasprintf:
* @string: the return location for the newly-allocated string. * @string: (not optional) (nullable): the return location for the newly-allocated string.
* @format: a standard printf() format string, but notice * @format: (not nullable): a standard printf() format string, but notice
* [string precision pitfalls][string-precision] * [string precision pitfalls][string-precision]
* @args: the list of arguments to insert in the output. * @args: the list of arguments to insert in the output.
* *
@ -305,6 +306,10 @@ g_vsnprintf (gchar *string,
* string to hold the output, instead of putting the output in a buffer * string to hold the output, instead of putting the output in a buffer
* you allocate in advance. * you allocate in advance.
* *
* The returned value in @string is guaranteed to be non-NULL, unless
* @format contains `%lc` or `%ls` conversions, which can fail if no
* multibyte representation is available for the given character.
*
* `glib/gprintf.h` must be explicitly included in order to use this function. * `glib/gprintf.h` must be explicitly included in order to use this function.
* *
* Returns: the number of bytes printed. * Returns: the number of bytes printed.
@ -327,9 +332,18 @@ g_vasprintf (gchar **string,
#elif defined (HAVE_VASPRINTF) #elif defined (HAVE_VASPRINTF)
len = vasprintf (string, format, args); {
if (len < 0) int saved_errno;
*string = NULL; len = vasprintf (string, format, args);
saved_errno = errno;
if (len < 0)
{
if (saved_errno == ENOMEM)
g_error ("%s: failed to allocate memory", G_STRLOC);
else
*string = NULL;
}
}
#else #else

View File

@ -491,7 +491,7 @@ g_stpcpy (gchar *dest,
/** /**
* g_strdup_vprintf: * g_strdup_vprintf:
* @format: a standard printf() format string, but notice * @format: (not nullable): a standard printf() format string, but notice
* [string precision pitfalls][string-precision] * [string precision pitfalls][string-precision]
* @args: the list of parameters to insert into the format string * @args: the list of parameters to insert into the format string
* *
@ -500,6 +500,10 @@ g_stpcpy (gchar *dest,
* the result. The returned string should be freed with g_free() when * the result. The returned string should be freed with g_free() when
* no longer needed. * no longer needed.
* *
* The returned string is guaranteed to be non-NULL, unless @format
* contains `%lc` or `%ls` conversions, which can fail if no multibyte
* representation is available for the given character.
*
* See also g_vasprintf(), which offers the same functionality, but * See also g_vasprintf(), which offers the same functionality, but
* additionally returns the length of the allocated string. * additionally returns the length of the allocated string.
* *
@ -518,7 +522,7 @@ g_strdup_vprintf (const gchar *format,
/** /**
* g_strdup_printf: * g_strdup_printf:
* @format: a standard printf() format string, but notice * @format: (not nullable): a standard printf() format string, but notice
* [string precision pitfalls][string-precision] * [string precision pitfalls][string-precision]
* @...: the parameters to insert into the format string * @...: the parameters to insert into the format string
* *
@ -527,6 +531,10 @@ g_strdup_vprintf (const gchar *format,
* the result. The returned string should be freed with g_free() when no * the result. The returned string should be freed with g_free() when no
* longer needed. * longer needed.
* *
* The returned string is guaranteed to be non-NULL, unless @format
* contains `%lc` or `%ls` conversions, which can fail if no multibyte
* representation is available for the given character.
*
* Returns: a newly-allocated string holding the result * Returns: a newly-allocated string holding the result
*/ */
gchar* gchar*

View File

@ -1144,7 +1144,7 @@ g_string_up (GString *string)
/** /**
* g_string_append_vprintf: * g_string_append_vprintf:
* @string: a #GString * @string: a #GString
* @format: the string format. See the printf() documentation * @format: (not nullable): the string format. See the printf() documentation
* @args: the list of arguments to insert in the output * @args: the list of arguments to insert in the output
* *
* Appends a formatted string onto the end of a #GString. * Appends a formatted string onto the end of a #GString.
@ -1179,7 +1179,7 @@ g_string_append_vprintf (GString *string,
/** /**
* g_string_vprintf: * g_string_vprintf:
* @string: a #GString * @string: a #GString
* @format: the string format. See the printf() documentation * @format: (not nullable): the string format. See the printf() documentation
* @args: the parameters to insert into the format string * @args: the parameters to insert into the format string
* *
* Writes a formatted string into a #GString. * Writes a formatted string into a #GString.