Make time zone file parser more robust [BZ #17715]
[glibc.git] / timezone / tst-tzset.c
blobaefcc765dd936da852e725bd5e90dfc88adfcce0
1 /* tzset tests with crafted time zone data.
2 Copyright (C) 2015 Free Software Foundation, Inc.
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.
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.
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/>. */
18 #define _GNU_SOURCE 1
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>
28 static int do_test (void);
29 #define TEST_FUNCTION do_test ()
30 #include "../test-skeleton.c"
32 /* Returns the name of a large TZ file. */
33 static char *
34 create_tz_file (off64_t size)
36 char *path;
37 int fd = create_temp_file ("tst-tzset-", &path);
38 if (fd < 0)
39 exit (1);
41 // Reopen for large-file support.
42 close (fd);
43 fd = open64 (path, O_WRONLY);
44 if (fd < 0)
46 printf ("open64 (%s) failed: %m\n", path);
47 exit (1);
50 static const char data[] = {
51 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
54 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
56 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00, 0x00,
58 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
61 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
62 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
63 0x00, 0x00, 0x00, 0x04, 0xf8, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00,
66 0x00, 0x0a, 0x58, 0x54, 0x47, 0x30, 0x0a
68 ssize_t ret = write (fd, data, sizeof (data));
69 if (ret < 0)
71 printf ("write failed: %m\n");
72 exit (1);
74 if ((size_t) ret != sizeof (data))
76 printf ("Short write\n");
77 exit (1);
79 if (lseek64 (fd, size, SEEK_CUR) < 0)
81 printf ("lseek failed: %m\n");
82 close (fd);
83 return NULL;
85 if (write (fd, "", 1) != 1)
87 printf ("Single-byte write failed\n");
88 close (fd);
89 return NULL;
91 if (close (fd) != 0)
93 printf ("close failed: %m\n");
94 exit (1);
96 return path;
99 static void
100 test_tz_file (off64_t size)
102 char *path = create_tz_file (size);
103 if (setenv ("TZ", path, 1) < 0)
105 printf ("setenv failed: %m\n");
106 exit (1);
108 tzset ();
109 free (path);
112 static int
113 do_test (void)
115 /* Limit the size of the process. Otherwise, some of the tests will
116 consume a lot of resources. */
118 struct rlimit limit;
119 if (getrlimit (RLIMIT_AS, &limit) != 0)
121 printf ("getrlimit (RLIMIT_AS) failed: %m\n");
122 return 1;
124 long target = 512 * 1024 * 1024;
125 if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
127 limit.rlim_cur = 512 * 1024 * 1024;
128 if (setrlimit (RLIMIT_AS, &limit) != 0)
130 printf ("setrlimit (RLIMIT_AS) failed: %m\n");
131 return 1;
136 int errors = 0;
137 for (int i = 1; i <= 4; ++i)
139 char tz[16];
140 snprintf (tz, sizeof (tz), "XT%d", i);
141 if (setenv ("TZ", tz, 1) < 0)
143 printf ("setenv failed: %m\n");
144 return 1;
146 tzset ();
147 if (strcmp (tzname[0], tz) == 0)
149 printf ("Unexpected success for %s\n", tz);
150 ++errors;
154 /* Large TZ files. */
156 /* This will succeed on 64-bit architectures, and fail on 32-bit
157 architectures. It used to crash on 32-bit. */
158 test_tz_file (64 * 1024 * 1024);
160 /* This will fail on 64-bit and 32-bit architectures. It used to
161 cause a test timeout on 64-bit and crash on 32-bit if the TZ file
162 open succeeded for some reason (it does not use O_LARGEFILE in
163 regular builds). */
164 test_tz_file (4LL * 1024 * 1024 * 1024 - 6);
166 /* Large TZ variables. */
168 size_t length = 64 * 1024 * 1024;
169 char *value = malloc (length + 1);
170 if (value == NULL)
172 puts ("malloc failed: %m");
173 return 1;
175 value[length] = '\0';
177 memset (value, ' ', length);
178 value[0] = 'U';
179 value[1] = 'T';
180 value[2] = 'C';
181 if (setenv ("TZ", value, 1) < 0)
183 printf ("setenv failed: %m\n");
184 return 1;
186 tzset ();
188 memset (value, '0', length);
189 value[0] = '<';
190 value[length - 1] = '>';
191 if (setenv ("TZ", value, 1) < 0)
193 printf ("setenv failed: %m\n");
194 return 1;
196 tzset ();
199 return errors > 0;