]> git.ipfire.org Git - thirdparty/glibc.git/blob - stdio-common/tst-printf.c
elf: Refuse to dlopen PIE objects [BZ #24323]
[thirdparty/glibc.git] / stdio-common / tst-printf.c
1 /* Copyright (C) 1991-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #ifdef BSD
19 #include </usr/include/stdio.h>
20 #define EXIT_SUCCESS 0
21 #else
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #endif
27
28 #include <float.h>
29 #include <libc-diag.h>
30
31 /* This whole file is picayune tests of corner cases of printf format strings.
32 The compiler warnings are not useful here. */
33 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat");
34
35 #if __GNUC_PREREQ (7, 0)
36 /* Compiler warnings about snprintf output truncation should also be
37 ignored. */
38 DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-truncation");
39 #endif
40
41 static void rfg1 (void);
42 static void rfg2 (void);
43 static void rfg3 (void);
44
45
46 static void
47 fmtchk (const char *fmt)
48 {
49 (void) fputs(fmt, stdout);
50 (void) printf(":\t`");
51 (void) printf(fmt, 0x12);
52 (void) printf("'\n");
53 }
54
55 static void
56 fmtst1chk (const char *fmt)
57 {
58 (void) fputs(fmt, stdout);
59 (void) printf(":\t`");
60 (void) printf(fmt, 4, 0x12);
61 (void) printf("'\n");
62 }
63
64 static void
65 fmtst2chk (const char *fmt)
66 {
67 (void) fputs(fmt, stdout);
68 (void) printf(":\t`");
69 (void) printf(fmt, 4, 4, 0x12);
70 (void) printf("'\n");
71 }
72
73 static int
74 do_test (void)
75 {
76 static char shortstr[] = "Hi, Z.";
77 static char longstr[] = "Good morning, Doctor Chandra. This is Hal. \
78 I am ready for my first lesson today.";
79 int result = 0;
80
81 fmtchk("%.4x");
82 fmtchk("%04x");
83 fmtchk("%4.4x");
84 fmtchk("%04.4x");
85 fmtchk("%4.3x");
86 fmtchk("%04.3x");
87
88 fmtst1chk("%.*x");
89 fmtst1chk("%0*x");
90 fmtst2chk("%*.*x");
91 fmtst2chk("%0*.*x");
92
93 #ifndef BSD
94 printf("bad format:\t\"%b\"\n");
95 printf("nil pointer (padded):\t\"%10p\"\n", (void *) NULL);
96 #endif
97
98 printf("decimal negative:\t\"%d\"\n", -2345);
99 printf("octal negative:\t\"%o\"\n", -2345);
100 printf("hex negative:\t\"%x\"\n", -2345);
101 printf("long decimal number:\t\"%ld\"\n", -123456L);
102 printf("long octal negative:\t\"%lo\"\n", -2345L);
103 printf("long unsigned decimal number:\t\"%lu\"\n", -123456L);
104 printf("zero-padded LDN:\t\"%010ld\"\n", -123456L);
105 printf("left-adjusted ZLDN:\t\"%-010ld\"\n", -123456L);
106 printf("space-padded LDN:\t\"%10ld\"\n", -123456L);
107 printf("left-adjusted SLDN:\t\"%-10ld\"\n", -123456L);
108
109 printf("zero-padded string:\t\"%010s\"\n", shortstr);
110 printf("left-adjusted Z string:\t\"%-010s\"\n", shortstr);
111 printf("space-padded string:\t\"%10s\"\n", shortstr);
112 printf("left-adjusted S string:\t\"%-10s\"\n", shortstr);
113 printf("null string:\t\"%s\"\n", (char *)NULL);
114 printf("limited string:\t\"%.22s\"\n", longstr);
115
116 printf("a-style max:\t\"%a\"\n", DBL_MAX);
117 printf("a-style -max:\t\"%a\"\n", -DBL_MAX);
118 printf("e-style >= 1:\t\"%e\"\n", 12.34);
119 printf("e-style >= .1:\t\"%e\"\n", 0.1234);
120 printf("e-style < .1:\t\"%e\"\n", 0.001234);
121 printf("e-style big:\t\"%.60e\"\n", 1e20);
122 printf ("e-style == .1:\t\"%e\"\n", 0.1);
123 printf("f-style >= 1:\t\"%f\"\n", 12.34);
124 printf("f-style >= .1:\t\"%f\"\n", 0.1234);
125 printf("f-style < .1:\t\"%f\"\n", 0.001234);
126 printf("g-style >= 1:\t\"%g\"\n", 12.34);
127 printf("g-style >= .1:\t\"%g\"\n", 0.1234);
128 printf("g-style < .1:\t\"%g\"\n", 0.001234);
129 printf("g-style big:\t\"%.60g\"\n", 1e20);
130
131 printf (" %6.5f\n", .099999999860301614);
132 printf (" %6.5f\n", .1);
133 printf ("x%5.4fx\n", .5);
134
135 printf ("%#03x\n", 1);
136
137 printf ("something really insane: %.10000f\n", 1.0);
138
139 {
140 double d = FLT_MIN;
141 int niter = 17;
142
143 while (niter-- != 0)
144 printf ("%.17e\n", d / 2);
145 fflush (stdout);
146 }
147
148 printf ("%15.5e\n", 4.9406564584124654e-324);
149
150 #define FORMAT "|%12.4f|%12.4e|%12.4g|\n"
151 printf (FORMAT, 0.0, 0.0, 0.0);
152 printf (FORMAT, 1.0, 1.0, 1.0);
153 printf (FORMAT, -1.0, -1.0, -1.0);
154 printf (FORMAT, 100.0, 100.0, 100.0);
155 printf (FORMAT, 1000.0, 1000.0, 1000.0);
156 printf (FORMAT, 10000.0, 10000.0, 10000.0);
157 printf (FORMAT, 12345.0, 12345.0, 12345.0);
158 printf (FORMAT, 100000.0, 100000.0, 100000.0);
159 printf (FORMAT, 123456.0, 123456.0, 123456.0);
160 #undef FORMAT
161
162 {
163 char buf[20];
164 char buf2[512];
165 printf ("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n",
166 snprintf (buf, sizeof (buf), "%30s", "foo"), (int) sizeof (buf),
167 buf);
168 printf ("snprintf (\"%%.999999u\", 10) == %d\n",
169 snprintf(buf2, sizeof(buf2), "%.999999u", 10));
170 }
171
172 printf("%.8f\n", DBL_MAX);
173 printf("%.8f\n", -DBL_MAX);
174 printf ("%e should be 1.234568e+06\n", 1234567.8);
175 printf ("%f should be 1234567.800000\n", 1234567.8);
176 printf ("%g should be 1.23457e+06\n", 1234567.8);
177 printf ("%g should be 123.456\n", 123.456);
178 printf ("%g should be 1e+06\n", 1000000.0);
179 printf ("%g should be 10\n", 10.0);
180 printf ("%g should be 0.02\n", 0.02);
181
182 #if 0
183 /* This test rather checks the way the compiler handles constant
184 folding. gcc behavior wrt to this changed in 3.2 so it is not a
185 portable test. */
186 {
187 double x=1.0;
188 printf("%.17f\n",(1.0/x/10.0+1.0)*x-x);
189 }
190 #endif
191
192 {
193 char buf[200];
194
195 sprintf(buf,"%*s%*s%*s",-1,"one",-20,"two",-30,"three");
196
197 result |= strcmp (buf,
198 "onetwo three ");
199
200 puts (result != 0 ? "Test failed!" : "Test ok.");
201 }
202
203 {
204 char buf[200];
205
206 sprintf (buf, "%07Lo", 040000000000ll);
207 printf ("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s", buf);
208
209 if (strcmp (buf, "40000000000") != 0)
210 {
211 result = 1;
212 fputs ("\tFAILED", stdout);
213 }
214 puts ("");
215 }
216
217 printf ("printf (\"%%hhu\", %u) = %hhu\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
218 printf ("printf (\"%%hu\", %u) = %hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
219 printf ("printf (\"%%hhi\", %i) = %hhi\n", UCHAR_MAX + 2, UCHAR_MAX + 2);
220 printf ("printf (\"%%hi\", %i) = %hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
221
222 printf ("printf (\"%%1$hhu\", %2$u) = %1$hhu\n",
223 UCHAR_MAX + 2, UCHAR_MAX + 2);
224 printf ("printf (\"%%1$hu\", %2$u) = %1$hu\n", USHRT_MAX + 2, USHRT_MAX + 2);
225 printf ("printf (\"%%1$hhi\", %2$i) = %1$hhi\n",
226 UCHAR_MAX + 2, UCHAR_MAX + 2);
227 printf ("printf (\"%%1$hi\", %2$i) = %1$hi\n", USHRT_MAX + 2, USHRT_MAX + 2);
228
229 puts ("--- Should be no further output. ---");
230 rfg1 ();
231 rfg2 ();
232 rfg3 ();
233
234 {
235 char bytes[7];
236 char buf[20];
237
238 memset (bytes, '\xff', sizeof bytes);
239 sprintf (buf, "foo%hhn\n", &bytes[3]);
240 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
241 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
242 {
243 puts ("%hhn overwrite more bytes");
244 result = 1;
245 }
246 if (bytes[3] != 3)
247 {
248 puts ("%hhn wrote incorrect value");
249 result = 1;
250 }
251 }
252
253 return result != 0;
254 }
255 \f
256 static void
257 rfg1 (void)
258 {
259 char buf[100];
260
261 sprintf (buf, "%5.s", "xyz");
262 if (strcmp (buf, " ") != 0)
263 printf ("got: '%s', expected: '%s'\n", buf, " ");
264 sprintf (buf, "%5.f", 33.3);
265 if (strcmp (buf, " 33") != 0)
266 printf ("got: '%s', expected: '%s'\n", buf, " 33");
267 sprintf (buf, "%8.e", 33.3e7);
268 if (strcmp (buf, " 3e+08") != 0)
269 printf ("got: '%s', expected: '%s'\n", buf, " 3e+08");
270 sprintf (buf, "%8.E", 33.3e7);
271 if (strcmp (buf, " 3E+08") != 0)
272 printf ("got: '%s', expected: '%s'\n", buf, " 3E+08");
273 sprintf (buf, "%.g", 33.3);
274 if (strcmp (buf, "3e+01") != 0)
275 printf ("got: '%s', expected: '%s'\n", buf, "3e+01");
276 sprintf (buf, "%.G", 33.3);
277 if (strcmp (buf, "3E+01") != 0)
278 printf ("got: '%s', expected: '%s'\n", buf, "3E+01");
279 }
280
281 static void
282 rfg2 (void)
283 {
284 int prec;
285 char buf[100];
286
287 prec = 0;
288 sprintf (buf, "%.*g", prec, 3.3);
289 if (strcmp (buf, "3") != 0)
290 printf ("got: '%s', expected: '%s'\n", buf, "3");
291 prec = 0;
292 sprintf (buf, "%.*G", prec, 3.3);
293 if (strcmp (buf, "3") != 0)
294 printf ("got: '%s', expected: '%s'\n", buf, "3");
295 prec = 0;
296 sprintf (buf, "%7.*G", prec, 3.33);
297 if (strcmp (buf, " 3") != 0)
298 printf ("got: '%s', expected: '%s'\n", buf, " 3");
299 prec = 3;
300 sprintf (buf, "%04.*o", prec, 33);
301 if (strcmp (buf, " 041") != 0)
302 printf ("got: '%s', expected: '%s'\n", buf, " 041");
303 prec = 7;
304 sprintf (buf, "%09.*u", prec, 33);
305 if (strcmp (buf, " 0000033") != 0)
306 printf ("got: '%s', expected: '%s'\n", buf, " 0000033");
307 prec = 3;
308 sprintf (buf, "%04.*x", prec, 33);
309 if (strcmp (buf, " 021") != 0)
310 printf ("got: '%s', expected: '%s'\n", buf, " 021");
311 prec = 3;
312 sprintf (buf, "%04.*X", prec, 33);
313 if (strcmp (buf, " 021") != 0)
314 printf ("got: '%s', expected: '%s'\n", buf, " 021");
315 }
316
317 static void
318 rfg3 (void)
319 {
320 char buf[100];
321 double g = 5.0000001;
322 unsigned long l = 1234567890;
323 double d = 321.7654321;
324 const char s[] = "test-string";
325 int i = 12345;
326 int h = 1234;
327
328 sprintf (buf,
329 "%1$*5$d %2$*6$hi %3$*7$lo %4$*8$f %9$*12$e %10$*13$g %11$*14$s",
330 i, h, l, d, 8, 5, 14, 14, d, g, s, 14, 3, 14);
331 if (strcmp (buf,
332 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string") != 0)
333 printf ("got: '%s', expected: '%s'\n", buf,
334 " 12345 1234 11145401322 321.765432 3.217654e+02 5 test-string");
335 }
336
337 #define TEST_FUNCTION do_test ()
338 #include "../test-skeleton.c"