diff mbox series

[05/21] stdio-common: tests: Incorrect maxlen parameter for swprintf

Message ID 20230620181910.1506893-6-fberat@redhat.com
State New
Headers show
Series Allow glibc to be built with _FORTIFY_SOURCE | expand

Commit Message

Frederic Berat June 20, 2023, 6:18 p.m. UTC
Few tests using swprintf are passing incorrect maxlen parameter.
This triggers an abort when _FORTIFY_SOURCE is enabled.
---
 stdio-common/tst-printf-bz25691.c    |  8 ++++----
 stdio-common/tst-vfprintf-mbs-prec.c | 15 ++++++++-------
 2 files changed, 12 insertions(+), 11 deletions(-)

Comments

Siddhesh Poyarekar June 21, 2023, 12:29 p.m. UTC | #1
On 2023-06-20 14:18, Frédéric Bérat wrote:
> Few tests using swprintf are passing incorrect maxlen parameter.
> This triggers an abort when _FORTIFY_SOURCE is enabled.
> ---

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

>   stdio-common/tst-printf-bz25691.c    |  8 ++++----
>   stdio-common/tst-vfprintf-mbs-prec.c | 15 ++++++++-------
>   2 files changed, 12 insertions(+), 11 deletions(-)
> 
> diff --git a/stdio-common/tst-printf-bz25691.c b/stdio-common/tst-printf-bz25691.c
> index db81fdf590..44e9ea7d9d 100644
> --- a/stdio-common/tst-printf-bz25691.c
> +++ b/stdio-common/tst-printf-bz25691.c
> @@ -78,12 +78,12 @@ do_test (void)
>       wchar_t *result = xmalloc (resultsize);
>       int ret;
>   
> -    ret = swprintf (result, resultsize, L"%.65537s", mbs);
> +    ret = swprintf (result, mbssize, L"%.65537s", mbs);
>       TEST_COMPARE (ret, mbssize - 1);
>       TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t),
>   		       expected, expectedsize * sizeof (wchar_t));
>   
> -    ret = swprintf (result, resultsize, L"%1$.65537s", mbs);
> +    ret = swprintf (result, mbssize, L"%1$.65537s", mbs);
>       TEST_COMPARE (ret, mbssize - 1);
>       TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t),
>   		       expected, expectedsize * sizeof (wchar_t));
> @@ -91,10 +91,10 @@ do_test (void)
>       /* Same test, but with an invalid multibyte sequence.  */
>       mbs[mbssize - 2] = 0xff;
>   
> -    ret = swprintf (result, resultsize, L"%.65537s", mbs);
> +    ret = swprintf (result, mbssize, L"%.65537s", mbs);
>       TEST_COMPARE (ret, -1);
>   
> -    ret = swprintf (result, resultsize, L"%1$.65537s", mbs);
> +    ret = swprintf (result, mbssize, L"%1$.65537s", mbs);
>       TEST_COMPARE (ret, -1);
>   
>       free (mbs);
> diff --git a/stdio-common/tst-vfprintf-mbs-prec.c b/stdio-common/tst-vfprintf-mbs-prec.c
> index 38350ed60f..41c4139f21 100644
> --- a/stdio-common/tst-vfprintf-mbs-prec.c
> +++ b/stdio-common/tst-vfprintf-mbs-prec.c
> @@ -441,7 +441,8 @@ test_mbs_long (const char *mbs, const wchar_t *wide, const size_t *length)
>   static void
>   test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
>   {
> -  wchar_t buf[2000];
> +#define BUF_SIZE 2000
> +  wchar_t buf[BUF_SIZE];
>     _Static_assert (sizeof (buf) > sizeof (wchar_t) * WIDE_STRING_LENGTH,
>                     "buffer size consistent with string length");
>     const wchar_t *suffix = L"||TERM";
> @@ -450,13 +451,13 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
>   
>     /* Test formatting of the entire string.  */
>     {
> -    int ret = swprintf (buf, sizeof (buf), L"%s%ls", mbs, suffix);
> +    int ret = swprintf (buf, BUF_SIZE, L"%s%ls", mbs, suffix);
>       TEST_VERIFY (ret == WIDE_STRING_LENGTH + wcslen (suffix));
>       TEST_VERIFY (wmemcmp (buf, wide, WIDE_STRING_LENGTH) == 0);
>       TEST_VERIFY (wcscmp (buf + WIDE_STRING_LENGTH, suffix) == 0);
>   
>       /* Left-justified string, printed in full.  */
> -    ret = swprintf (buf, sizeof (buf), L"%-1500s%ls", mbs, suffix);
> +    ret = swprintf (buf, BUF_SIZE, L"%-1500s%ls", mbs, suffix);
>       TEST_VERIFY (ret == 1500 + wcslen (suffix));
>       TEST_VERIFY (wmemcmp (buf, wide, WIDE_STRING_LENGTH) == 0);
>       for (size_t i = WIDE_STRING_LENGTH; i < 1500; ++i)
> @@ -464,7 +465,7 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
>       TEST_VERIFY (wcscmp (buf + 1500, suffix) == 0);
>   
>       /* Right-justified string, printed in full.   */
> -    ret = swprintf (buf, sizeof (buf), L"%1500s%ls", mbs, suffix);
> +    ret = swprintf (buf, BUF_SIZE, L"%1500s%ls", mbs, suffix);
>       TEST_VERIFY (ret == 1500 + wcslen (suffix));
>       size_t padding = 1500 - WIDE_STRING_LENGTH;
>       for (size_t i = 0; i < padding; ++i)
> @@ -484,14 +485,14 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
>           printf ("info: %s: wide_len=%d actual_wide_len=%zu\n",
>                   __func__, wide_len, actual_wide_len);
>   
> -      int ret = swprintf (buf, sizeof (buf), L"%.*s%ls",
> +      int ret = swprintf (buf, BUF_SIZE, L"%.*s%ls",
>                             wide_len, mbs, suffix);
>         TEST_VERIFY (ret == actual_wide_len + wcslen (suffix));
>         TEST_VERIFY (wmemcmp (buf, wide, actual_wide_len) == 0);
>         TEST_VERIFY (wcscmp (buf + actual_wide_len, suffix) == 0);
>   
>         /* Left-justified string, printed in full.  */
> -      ret = swprintf (buf, sizeof (buf), L"%-1500.*s%ls",
> +      ret = swprintf (buf, BUF_SIZE, L"%-1500.*s%ls",
>                         wide_len, mbs, suffix);
>         TEST_VERIFY (ret == 1500 + wcslen (suffix));
>         TEST_VERIFY (wmemcmp (buf, wide, actual_wide_len) == 0);
> @@ -500,7 +501,7 @@ test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
>         TEST_VERIFY (wcscmp (buf + 1500, suffix) == 0);
>   
>         /* Right-justified string, printed in full.   */
> -      ret = swprintf (buf, sizeof (buf), L"%1500.*s%ls",
> +      ret = swprintf (buf, BUF_SIZE, L"%1500.*s%ls",
>                         wide_len, mbs, suffix);
>         TEST_VERIFY (ret == 1500 + wcslen (suffix));
>         size_t padding = 1500 - actual_wide_len;
diff mbox series

Patch

diff --git a/stdio-common/tst-printf-bz25691.c b/stdio-common/tst-printf-bz25691.c
index db81fdf590..44e9ea7d9d 100644
--- a/stdio-common/tst-printf-bz25691.c
+++ b/stdio-common/tst-printf-bz25691.c
@@ -78,12 +78,12 @@  do_test (void)
     wchar_t *result = xmalloc (resultsize);
     int ret;
 
-    ret = swprintf (result, resultsize, L"%.65537s", mbs);
+    ret = swprintf (result, mbssize, L"%.65537s", mbs);
     TEST_COMPARE (ret, mbssize - 1);
     TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t),
 		       expected, expectedsize * sizeof (wchar_t));
 
-    ret = swprintf (result, resultsize, L"%1$.65537s", mbs);
+    ret = swprintf (result, mbssize, L"%1$.65537s", mbs);
     TEST_COMPARE (ret, mbssize - 1);
     TEST_COMPARE_BLOB (result, (ret + 1) * sizeof (wchar_t),
 		       expected, expectedsize * sizeof (wchar_t));
@@ -91,10 +91,10 @@  do_test (void)
     /* Same test, but with an invalid multibyte sequence.  */
     mbs[mbssize - 2] = 0xff;
 
-    ret = swprintf (result, resultsize, L"%.65537s", mbs);
+    ret = swprintf (result, mbssize, L"%.65537s", mbs);
     TEST_COMPARE (ret, -1);
 
-    ret = swprintf (result, resultsize, L"%1$.65537s", mbs);
+    ret = swprintf (result, mbssize, L"%1$.65537s", mbs);
     TEST_COMPARE (ret, -1);
 
     free (mbs);
diff --git a/stdio-common/tst-vfprintf-mbs-prec.c b/stdio-common/tst-vfprintf-mbs-prec.c
index 38350ed60f..41c4139f21 100644
--- a/stdio-common/tst-vfprintf-mbs-prec.c
+++ b/stdio-common/tst-vfprintf-mbs-prec.c
@@ -441,7 +441,8 @@  test_mbs_long (const char *mbs, const wchar_t *wide, const size_t *length)
 static void
 test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
 {
-  wchar_t buf[2000];
+#define BUF_SIZE 2000
+  wchar_t buf[BUF_SIZE];
   _Static_assert (sizeof (buf) > sizeof (wchar_t) * WIDE_STRING_LENGTH,
                   "buffer size consistent with string length");
   const wchar_t *suffix = L"||TERM";
@@ -450,13 +451,13 @@  test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
 
   /* Test formatting of the entire string.  */
   {
-    int ret = swprintf (buf, sizeof (buf), L"%s%ls", mbs, suffix);
+    int ret = swprintf (buf, BUF_SIZE, L"%s%ls", mbs, suffix);
     TEST_VERIFY (ret == WIDE_STRING_LENGTH + wcslen (suffix));
     TEST_VERIFY (wmemcmp (buf, wide, WIDE_STRING_LENGTH) == 0);
     TEST_VERIFY (wcscmp (buf + WIDE_STRING_LENGTH, suffix) == 0);
 
     /* Left-justified string, printed in full.  */
-    ret = swprintf (buf, sizeof (buf), L"%-1500s%ls", mbs, suffix);
+    ret = swprintf (buf, BUF_SIZE, L"%-1500s%ls", mbs, suffix);
     TEST_VERIFY (ret == 1500 + wcslen (suffix));
     TEST_VERIFY (wmemcmp (buf, wide, WIDE_STRING_LENGTH) == 0);
     for (size_t i = WIDE_STRING_LENGTH; i < 1500; ++i)
@@ -464,7 +465,7 @@  test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
     TEST_VERIFY (wcscmp (buf + 1500, suffix) == 0);
 
     /* Right-justified string, printed in full.   */
-    ret = swprintf (buf, sizeof (buf), L"%1500s%ls", mbs, suffix);
+    ret = swprintf (buf, BUF_SIZE, L"%1500s%ls", mbs, suffix);
     TEST_VERIFY (ret == 1500 + wcslen (suffix));
     size_t padding = 1500 - WIDE_STRING_LENGTH;
     for (size_t i = 0; i < padding; ++i)
@@ -484,14 +485,14 @@  test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
         printf ("info: %s: wide_len=%d actual_wide_len=%zu\n",
                 __func__, wide_len, actual_wide_len);
 
-      int ret = swprintf (buf, sizeof (buf), L"%.*s%ls",
+      int ret = swprintf (buf, BUF_SIZE, L"%.*s%ls",
                           wide_len, mbs, suffix);
       TEST_VERIFY (ret == actual_wide_len + wcslen (suffix));
       TEST_VERIFY (wmemcmp (buf, wide, actual_wide_len) == 0);
       TEST_VERIFY (wcscmp (buf + actual_wide_len, suffix) == 0);
 
       /* Left-justified string, printed in full.  */
-      ret = swprintf (buf, sizeof (buf), L"%-1500.*s%ls",
+      ret = swprintf (buf, BUF_SIZE, L"%-1500.*s%ls",
                       wide_len, mbs, suffix);
       TEST_VERIFY (ret == 1500 + wcslen (suffix));
       TEST_VERIFY (wmemcmp (buf, wide, actual_wide_len) == 0);
@@ -500,7 +501,7 @@  test_wide_long (const char *mbs, const wchar_t *wide, const size_t *length)
       TEST_VERIFY (wcscmp (buf + 1500, suffix) == 0);
 
       /* Right-justified string, printed in full.   */
-      ret = swprintf (buf, sizeof (buf), L"%1500.*s%ls",
+      ret = swprintf (buf, BUF_SIZE, L"%1500.*s%ls",
                       wide_len, mbs, suffix);
       TEST_VERIFY (ret == 1500 + wcslen (suffix));
       size_t padding = 1500 - actual_wide_len;