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