1 /* Test for memory leak with large width (BZ#25691).
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
27 #include <support/check.h>
28 #include <support/support.h>
35 /* For 's' conversion specifier with 'l' modifier the array must be
36 converted to multibyte characters up to the precision specific
39 /* The input size value is to force a heap allocation on temporary
40 buffer (in the old implementation). */
41 const size_t winputsize
= 64 * 1024 + 1;
42 wchar_t *winput
= xmalloc (winputsize
* sizeof (wchar_t));
43 wmemset (winput
, L
'a', winputsize
- 1);
44 winput
[winputsize
- 1] = L
'\0';
47 const char expected
[] = "aaaaaaaa";
50 ret
= snprintf (result
, sizeof (result
), "%.65537ls", winput
);
51 TEST_COMPARE (ret
, winputsize
- 1);
52 TEST_COMPARE_BLOB (result
, sizeof (result
), expected
, sizeof (expected
));
54 ret
= snprintf (result
, sizeof (result
), "%ls", winput
);
55 TEST_COMPARE (ret
, winputsize
- 1);
56 TEST_COMPARE_BLOB (result
, sizeof (result
), expected
, sizeof (expected
));
61 /* For 's' converstion specifier the array is interpreted as a multibyte
62 character sequence and converted to wide characters up to the precision
65 /* The input size value is to force a heap allocation on temporary
66 buffer (in the old implementation). */
67 const size_t mbssize
= 32 * 1024;
68 char *mbs
= xmalloc (mbssize
);
69 memset (mbs
, 'a', mbssize
- 1);
70 mbs
[mbssize
- 1] = '\0';
72 const size_t expectedsize
= 32 * 1024;
73 wchar_t *expected
= xmalloc (expectedsize
* sizeof (wchar_t));
74 wmemset (expected
, L
'a', expectedsize
- 1);
75 expected
[expectedsize
-1] = L
'\0';
77 const size_t resultsize
= mbssize
* sizeof (wchar_t);
78 wchar_t *result
= xmalloc (resultsize
);
81 ret
= swprintf (result
, resultsize
, L
"%.65537s", mbs
);
82 TEST_COMPARE (ret
, mbssize
- 1);
83 TEST_COMPARE_BLOB (result
, (ret
+ 1) * sizeof (wchar_t),
84 expected
, expectedsize
* sizeof (wchar_t));
86 ret
= swprintf (result
, resultsize
, L
"%1$.65537s", mbs
);
87 TEST_COMPARE (ret
, mbssize
- 1);
88 TEST_COMPARE_BLOB (result
, (ret
+ 1) * sizeof (wchar_t),
89 expected
, expectedsize
* sizeof (wchar_t));
91 /* Same test, but with an invalid multibyte sequence. */
92 mbs
[mbssize
- 2] = 0xff;
94 ret
= swprintf (result
, resultsize
, L
"%.65537s", mbs
);
95 TEST_COMPARE (ret
, -1);
97 ret
= swprintf (result
, resultsize
, L
"%1$.65537s", mbs
);
98 TEST_COMPARE (ret
, -1);
108 #include <support/test-driver.c>