test-strndup.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Test strndup functions.
  2. Copyright (C) 2023-2026 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. <https://www.gnu.org/licenses/>. */
  15. #include <support/check.h>
  16. #define TEST_MAIN
  17. #include "test-string.h"
  18. static void
  19. do_one_test (const char *src, size_t len, size_t n)
  20. {
  21. char *dst = strndup (src, n);
  22. size_t s = (len > n ? n: len) * sizeof (char);
  23. TEST_COMPARE_BLOB (dst, s, src, s);
  24. free (dst);
  25. }
  26. static void
  27. do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
  28. {
  29. size_t i;
  30. char *s1;
  31. align1 &= 7;
  32. if ((align1 + len) * sizeof (char) >= page_size)
  33. return;
  34. align2 &= 7;
  35. if ((align2 + len) * sizeof (char) >= page_size)
  36. return;
  37. s1 = (char *) (buf1) + align1;
  38. for (i = 0; i < len; ++i)
  39. s1[i] = 32 + 23 * i % (max_char - 32);
  40. s1[len] = 0;
  41. for (i = len + 1; (i + align1) * sizeof (char) < page_size && i < len + 64;
  42. ++i)
  43. s1[i] = 32 + 32 * i % (max_char - 32);
  44. do_one_test (s1, len, n);
  45. }
  46. static void
  47. do_page_tests (void)
  48. {
  49. char *s1;
  50. const size_t maxoffset = 64;
  51. /* Put s1 at the maxoffset from the edge of buf1's last page. */
  52. s1 = (char *) buf1 + BUF1PAGES * page_size / sizeof (char) - maxoffset;
  53. memset (s1, 'a', maxoffset - 1);
  54. s1[maxoffset - 1] = '\0';
  55. /* Both strings are bounded to a page with read/write access and the next
  56. page is protected with PROT_NONE (meaning that any access outside of the
  57. page regions will trigger an invalid memory access).
  58. The loop copies the string s1 for all possible offsets up to maxoffset
  59. for both inputs with a size larger than s1 (so memory access outside the
  60. expected memory regions might trigger invalid access). */
  61. for (size_t off1 = 0; off1 < maxoffset; off1++)
  62. for (size_t off2 = 0; off2 < maxoffset; off2++)
  63. do_one_test (s1 + off1, maxoffset - off1 - 1,
  64. maxoffset + (maxoffset - off2));
  65. }
  66. static void
  67. do_random_tests (void)
  68. {
  69. size_t i, j, n, align1, align2, len, size, mode;
  70. char *p1 = (char *) (buf1 + page_size) - 512;
  71. char *res;
  72. for (n = 0; n < ITERATIONS; n++)
  73. {
  74. mode = random ();
  75. if (mode & 1)
  76. {
  77. size = random () & 255;
  78. align1 = 512 - size - (random () & 15);
  79. if (mode & 2)
  80. align2 = align1 - (random () & 24);
  81. else
  82. align2 = align1 - (random () & 31);
  83. if (mode & 4)
  84. {
  85. j = align1;
  86. align1 = align2;
  87. align2 = j;
  88. }
  89. if (mode & 8)
  90. len = size - (random () & 31);
  91. else
  92. len = 512;
  93. if (len >= 512)
  94. len = random () & 511;
  95. }
  96. else
  97. {
  98. align1 = random () & 31;
  99. if (mode & 2)
  100. align2 = random () & 31;
  101. else
  102. align2 = align1 + (random () & 24);
  103. len = random () & 511;
  104. j = align1;
  105. if (align2 > j)
  106. j = align2;
  107. if (mode & 4)
  108. {
  109. size = random () & 511;
  110. if (size + j > 512)
  111. size = 512 - j - (random () & 31);
  112. }
  113. else
  114. size = 512 - j;
  115. if ((mode & 8) && len + j >= 512)
  116. len = 512 - j - (random () & 7);
  117. }
  118. j = len + align1 + 64;
  119. if (j > 512)
  120. j = 512;
  121. for (i = 0; i < j; i++)
  122. {
  123. if (i == len + align1)
  124. p1[i] = 0;
  125. else
  126. {
  127. p1[i] = random () & CHAR_MAX;
  128. if (i >= align1 && i < len + align1 && !p1[i])
  129. p1[i] = (random () & 127) + 3;
  130. }
  131. }
  132. res = (char *) strndup ((char *) (p1 + align1), size);
  133. j = len + 1;
  134. if (size < j)
  135. j = size;
  136. TEST_COMPARE_BLOB (res, j, (char *) (p1 + align1), j);
  137. free (res);
  138. }
  139. }
  140. int
  141. test_main (void)
  142. {
  143. size_t i;
  144. test_init ();
  145. printf ("%28s", "");
  146. printf ("\t%s", "strndup");
  147. putchar ('\n');
  148. for (i = 1; i < 8; ++i)
  149. {
  150. do_test (i, i, 16, 16, 127);
  151. do_test (i, i, 16, 16, CHAR_MAX);
  152. do_test (i, 2 * i, 16, 16, 127);
  153. do_test (2 * i, i, 16, 16, CHAR_MAX);
  154. do_test (8 - i, 2 * i, 1 << i, 2 << i, 127);
  155. do_test (2 * i, 8 - i, 2 << i, 1 << i, 127);
  156. do_test (8 - i, 2 * i, 1 << i, 2 << i, CHAR_MAX);
  157. do_test (2 * i, 8 - i, 2 << i, 1 << i, CHAR_MAX);
  158. }
  159. for (i = 1; i < 8; ++i)
  160. {
  161. do_test (0, 0, 4 << i, 8 << i, 127);
  162. do_test (0, 0, 16 << i, 8 << i, 127);
  163. do_test (8 - i, 2 * i, 4 << i, 8 << i, 127);
  164. do_test (8 - i, 2 * i, 16 << i, 8 << i, 127);
  165. }
  166. do_random_tests ();
  167. do_page_tests ();
  168. return ret;
  169. }
  170. #include <support/test-driver.c>