From a99e75d0a8345b21c4132ece9b3510a38ae30e1d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 12 Dec 2001 22:05:55 +0000 Subject: [PATCH] This fixes #60543: * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). * tests/strfunc-test.c: Add some tests for g_snprintf(). --- ChangeLog | 8 +++ ChangeLog.pre-2-0 | 8 +++ ChangeLog.pre-2-10 | 8 +++ ChangeLog.pre-2-12 | 8 +++ ChangeLog.pre-2-2 | 8 +++ ChangeLog.pre-2-4 | 8 +++ ChangeLog.pre-2-6 | 8 +++ ChangeLog.pre-2-8 | 8 +++ docs/reference/ChangeLog | 4 ++ docs/reference/glib/tmpl/string_utils.sgml | 34 +++++++++-- glib/gutils.c | 71 ++++++---------------- tests/strfunc-test.c | 10 +++ 12 files changed, 123 insertions(+), 60 deletions(-) diff --git a/ChangeLog b/ChangeLog index ddf8a47f..9fb967d2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-0 +++ b/ChangeLog.pre-2-0 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-2 +++ b/ChangeLog.pre-2-2 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-4 +++ b/ChangeLog.pre-2-4 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-6 +++ b/ChangeLog.pre-2-6 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8 index ddf8a47f..9fb967d2 100644 --- a/ChangeLog.pre-2-8 +++ b/ChangeLog.pre-2-8 @@ -1,3 +1,11 @@ +2001-12-12 Matthias Clasen + + This fixes #60543: + + * glib/gutils.c (g_snprintf, g_vsnprintf): Switch to C99-[v]snprintf(). + + * tests/strfunc-test.c: Add some tests for g_snprintf(). + 2001-12-12 Matthias Clasen * glib/gconvert.c, glib/grand.c, glib/ghash.c, diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index cc38554b..191959ff 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,7 @@ +2001-12-12 Matthias Clasen + + * glib/tmpl/string_utils.sgml: Correct docs for g_[v]snprintf. + 2001-12-12 Matthias Clasen * glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml, diff --git a/docs/reference/glib/tmpl/string_utils.sgml b/docs/reference/glib/tmpl/string_utils.sgml index 1955df83..16ebaaca 100644 --- a/docs/reference/glib/tmpl/string_utils.sgml +++ b/docs/reference/glib/tmpl/string_utils.sgml @@ -173,17 +173,28 @@ See also g_strdup_printf(). In versions of GLib prior to 1.2.3, this function may return -1 if the output -was truncated, and the truncated string may not be nul-terminated. +was truncated, and the truncated string may not be nul-terminated. +In versions prior to 1.3.12, this function returns the length of the output +string. + + + + +The return value of g_snprintf() conforms to the snprintf() +function as standardized in ISO C99. Note that this is different from +traditional snprintf(), which returns the length of +the output string. @string: the buffer to hold the output. -@n: the maximum number of characters to produce (including the terminating null +@n: the maximum number of characters to produce (including the terminating nul character). -@format: the format string. See the sprintf() +@format: the format string. See the sprintf(). documentation. @Varargs: the arguments to insert in the output. -@Returns: the length of the output string. +@Returns: the number of characters which would be produced if the buffer was +large enough. @@ -200,16 +211,27 @@ See also g_strdup_vprintf(). In versions of GLib prior to 1.2.3, this function may return -1 if the output was truncated, and the truncated string may not be nul-terminated. +In versions prior to 1.3.12, this function returns the length of the output +string. + + + + +The return value of g_vsnprintf() conforms to the vsnprintf() +function as standardized in ISO C99. Note that this is different from +traditional vsnprintf(), which returns the length of +the output string. @string: the buffer to hold the output. -@n: the maximum number of characters to produce (including the terminating null +@n: the maximum number of characters to produce (including the terminating nul character). @format: the format string. See the sprintf() documentation. @args: the list of arguments to insert in the output. -@Returns: the length of the output string. +@Returns: the number of characters which would be produced if the buffer was +large enough. diff --git a/glib/gutils.c b/glib/gutils.c index 6727181b..66891a12 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -341,44 +341,14 @@ g_snprintf (gchar *str, gchar const *fmt, ...) { -#ifdef HAVE_VSNPRINTF va_list args; gint retval; - - g_return_val_if_fail (str != NULL, 0); - g_return_val_if_fail (n > 0, 0); - g_return_val_if_fail (fmt != NULL, 0); - - va_start (args, fmt); - retval = vsnprintf (str, n, fmt, args); - va_end (args); - - if (retval < 0) - { - str[n-1] = '\0'; - retval = strlen (str); - } - - return retval; -#else /* !HAVE_VSNPRINTF */ - gchar *printed; - va_list args; - - g_return_val_if_fail (str != NULL, 0); - g_return_val_if_fail (n > 0, 0); - g_return_val_if_fail (fmt != NULL, 0); va_start (args, fmt); - printed = g_strdup_vprintf (fmt, args); + retval = g_vsnprintf (str, n, fmt, args); va_end (args); - strncpy (str, printed, n); - str[n-1] = '\0'; - - g_free (printed); - - return strlen (str); -#endif /* !HAVE_VSNPRINTF */ + return retval; } gint @@ -387,37 +357,30 @@ g_vsnprintf (gchar *str, gchar const *fmt, va_list args) { -#ifdef HAVE_VSNPRINTF - gint retval; - - g_return_val_if_fail (str != NULL, 0); - g_return_val_if_fail (n > 0, 0); +#ifdef HAVE_VSNPRINTF_C99 + g_return_val_if_fail (n == 0 || str != NULL, 0); g_return_val_if_fail (fmt != NULL, 0); - retval = vsnprintf (str, n, fmt, args); - - if (retval < 0) - { - str[n-1] = '\0'; - retval = strlen (str); - } - - return retval; -#else /* !HAVE_VSNPRINTF */ + return vsnprintf (str, n, fmt, args); +#else /* !HAVE_VSNPRINTF_C99 */ gchar *printed; - - g_return_val_if_fail (str != NULL, 0); - g_return_val_if_fail (n > 0, 0); + gint retval; + + g_return_val_if_fail (n == 0 || str != NULL, 0); g_return_val_if_fail (fmt != NULL, 0); printed = g_strdup_vprintf (fmt, args); - strncpy (str, printed, n); - str[n-1] = '\0'; + retval = strlen (printed); + if (n > 0) + { + strncpy (str, printed, n - 1); + str[n-1] = '\0'; + } g_free (printed); - return strlen (str); -#endif /* !HAVE_VSNPRINTF */ + return retval; +#endif /* !HAVE_VSNPRINTF_C99 */ } guint diff --git a/tests/strfunc-test.c b/tests/strfunc-test.c index 0c7b5c21..73eb86a8 100644 --- a/tests/strfunc-test.c +++ b/tests/strfunc-test.c @@ -383,5 +383,15 @@ main (int argc, #undef S + { + gchar buf[5]; + + TEST (NULL, 3 == g_snprintf (buf, 0, "%s", "abc")); + TEST (NULL, 3 == g_snprintf (NULL,0, "%s", "abc")); + TEST (NULL, 3 == g_snprintf (buf, 5, "%s", "abc")); + TEST (NULL, 4 == g_snprintf (buf, 5, "%s", "abcd")); + TEST (NULL, 9 == g_snprintf (buf, 5, "%s", "abcdefghi")); + } + return any_failed; } -- 2.34.1