tst-tzset.c 5.1 KB

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