1 | /* Test for the long double variants of *printf_chk functions. |
2 | Copyright (C) 2019-2024 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 | #define _FORTIFY_SOURCE 2 |
20 | |
21 | #include <stdarg.h> |
22 | #include <stdint.h> |
23 | #include <stdio.h> |
24 | #include <stdlib.h> |
25 | |
26 | #include <support/capture_subprocess.h> |
27 | #include <support/check.h> |
28 | |
29 | static void |
30 | do_test_call_varg (FILE *stream, const char *format, ...) |
31 | { |
32 | char *buffer = NULL; |
33 | char string[128]; |
34 | int res; |
35 | va_list args; |
36 | |
37 | printf ("%20s" , "__vasprintf_chk: " ); |
38 | va_start (args, format); |
39 | res = __vasprintf_chk (&buffer, 1, format, args); |
40 | va_end (args); |
41 | if (res == -1) |
42 | printf ("Error using vasprintf\n" ); |
43 | if (buffer == NULL) |
44 | printf ("Error using vasprintf\n" ); |
45 | else |
46 | { |
47 | printf ("%s" , buffer); |
48 | free (ptr: buffer); |
49 | } |
50 | printf ("\n" ); |
51 | |
52 | printf ("%20s" , "__vdprintf_chk: " ); |
53 | va_start (args, format); |
54 | __vdprintf_chk (fileno (stream: stream), 1, format, args); |
55 | va_end (args); |
56 | printf ("\n" ); |
57 | |
58 | printf ("%20s" , "__vfprintf_chk: " ); |
59 | va_start (args, format); |
60 | __vfprintf_chk (stream, 1, format, args); |
61 | va_end (args); |
62 | printf ("\n" ); |
63 | |
64 | printf ("%20s" , "__vprintf_chk: " ); |
65 | va_start (args, format); |
66 | __vprintf_chk (1, format, args); |
67 | va_end (args); |
68 | printf ("\n" ); |
69 | |
70 | printf ("%20s" , "__vsnprintf_chk: " ); |
71 | va_start (args, format); |
72 | __vsnprintf_chk (string, 79, 1, 127, format, args); |
73 | va_end (args); |
74 | printf ("%s" , string); |
75 | printf ("\n" ); |
76 | |
77 | printf ("%20s" , "__vsprintf_chk: " ); |
78 | va_start (args, format); |
79 | __vsprintf_chk (string, 1, 127, format, args); |
80 | va_end (args); |
81 | printf ("%s" , string); |
82 | printf ("\n" ); |
83 | } |
84 | |
85 | static void |
86 | do_test_call_rarg (FILE *stream, const char *format, long double ld, |
87 | double d) |
88 | { |
89 | char *buffer = NULL; |
90 | char string[128]; |
91 | int res; |
92 | |
93 | printf ("%20s" , "__asprintf_chk: " ); |
94 | res = __asprintf_chk (&buffer, 1, format, ld, d); |
95 | if (res == -1) |
96 | printf ("Error using vasprintf\n" ); |
97 | if (buffer == NULL) |
98 | printf ("Error using asprintf\n" ); |
99 | else |
100 | { |
101 | printf ("%s" , buffer); |
102 | free (ptr: buffer); |
103 | } |
104 | printf ("\n" ); |
105 | |
106 | printf ("%20s" , "__dprintf_chk: " ); |
107 | __dprintf_chk (fileno (stream: stream), 1, format, ld, d); |
108 | printf ("\n" ); |
109 | |
110 | printf ("%20s" , "__fprintf_chk: " ); |
111 | __fprintf_chk (stdout, 1, format, ld, d); |
112 | printf ("\n" ); |
113 | |
114 | printf ("%20s" , "__printf_chk: " ); |
115 | __printf_chk (1, format, ld, d); |
116 | printf ("\n" ); |
117 | |
118 | printf ("%20s" , "__snprintf_chk: " ); |
119 | __snprintf_chk (string, 79, 1, 127, format, ld, d); |
120 | printf ("%s" , string); |
121 | printf ("\n" ); |
122 | |
123 | printf ("%20s" , "__sprintf_chk: " ); |
124 | __sprintf_chk (string, 1, 127, format, ld, d); |
125 | printf ("%s" , string); |
126 | printf ("\n" ); |
127 | } |
128 | |
129 | static void |
130 | do_test_call (void) |
131 | { |
132 | long double ld = -1; |
133 | double d = -1; |
134 | |
135 | /* Print in decimal notation. */ |
136 | do_test_call_rarg (stdout, format: "%.10Lf, %.10f" , ld, d); |
137 | do_test_call_varg (stdout, format: "%.10Lf, %.10f" , ld, d); |
138 | |
139 | /* Print in hexadecimal notation. */ |
140 | do_test_call_rarg (stdout, format: "%.10La, %.10a" , ld, d); |
141 | do_test_call_varg (stdout, format: "%.10La, %.10a" , ld, d); |
142 | |
143 | /* Test positional parameters. */ |
144 | do_test_call_varg (stdout, format: "%3$Lf, %2$Lf, %1$f" , |
145 | (double) 1, (long double) 2, (long double) 3); |
146 | } |
147 | |
148 | static int |
149 | do_test (void) |
150 | { |
151 | struct support_capture_subprocess result; |
152 | result = support_capture_subprocess (callback: (void *) &do_test_call, NULL); |
153 | |
154 | /* Compare against the expected output. */ |
155 | const char *expected = |
156 | " __asprintf_chk: -1.0000000000, -1.0000000000\n" |
157 | " __dprintf_chk: -1.0000000000, -1.0000000000\n" |
158 | " __fprintf_chk: -1.0000000000, -1.0000000000\n" |
159 | " __printf_chk: -1.0000000000, -1.0000000000\n" |
160 | " __snprintf_chk: -1.0000000000, -1.0000000000\n" |
161 | " __sprintf_chk: -1.0000000000, -1.0000000000\n" |
162 | " __vasprintf_chk: -1.0000000000, -1.0000000000\n" |
163 | " __vdprintf_chk: -1.0000000000, -1.0000000000\n" |
164 | " __vfprintf_chk: -1.0000000000, -1.0000000000\n" |
165 | " __vprintf_chk: -1.0000000000, -1.0000000000\n" |
166 | " __vsnprintf_chk: -1.0000000000, -1.0000000000\n" |
167 | " __vsprintf_chk: -1.0000000000, -1.0000000000\n" |
168 | " __asprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
169 | " __dprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
170 | " __fprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
171 | " __printf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
172 | " __snprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
173 | " __sprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
174 | " __vasprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
175 | " __vdprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
176 | " __vfprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
177 | " __vprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
178 | " __vsnprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
179 | " __vsprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n" |
180 | " __vasprintf_chk: 3.000000, 2.000000, 1.000000\n" |
181 | " __vdprintf_chk: 3.000000, 2.000000, 1.000000\n" |
182 | " __vfprintf_chk: 3.000000, 2.000000, 1.000000\n" |
183 | " __vprintf_chk: 3.000000, 2.000000, 1.000000\n" |
184 | " __vsnprintf_chk: 3.000000, 2.000000, 1.000000\n" |
185 | " __vsprintf_chk: 3.000000, 2.000000, 1.000000\n" ; |
186 | TEST_COMPARE_STRING (expected, result.out.buffer); |
187 | |
188 | return 0; |
189 | } |
190 | |
191 | #include <support/test-driver.c> |
192 | |