[glib/ascii-formatd-libc-dep] Improve g_ascii_formatd docs and preconditions




commit 1acf931b62401c12f3f3d84897826b931dae5029
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jan 18 11:26:09 2022 -0500

    Improve g_ascii_formatd docs and preconditions
    
    Clarify that the format passed to g_ascii_formatd
    must be just a single format specifier for a double
    argument, and must start with '%'.
    
    Apply the precondition checks for this regardless
    which libc we are using.
    
    Related to https://gitlab.gnome.org/GNOME/pango/-/issues/659

 glib/gstrfuncs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
---
diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c
index e486251ab..e99412d61 100644
--- a/glib/gstrfuncs.c
+++ b/glib/gstrfuncs.c
@@ -927,7 +927,7 @@ g_ascii_dtostr (gchar       *buffer,
  * @buffer: A buffer to place the resulting string in
  * @buf_len: The length of the buffer.
  * @format: The printf()-style format to use for the
- *          code to use for converting.
+ *   code to use for converting
  * @d: The #gdouble to convert
  *
  * Converts a #gdouble to a string, using the '.' as
@@ -935,6 +935,9 @@ g_ascii_dtostr (gchar       *buffer,
  * a printf()-style format string. Allowed conversion
  * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
  *
+ * The @format must just be a single format specifier
+ * starting with '%', expecting a double argument.
+ *
  * The returned buffer is guaranteed to be nul-terminated.
  *
  * If you just want to want to serialize the value into a
@@ -951,6 +954,10 @@ g_ascii_formatd (gchar       *buffer,
 #ifdef USE_XLOCALE
   locale_t old_locale;
 
+  g_return_val_if_fail (buffer != NULL, NULL);
+  g_return_val_if_fail (format[0] == '%', NULL);
+  g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
+
   old_locale = uselocale (get_C_locale ());
    _g_snprintf (buffer, buf_len, format, d);
   uselocale (old_locale);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]