From 48f8accaa33dfce46b63cc225a88d9421e4d2dc8 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 1 Sep 2008 21:28:44 -0600 Subject: [PATCH] vasnprintf-posix: handle large precision via %.*d * lib/vasnprintf.c (VASNPRINTF): Don't pass precision to snprintf when handling it ourselves. * tests/test-vasnprintf-posix.c (test_function): Add test. * tests/test-snprintf-posix.h (test_function): Likewise. * tests/test-sprintf-posix.h (test_function): Likewise. * tests/test-vasprintf-posix.c (test_function): Likewise. Reported by Alain Guibert. Signed-off-by: Eric Blake --- ChangeLog | 11 +++++++++++ lib/vasnprintf.c | 2 +- tests/test-snprintf-posix.h | 12 ++++++++++++ tests/test-sprintf-posix.h | 12 ++++++++++++ tests/test-vasnprintf-posix.c | 13 +++++++++++++ tests/test-vasprintf-posix.c | 13 +++++++++++++ 6 files changed, 62 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 0277d1c5f..23443eeee 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2008-09-02 Eric Blake + + vasnprintf-posix: handle large precision via %.*d + * lib/vasnprintf.c (VASNPRINTF): Don't pass precision to snprintf + when handling it ourselves. + * tests/test-vasnprintf-posix.c (test_function): Add test. + * tests/test-snprintf-posix.h (test_function): Likewise. + * tests/test-sprintf-posix.h (test_function): Likewise. + * tests/test-vasprintf-posix.c (test_function): Likewise. + Reported by Alain Guibert. + 2008-09-01 Eric Blake c-stack: make configure-time check more robust diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 89829c9a4..4ddf45fa0 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -4176,7 +4176,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, abort (); prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int; } - if (dp->precision_arg_index != ARG_NONE) + if (!prec_ourselves && dp->precision_arg_index != ARG_NONE) { if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) abort (); diff --git a/tests/test-snprintf-posix.h b/tests/test-snprintf-posix.h index 1134d80b5..43329300b 100644 --- a/tests/test-snprintf-posix.h +++ b/tests/test-snprintf-posix.h @@ -2950,6 +2950,18 @@ test_function (int (*my_snprintf) (char *, size_t, const char *, ...)) { char result[5000]; int retval = + my_snprintf (result, sizeof (result), "%.*d %d", 4000, 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (retval == strlen (result)); + } + + { + char result[5000]; + int retval = my_snprintf (result, sizeof (result), "%.4000d %d", -1234567, 99); size_t i; ASSERT (result != NULL); diff --git a/tests/test-sprintf-posix.h b/tests/test-sprintf-posix.h index 5beadca66..7767c89bc 100644 --- a/tests/test-sprintf-posix.h +++ b/tests/test-sprintf-posix.h @@ -2924,6 +2924,18 @@ test_function (int (*my_sprintf) (char *, const char *, ...)) { char result[5000]; int retval = + my_sprintf (result, "%.*d %d", 4000, 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (retval == strlen (result)); + } + + { + char result[5000]; + int retval = my_sprintf (result, "%.4000d %d", -1234567, 99); size_t i; ASSERT (result != NULL); diff --git a/tests/test-vasnprintf-posix.c b/tests/test-vasnprintf-posix.c index 42d9267cd..4c9d47e23 100644 --- a/tests/test-vasnprintf-posix.c +++ b/tests/test-vasnprintf-posix.c @@ -3457,6 +3457,19 @@ test_function (char * (*my_asnprintf) (char *, size_t *, const char *, ...)) { size_t length; char *result = + my_asnprintf (NULL, &length, "%.*d %d", 4000, 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (length == strlen (result)); + free (result); + } + + { + size_t length; + char *result = my_asnprintf (NULL, &length, "%.4000d %d", -1234567, 99); size_t i; ASSERT (result != NULL); diff --git a/tests/test-vasprintf-posix.c b/tests/test-vasprintf-posix.c index e75e15d38..135d955df 100644 --- a/tests/test-vasprintf-posix.c +++ b/tests/test-vasprintf-posix.c @@ -3438,6 +3438,19 @@ test_function (int (*my_asprintf) (char **, const char *, ...)) { char *result; int retval = + my_asprintf (&result, "%.*d %d", 4000, 1234567, 99); + size_t i; + ASSERT (result != NULL); + for (i = 0; i < 4000 - 7; i++) + ASSERT (result[i] == '0'); + ASSERT (strcmp (result + 4000 - 7, "1234567 99") == 0); + ASSERT (retval == strlen (result)); + free (result); + } + + { + char *result; + int retval = my_asprintf (&result, "%.4000d %d", -1234567, 99); size_t i; ASSERT (result != NULL); -- 2.11.0