]>
Commit | Line | Data |
---|---|---|
fd3e6373 | 1 | /* Tests of *printf for very large strings. |
04277e02 | 2 | Copyright (C) 2000-2019 Free Software Foundation, Inc. |
fd3e6373 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2000. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
fd3e6373 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
fd3e6373 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
fd3e6373 | 19 | |
d10b132b | 20 | #include <array_length.h> |
fd3e6373 UD |
21 | #include <locale.h> |
22 | #include <mcheck.h> | |
23 | #include <stdint.h> | |
24 | #include <stdio.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/stat.h> | |
e15f7de6 | 29 | #include <libc-diag.h> |
fd3e6373 UD |
30 | |
31 | ||
32 | const char *locs[] = | |
33 | { | |
34 | "C", "de_DE.ISO-8859-1", "de_DE.UTF-8", "ja_JP.EUC-JP" | |
35 | }; | |
fd3e6373 UD |
36 | |
37 | char large[50000]; | |
38 | ||
29955b5d AS |
39 | static int |
40 | do_test (void) | |
fd3e6373 | 41 | { |
5dfe6778 | 42 | char buf[25]; |
57b36a0a | 43 | size_t i; |
fd3e6373 UD |
44 | int res = 0; |
45 | int fd; | |
46 | ||
47 | mtrace (); | |
48 | ||
5dfe6778 | 49 | strcpy (buf, "/tmp/test-vfprintfXXXXXX"); |
fd3e6373 UD |
50 | fd = mkstemp (buf); |
51 | if (fd == -1) | |
52 | { | |
53 | printf ("cannot open temporary file: %m\n"); | |
54 | exit (1); | |
55 | } | |
56 | unlink (buf); | |
57 | ||
d10b132b | 58 | for (i = 0; i < array_length (locs); ++i) |
fd3e6373 UD |
59 | { |
60 | FILE *fp; | |
61 | struct stat st; | |
62 | int fd2; | |
63 | ||
64 | setlocale (LC_ALL, locs[i]); | |
65 | ||
66 | memset (large, '\1', sizeof (large)); | |
67 | large[sizeof (large) - 1] = '\0'; | |
68 | ||
69 | fd2 = dup (fd); | |
70 | if (fd2 == -1) | |
71 | { | |
72 | printf ("cannot dup for locale %s: %m\n", | |
73 | setlocale (LC_ALL, NULL)); | |
74 | exit (1); | |
75 | } | |
76 | ||
77 | if (ftruncate (fd2, 0) != 0) | |
78 | { | |
79 | printf ("cannot truncate file for locale %s: %m\n", | |
80 | setlocale (LC_ALL, NULL)); | |
81 | exit (1); | |
82 | } | |
83 | ||
84 | fp = fdopen (fd2, "a"); | |
85 | if (fp == NULL) | |
86 | { | |
87 | printf ("cannot create FILE for locale %s: %m\n", | |
88 | setlocale (LC_ALL, NULL)); | |
89 | exit (1); | |
90 | } | |
91 | ||
92 | fprintf (fp, "%s", large); | |
93 | fprintf (fp, "%.*s", 30000, large); | |
94 | large[20000] = '\0'; | |
1c4053db RM |
95 | /* We're testing a large format string here and need to generate it |
96 | to avoid this source file being ridiculous. So disable the warning | |
97 | about a generated format string. */ | |
98 | DIAG_PUSH_NEEDS_COMMENT; | |
99 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-security"); | |
fd3e6373 | 100 | fprintf (fp, large); |
1c4053db | 101 | DIAG_POP_NEEDS_COMMENT; |
1cb04337 | 102 | fprintf (fp, "%-1.300000000s", "hello"); |
fd3e6373 UD |
103 | |
104 | if (fflush (fp) != 0 || ferror (fp) != 0 || fclose (fp) != 0) | |
105 | { | |
106 | printf ("write error for locale %s: %m\n", | |
107 | setlocale (LC_ALL, NULL)); | |
108 | exit (1); | |
109 | } | |
110 | ||
111 | if (fstat (fd, &st) != 0) | |
112 | { | |
113 | printf ("cannot stat for locale %s: %m\n", | |
114 | setlocale (LC_ALL, NULL)); | |
115 | exit (1); | |
116 | } | |
1cb04337 | 117 | else if (st.st_size != 50000 + 30000 + 19999 + 5) |
fd3e6373 UD |
118 | { |
119 | printf ("file size incorrect for locale %s: %jd instead of %jd\n", | |
120 | setlocale (LC_ALL, NULL), | |
1cb04337 UD |
121 | (intmax_t) st.st_size, |
122 | (intmax_t) 50000 + 30000 + 19999 + 5); | |
fd3e6373 UD |
123 | res = 1; |
124 | } | |
125 | else | |
126 | printf ("locale %s OK\n", setlocale (LC_ALL, NULL)); | |
127 | } | |
128 | ||
129 | close (fd); | |
130 | ||
131 | return res; | |
132 | } | |
29955b5d AS |
133 | |
134 | #define TEST_FUNCTION do_test () | |
135 | #include "../test-skeleton.c" |