]>
Commit | Line | Data |
---|---|---|
42261ad7 | 1 | /* tzset tests with crafted time zone data. |
04277e02 | 2 | Copyright (C) 2015-2019 Free Software Foundation, Inc. |
42261ad7 FW |
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 | |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
42261ad7 FW |
17 | |
18 | #define _GNU_SOURCE 1 | |
19 | ||
20 | #include <errno.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sys/resource.h> | |
25 | #include <time.h> | |
26 | #include <unistd.h> | |
aa42b3db | 27 | #include <support/check.h> |
42261ad7 FW |
28 | |
29 | static int do_test (void); | |
30 | #define TEST_FUNCTION do_test () | |
31 | #include "../test-skeleton.c" | |
32 | ||
33 | /* Returns the name of a large TZ file. */ | |
34 | static char * | |
35 | create_tz_file (off64_t size) | |
36 | { | |
37 | char *path; | |
38 | int fd = create_temp_file ("tst-tzset-", &path); | |
39 | if (fd < 0) | |
40 | exit (1); | |
aa42b3db FW |
41 | if (!support_descriptor_supports_holes (fd)) |
42 | FAIL_UNSUPPORTED ("File %s does not support holes", path); | |
42261ad7 FW |
43 | |
44 | // Reopen for large-file support. | |
45 | close (fd); | |
46 | fd = open64 (path, O_WRONLY); | |
47 | if (fd < 0) | |
48 | { | |
49 | printf ("open64 (%s) failed: %m\n", path); | |
50 | exit (1); | |
51 | } | |
52 | ||
53 | static const char data[] = { | |
54 | 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00, | |
55 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
56 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
57 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | |
58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
59 | 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, | |
60 | 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00, 0x00, | |
61 | 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00, | |
62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
64 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, | |
65 | 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, | |
66 | 0x00, 0x00, 0x00, 0x04, 0xf8, 0x00, 0x00, 0x00, | |
67 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
68 | 0x00, 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00, | |
69 | 0x00, 0x0a, 0x58, 0x54, 0x47, 0x30, 0x0a | |
70 | }; | |
71 | ssize_t ret = write (fd, data, sizeof (data)); | |
72 | if (ret < 0) | |
73 | { | |
74 | printf ("write failed: %m\n"); | |
75 | exit (1); | |
76 | } | |
77 | if ((size_t) ret != sizeof (data)) | |
78 | { | |
79 | printf ("Short write\n"); | |
80 | exit (1); | |
81 | } | |
82 | if (lseek64 (fd, size, SEEK_CUR) < 0) | |
83 | { | |
84 | printf ("lseek failed: %m\n"); | |
85 | close (fd); | |
86 | return NULL; | |
87 | } | |
88 | if (write (fd, "", 1) != 1) | |
89 | { | |
90 | printf ("Single-byte write failed\n"); | |
91 | close (fd); | |
92 | return NULL; | |
93 | } | |
94 | if (close (fd) != 0) | |
95 | { | |
96 | printf ("close failed: %m\n"); | |
97 | exit (1); | |
98 | } | |
99 | return path; | |
100 | } | |
101 | ||
102 | static void | |
103 | test_tz_file (off64_t size) | |
104 | { | |
105 | char *path = create_tz_file (size); | |
106 | if (setenv ("TZ", path, 1) < 0) | |
107 | { | |
108 | printf ("setenv failed: %m\n"); | |
109 | exit (1); | |
110 | } | |
111 | tzset (); | |
112 | free (path); | |
113 | } | |
114 | ||
115 | static int | |
116 | do_test (void) | |
117 | { | |
118 | /* Limit the size of the process. Otherwise, some of the tests will | |
119 | consume a lot of resources. */ | |
120 | { | |
121 | struct rlimit limit; | |
122 | if (getrlimit (RLIMIT_AS, &limit) != 0) | |
123 | { | |
124 | printf ("getrlimit (RLIMIT_AS) failed: %m\n"); | |
125 | return 1; | |
126 | } | |
127 | long target = 512 * 1024 * 1024; | |
128 | if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target) | |
129 | { | |
130 | limit.rlim_cur = 512 * 1024 * 1024; | |
131 | if (setrlimit (RLIMIT_AS, &limit) != 0) | |
132 | { | |
133 | printf ("setrlimit (RLIMIT_AS) failed: %m\n"); | |
134 | return 1; | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | int errors = 0; | |
140 | for (int i = 1; i <= 4; ++i) | |
141 | { | |
142 | char tz[16]; | |
143 | snprintf (tz, sizeof (tz), "XT%d", i); | |
144 | if (setenv ("TZ", tz, 1) < 0) | |
145 | { | |
146 | printf ("setenv failed: %m\n"); | |
147 | return 1; | |
148 | } | |
149 | tzset (); | |
150 | if (strcmp (tzname[0], tz) == 0) | |
151 | { | |
152 | printf ("Unexpected success for %s\n", tz); | |
153 | ++errors; | |
154 | } | |
155 | } | |
156 | ||
157 | /* Large TZ files. */ | |
158 | ||
159 | /* This will succeed on 64-bit architectures, and fail on 32-bit | |
160 | architectures. It used to crash on 32-bit. */ | |
161 | test_tz_file (64 * 1024 * 1024); | |
162 | ||
163 | /* This will fail on 64-bit and 32-bit architectures. It used to | |
164 | cause a test timeout on 64-bit and crash on 32-bit if the TZ file | |
165 | open succeeded for some reason (it does not use O_LARGEFILE in | |
166 | regular builds). */ | |
167 | test_tz_file (4LL * 1024 * 1024 * 1024 - 6); | |
168 | ||
169 | /* Large TZ variables. */ | |
170 | { | |
171 | size_t length = 64 * 1024 * 1024; | |
172 | char *value = malloc (length + 1); | |
173 | if (value == NULL) | |
174 | { | |
175 | puts ("malloc failed: %m"); | |
176 | return 1; | |
177 | } | |
178 | value[length] = '\0'; | |
179 | ||
180 | memset (value, ' ', length); | |
181 | value[0] = 'U'; | |
182 | value[1] = 'T'; | |
183 | value[2] = 'C'; | |
184 | if (setenv ("TZ", value, 1) < 0) | |
185 | { | |
186 | printf ("setenv failed: %m\n"); | |
187 | return 1; | |
188 | } | |
189 | tzset (); | |
190 | ||
191 | memset (value, '0', length); | |
192 | value[0] = '<'; | |
193 | value[length - 1] = '>'; | |
194 | if (setenv ("TZ", value, 1) < 0) | |
195 | { | |
196 | printf ("setenv failed: %m\n"); | |
197 | return 1; | |
198 | } | |
199 | tzset (); | |
200 | } | |
201 | ||
202 | return errors > 0; | |
203 | } |