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. |
4 | |
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. |
9 | |
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. |
14 | |
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/>. */ |
18 | |
19 | #include <stdio.h> |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | #include <wchar.h> |
23 | #include <stdint.h> |
24 | #include <locale.h> |
25 | |
26 | #include <mcheck.h> |
27 | #include <support/check.h> |
28 | #include <support/support.h> |
29 | |
30 | static int |
31 | do_test (void) |
32 | { |
33 | mtrace (); |
34 | |
35 | /* For 's' conversion specifier with 'l' modifier the array must be |
36 | converted to multibyte characters up to the precision specific |
37 | value. */ |
38 | { |
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 (n: winputsize * sizeof (wchar_t)); |
43 | wmemset (winput, L'a', winputsize - 1); |
44 | winput[winputsize - 1] = L'\0'; |
45 | |
46 | char result[9]; |
47 | const char expected[] = "aaaaaaaa" ; |
48 | int ret; |
49 | |
50 | ret = snprintf (s: result, maxlen: sizeof (result), format: "%.65537ls" , winput); |
51 | TEST_COMPARE (ret, winputsize - 1); |
52 | TEST_COMPARE_BLOB (result, sizeof (result), expected, sizeof (expected)); |
53 | |
54 | ret = snprintf (s: result, maxlen: sizeof (result), format: "%ls" , winput); |
55 | TEST_COMPARE (ret, winputsize - 1); |
56 | TEST_COMPARE_BLOB (result, sizeof (result), expected, sizeof (expected)); |
57 | |
58 | free (ptr: winput); |
59 | } |
60 | |
61 | /* For 's' converstion specifier the array is interpreted as a multibyte |
62 | character sequence and converted to wide characters up to the precision |
63 | specific value. */ |
64 | { |
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 (n: mbssize); |
69 | memset (mbs, 'a', mbssize - 1); |
70 | mbs[mbssize - 1] = '\0'; |
71 | |
72 | const size_t expectedsize = 32 * 1024; |
73 | wchar_t *expected = xmalloc (n: expectedsize * sizeof (wchar_t)); |
74 | wmemset (expected, L'a', expectedsize - 1); |
75 | expected[expectedsize-1] = L'\0'; |
76 | |
77 | const size_t resultsize = mbssize * sizeof (wchar_t); |
78 | wchar_t *result = xmalloc (n: resultsize); |
79 | int ret; |
80 | |
81 | ret = swprintf (s: result, n: resultsize, format: 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)); |
85 | |
86 | ret = swprintf (s: result, n: resultsize, format: 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)); |
90 | |
91 | /* Same test, but with an invalid multibyte sequence. */ |
92 | mbs[mbssize - 2] = 0xff; |
93 | |
94 | ret = swprintf (s: result, n: resultsize, format: L"%.65537s" , mbs); |
95 | TEST_COMPARE (ret, -1); |
96 | |
97 | ret = swprintf (s: result, n: resultsize, format: L"%1$.65537s" , mbs); |
98 | TEST_COMPARE (ret, -1); |
99 | |
100 | free (ptr: mbs); |
101 | free (ptr: result); |
102 | free (ptr: expected); |
103 | } |
104 | |
105 | return 0; |
106 | } |
107 | |
108 | #include <support/test-driver.c> |
109 | |