tst-tzset.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* tzset tests with crafted time zone data.
  2. Copyright (C) 2015-2026 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #define _GNU_SOURCE 1
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/resource.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <support/check.h>
  23. #include <inttypes.h>
  24. static int do_test (void);
  25. #define TEST_FUNCTION do_test ()
  26. #include "../test-skeleton.c"
  27. /* Returns the name of a large TZ file. */
  28. static char *
  29. create_tz_file (off64_t size)
  30. {
  31. char *path;
  32. int fd = create_temp_file ("tst-tzset-", &path);
  33. if (fd < 0)
  34. exit (1);
  35. if (!support_descriptor_supports_holes (fd))
  36. FAIL_UNSUPPORTED ("File %s does not support holes", path);
  37. // Reopen for large-file support.
  38. close (fd);
  39. fd = open64 (path, O_WRONLY);
  40. if (fd < 0)
  41. {
  42. printf ("open64 (%s) failed: %m\n", path);
  43. exit (1);
  44. }
  45. static const char data[] = {
  46. 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00,
  47. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  49. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  50. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  51. 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
  52. 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00, 0x00,
  53. 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00,
  54. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  55. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  57. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  58. 0x00, 0x00, 0x00, 0x04, 0xf8, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00,
  61. 0x00, 0x0a, 0x58, 0x54, 0x47, 0x30, 0x0a
  62. };
  63. ssize_t ret = write (fd, data, sizeof (data));
  64. if (ret < 0)
  65. {
  66. printf ("write failed: %m\n");
  67. exit (1);
  68. }
  69. if ((size_t) ret != sizeof (data))
  70. {
  71. printf ("Short write\n");
  72. exit (1);
  73. }
  74. if (lseek64 (fd, size, SEEK_CUR) < 0)
  75. {
  76. printf ("lseek failed: %m\n");
  77. close (fd);
  78. return NULL;
  79. }
  80. if (write (fd, "", 1) != 1)
  81. {
  82. printf ("Single-byte write failed\n");
  83. close (fd);
  84. return NULL;
  85. }
  86. if (close (fd) != 0)
  87. {
  88. printf ("close failed: %m\n");
  89. exit (1);
  90. }
  91. return path;
  92. }
  93. static void
  94. test_tz_file (off64_t size)
  95. {
  96. char *path = create_tz_file (size);
  97. if (path == NULL)
  98. {
  99. printf ("creating timezone file of size: %" PRId64 "MiB failed.\n",
  100. size / (1024 * 1024));
  101. exit (1);
  102. }
  103. if (setenv ("TZ", path, 1) < 0)
  104. {
  105. printf ("setenv failed: %m\n");
  106. exit (1);
  107. }
  108. tzset ();
  109. free (path);
  110. }
  111. static int
  112. do_test (void)
  113. {
  114. /* Limit the size of the process. Otherwise, some of the tests will
  115. consume a lot of resources. */
  116. {
  117. struct rlimit limit;
  118. if (getrlimit (RLIMIT_AS, &limit) != 0)
  119. {
  120. printf ("getrlimit (RLIMIT_AS) failed: %m\n");
  121. return 1;
  122. }
  123. long target = 512 * 1024 * 1024;
  124. if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
  125. {
  126. limit.rlim_cur = 512 * 1024 * 1024;
  127. if (setrlimit (RLIMIT_AS, &limit) != 0)
  128. {
  129. printf ("setrlimit (RLIMIT_AS) failed: %m\n");
  130. return 1;
  131. }
  132. }
  133. }
  134. int errors = 0;
  135. for (int i = 1; i <= 4; ++i)
  136. {
  137. char tz[16];
  138. snprintf (tz, sizeof (tz), "XT%d", i);
  139. if (setenv ("TZ", tz, 1) < 0)
  140. {
  141. printf ("setenv failed: %m\n");
  142. return 1;
  143. }
  144. tzset ();
  145. if (strcmp (tzname[0], tz) == 0)
  146. {
  147. printf ("Unexpected success for %s\n", tz);
  148. ++errors;
  149. }
  150. }
  151. /* Large TZ files. */
  152. /* This will succeed on 64-bit architectures, and fail on 32-bit
  153. architectures. It used to crash on 32-bit. */
  154. test_tz_file (64 * 1024 * 1024);
  155. /* This will fail on 64-bit and 32-bit architectures. It used to
  156. cause a test timeout on 64-bit and crash on 32-bit if the TZ file
  157. open succeeded for some reason (it does not use O_LARGEFILE in
  158. regular builds). */
  159. test_tz_file (4LL * 1024 * 1024 * 1024 - 6);
  160. /* Large TZ variables. */
  161. {
  162. size_t length = 64 * 1024 * 1024;
  163. char *value = malloc (length + 1);
  164. if (value == NULL)
  165. {
  166. puts ("malloc failed: %m");
  167. return 1;
  168. }
  169. value[length] = '\0';
  170. memset (value, ' ', length);
  171. value[0] = 'U';
  172. value[1] = 'T';
  173. value[2] = 'C';
  174. if (setenv ("TZ", value, 1) < 0)
  175. {
  176. printf ("setenv failed: %m\n");
  177. return 1;
  178. }
  179. tzset ();
  180. memset (value, '0', length);
  181. value[0] = '<';
  182. value[length - 1] = '>';
  183. if (setenv ("TZ", value, 1) < 0)
  184. {
  185. printf ("setenv failed: %m\n");
  186. return 1;
  187. }
  188. tzset ();
  189. }
  190. return errors > 0;
  191. }