vasnprintf-posix: handle large precision via %.*d
authorEric Blake <ebb9@byu.net>
Tue, 2 Sep 2008 03:28:44 +0000 (21:28 -0600)
committerEric Blake <ebb9@byu.net>
Tue, 2 Sep 2008 12:14:08 +0000 (06:14 -0600)
* 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 <ebb9@byu.net>
ChangeLog
lib/vasnprintf.c
tests/test-snprintf-posix.h
tests/test-sprintf-posix.h
tests/test-vasnprintf-posix.c
tests/test-vasprintf-posix.c

index 0277d1c..23443ee 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-09-02  Eric Blake  <ebb9@byu.net>
+
+       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  <ebb9@byu.net>
 
        c-stack: make configure-time check more robust
index 89829c9..4ddf45f 100644 (file)
@@ -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 ();
index 1134d80..4332930 100644 (file)
@@ -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);
index 5beadca..7767c89 100644 (file)
@@ -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);
index 42d9267..4c9d47e 100644 (file)
@@ -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);
index e75e15d..135d955 100644 (file)
@@ -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);