PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/posix/tst-regcomp-truncated.c

https://gitlab.com/gbenson/glibc
C | 191 lines | 163 code | 5 blank | 23 comment | 1 complexity | 794788b011dc38d31c34eb4f9b6b9ea2 MD5 | raw file
  1. /* Test compilation of truncated regular expressions.
  2. Copyright (C) 2018 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. /* This test constructs various patterns in an attempt to trigger
  16. over-reading the regular expression compiler, such as bug
  17. 23578. */
  18. #include <array_length.h>
  19. #include <errno.h>
  20. #include <locale.h>
  21. #include <regex.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <support/check.h>
  26. #include <support/next_to_fault.h>
  27. #include <support/support.h>
  28. #include <support/test-driver.h>
  29. #include <wchar.h>
  30. /* Locales to test. */
  31. static const char locales[][17] =
  32. {
  33. "C",
  34. "en_US.UTF-8",
  35. "de_DE.ISO-8859-1",
  36. };
  37. /* Syntax options. Will be combined with other flags. */
  38. static const reg_syntax_t syntaxes[] =
  39. {
  40. RE_SYNTAX_EMACS,
  41. RE_SYNTAX_AWK,
  42. RE_SYNTAX_GNU_AWK,
  43. RE_SYNTAX_POSIX_AWK,
  44. RE_SYNTAX_GREP,
  45. RE_SYNTAX_EGREP,
  46. RE_SYNTAX_POSIX_EGREP,
  47. RE_SYNTAX_POSIX_BASIC,
  48. RE_SYNTAX_POSIX_EXTENDED,
  49. RE_SYNTAX_POSIX_MINIMAL_EXTENDED,
  50. };
  51. /* Trailing characters placed after the initial character. */
  52. static const char trailing_strings[][4] =
  53. {
  54. "",
  55. "[",
  56. "\\",
  57. "[\\",
  58. "(",
  59. "(\\",
  60. "\\(",
  61. };
  62. static int
  63. do_test (void)
  64. {
  65. /* Staging buffer for the constructed regular expression. */
  66. char buffer[16];
  67. /* Allocation used to detect over-reading by the regular expression
  68. compiler. */
  69. struct support_next_to_fault ntf
  70. = support_next_to_fault_allocate (sizeof (buffer));
  71. /* Arbitrary Unicode codepoint at which we stop generating
  72. characters. We do not probe the whole range because that would
  73. take too long due to combinatorical exploision as the result of
  74. combination with other flags. */
  75. static const wchar_t last_character = 0xfff;
  76. for (size_t locale_idx = 0; locale_idx < array_length (locales);
  77. ++ locale_idx)
  78. {
  79. if (setlocale (LC_ALL, locales[locale_idx]) == NULL)
  80. {
  81. support_record_failure ();
  82. printf ("error: setlocale (\"%s\"): %m", locales[locale_idx]);
  83. continue;
  84. }
  85. if (test_verbose > 0)
  86. printf ("info: testing locale \"%s\"\n", locales[locale_idx]);
  87. for (wchar_t wc = 0; wc <= last_character; ++wc)
  88. {
  89. char *after_wc;
  90. if (wc == 0)
  91. {
  92. /* wcrtomb treats L'\0' in a special way. */
  93. *buffer = '\0';
  94. after_wc = &buffer[1];
  95. }
  96. else
  97. {
  98. mbstate_t ps = { };
  99. size_t ret = wcrtomb (buffer, wc, &ps);
  100. if (ret == (size_t) -1)
  101. {
  102. /* EILSEQ means that the target character set
  103. cannot encode the character. */
  104. if (errno != EILSEQ)
  105. {
  106. support_record_failure ();
  107. printf ("error: wcrtomb (0x%x) failed: %m\n",
  108. (unsigned) wc);
  109. }
  110. continue;
  111. }
  112. TEST_VERIFY_EXIT (ret != 0);
  113. after_wc = &buffer[ret];
  114. }
  115. for (size_t trailing_idx = 0;
  116. trailing_idx < array_length (trailing_strings);
  117. ++trailing_idx)
  118. {
  119. char *after_trailing
  120. = stpcpy (after_wc, trailing_strings[trailing_idx]);
  121. for (int do_nul = 0; do_nul < 2; ++do_nul)
  122. {
  123. char *after_nul;
  124. if (do_nul)
  125. {
  126. *after_trailing = '\0';
  127. after_nul = &after_trailing[1];
  128. }
  129. else
  130. after_nul = after_trailing;
  131. size_t length = after_nul - buffer;
  132. /* Make sure that the faulting region starts
  133. after the used portion of the buffer. */
  134. char *ntf_start = ntf.buffer + sizeof (buffer) - length;
  135. memcpy (ntf_start, buffer, length);
  136. for (const reg_syntax_t *psyntax = syntaxes;
  137. psyntax < array_end (syntaxes); ++psyntax)
  138. for (int do_icase = 0; do_icase < 2; ++do_icase)
  139. {
  140. re_syntax_options = *psyntax;
  141. if (do_icase)
  142. re_syntax_options |= RE_ICASE;
  143. regex_t reg;
  144. memset (&reg, 0, sizeof (reg));
  145. const char *msg = re_compile_pattern
  146. (ntf_start, length, &reg);
  147. if (msg != NULL)
  148. {
  149. if (test_verbose > 0)
  150. {
  151. char *quoted = support_quote_blob
  152. (buffer, length);
  153. printf ("info: compilation failed for pattern"
  154. " \"%s\", syntax 0x%lx: %s\n",
  155. quoted, re_syntax_options, msg);
  156. free (quoted);
  157. }
  158. }
  159. else
  160. regfree (&reg);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. support_next_to_fault_free (&ntf);
  167. return 0;
  168. }
  169. #include <support/test-driver.c>