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