tst-strlcpy2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Test strlcpy 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. #define TEST_MAIN
  16. #ifndef WIDE
  17. # define TEST_NAME "strlcpy"
  18. #else
  19. # define TEST_NAME "wcslcpy"
  20. #endif /* WIDE */
  21. #include "test-string.h"
  22. #ifdef WIDE
  23. # include <wchar.h>
  24. # define BIG_CHAR WCHAR_MAX
  25. # define CHAR wchar_t
  26. # define MEMCMP wmemcmp
  27. # define MEMSET wmemset
  28. # define SIMPLE_STRLCPY simple_wcslcpy
  29. # define SMALL_CHAR 1273
  30. # define STRLCPY wcslcpy
  31. # define STRLEN wcslen
  32. #else
  33. # define BIG_CHAR CHAR_MAX
  34. # define CHAR char
  35. # define MEMCMP memcmp
  36. # define MEMSET memset
  37. # define SIMPLE_STRLCPY simple_strlcpy
  38. # define SMALL_CHAR 127
  39. # define STRLCPY strlcpy
  40. # define STRLEN strlen
  41. #endif /* !WIDE */
  42. /* Naive implementation to verify results. */
  43. size_t
  44. SIMPLE_STRLCPY (CHAR *dst, const CHAR *src, size_t n)
  45. {
  46. size_t ret = STRLEN (src);
  47. if (!n)
  48. return ret;
  49. while (--n)
  50. if ((*dst++ = *src++) == '\0')
  51. return ret;
  52. *dst = '\0';
  53. return ret;
  54. }
  55. IMPL (SIMPLE_STRLCPY, 0)
  56. IMPL (STRLCPY, 1)
  57. typedef size_t (*proto_t) (CHAR *, const CHAR *, size_t);
  58. static void
  59. do_one_test (impl_t *impl, CHAR *dst, const CHAR *src, size_t len, size_t n)
  60. {
  61. if (CALL (impl, dst, src, n) != len)
  62. {
  63. error (0, 0, "Wrong result in function %s %zd %zd", impl->name,
  64. CALL (impl, dst, src, n), len);
  65. ret = 1;
  66. return;
  67. }
  68. if (n == 0)
  69. return;
  70. len = (len >= n ? n - 1 : len);
  71. if (MEMCMP (dst, src, len) != 0)
  72. {
  73. error (0, 0, "Wrong result in function1 %s", impl->name);
  74. ret = 1;
  75. return;
  76. }
  77. if (dst [len] != '\0')
  78. {
  79. error (0, 0, "Wrong result in function2 %s", impl->name);
  80. ret = 1;
  81. return;
  82. }
  83. }
  84. static void
  85. do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
  86. {
  87. size_t i;
  88. CHAR *s1, *s2;
  89. /* For wcslcpy: align1 and align2 here mean alignment not in bytes,
  90. but in wchar_ts, in bytes it will equal to align * (sizeof (wchar_t)). */
  91. align1 &= 7;
  92. if ((align1 + len) * sizeof (CHAR) >= page_size)
  93. return;
  94. align2 &= 7;
  95. if ((align2 + len) * sizeof (CHAR) >= page_size)
  96. return;
  97. s1 = (CHAR *) (buf1) + align1;
  98. s2 = (CHAR *) (buf2) + align2;
  99. for (i = 0; i < len; ++i)
  100. s1[i] = 32 + 23 * i % (max_char - 32);
  101. s1[len] = 0;
  102. FOR_EACH_IMPL (impl, 0)
  103. do_one_test (impl, s2, s1, len, n);
  104. }
  105. static void
  106. do_page_tests (void)
  107. {
  108. CHAR *s1, *s2;
  109. const size_t maxoffset = 64;
  110. /* Put s1 at the maxoffset from the edge of buf1's last page. */
  111. s1 = (CHAR *) buf1 + BUF1PAGES * page_size / sizeof(CHAR) - maxoffset;
  112. /* s2 needs room to put a string with size of maxoffset + 1 at s2 +
  113. (maxoffset - 1). */
  114. s2 = (CHAR *) buf2 + page_size / sizeof(CHAR) - maxoffset * 2;
  115. MEMSET (s1, 'a', maxoffset - 1);
  116. s1[maxoffset - 1] = '\0';
  117. /* Both strings are bounded to a page with read/write access and the next
  118. page is protected with PROT_NONE (meaning that any access outside of the
  119. page regions will trigger an invalid memory access).
  120. The loop copies the string s1 for all possible offsets up to maxoffset
  121. for both inputs with a size larger than s1 (so memory access outside the
  122. expected memory regions might trigger invalid access). */
  123. for (size_t off1 = 0; off1 < maxoffset; off1++)
  124. {
  125. for (size_t off2 = 0; off2 < maxoffset; off2++)
  126. {
  127. FOR_EACH_IMPL (impl, 0)
  128. do_one_test (impl, s2 + off2, s1 + off1, maxoffset - off1 - 1,
  129. maxoffset + (maxoffset - off2));
  130. }
  131. }
  132. }
  133. static void
  134. do_random_tests (void)
  135. {
  136. size_t i, j, n, align1, align2, len, size, mode;
  137. CHAR *p1 = (CHAR *) (buf1 + page_size) - 1024;
  138. CHAR *p2 = (CHAR *) (buf2 + page_size) - 1024;
  139. size_t res;
  140. for (n = 0; n < ITERATIONS; n++)
  141. {
  142. /* For wcslcpy: align1 and align2 here mean align not in bytes,
  143. but in wchar_ts, in bytes it will equal to align * (sizeof
  144. (wchar_t)). */
  145. mode = random ();
  146. if (mode & 1)
  147. {
  148. size = random () & 255;
  149. align1 = 512 - size - (random () & 15);
  150. if (mode & 2)
  151. align2 = align1 - (random () & 24);
  152. else
  153. align2 = align1 - (random () & 31);
  154. if (mode & 4)
  155. {
  156. j = align1;
  157. align1 = align2;
  158. align2 = j;
  159. }
  160. if (mode & 8)
  161. len = size - (random () & 31);
  162. else
  163. len = 512;
  164. if (len >= 512)
  165. len = random () & 511;
  166. }
  167. else
  168. {
  169. align1 = random () & 31;
  170. if (mode & 2)
  171. align2 = random () & 31;
  172. else
  173. align2 = align1 + (random () & 24);
  174. len = random () & 511;
  175. j = align1;
  176. if (align2 > j)
  177. j = align2;
  178. if (mode & 4)
  179. {
  180. size = random () & 511;
  181. if (size + j > 512)
  182. size = 512 - j - (random () & 31);
  183. }
  184. else
  185. size = 512 - j;
  186. if ((mode & 8) && len + j >= 512)
  187. len = 512 - j - (random () & 7);
  188. }
  189. j = len + align1;
  190. for (i = 0; i < j; i++)
  191. {
  192. p1[i] = random () & BIG_CHAR;
  193. if (i >= align1 && i < len + align1 && !p1[i])
  194. p1[i] = (random () & SMALL_CHAR) + 3;
  195. }
  196. p1[i] = 0;
  197. FOR_EACH_IMPL (impl, 1)
  198. {
  199. MEMSET (p2 - 64, '\1', 512 + 64);
  200. res = CALL (impl, (CHAR *) (p2 + align2),
  201. (CHAR *) (p1 + align1), size);
  202. if (res != len)
  203. {
  204. error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd) %zd != %zd",
  205. n, impl->name, align1, align2, len, res);
  206. ret = 1;
  207. }
  208. for (j = 0; j < align2 + 64; ++j)
  209. {
  210. if (p2[j - 64] != '\1')
  211. {
  212. error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)",
  213. n, impl->name, align1, align2, len);
  214. ret = 1;
  215. break;
  216. }
  217. }
  218. j = align2 + len + 1;
  219. if (size + align2 > j)
  220. j = size + align2;
  221. for (; j < 512; ++j)
  222. {
  223. if (p2[j] != '\1')
  224. {
  225. error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)",
  226. n, impl->name, align1, align2, len);
  227. ret = 1;
  228. break;
  229. }
  230. }
  231. j = len;
  232. /* Check for zero size. */
  233. if (size)
  234. {
  235. if (size <= j)
  236. j = size - 1;
  237. if (MEMCMP (p1 + align1, p2 + align2, j))
  238. {
  239. error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)",
  240. n, impl->name, align1, align2, len);
  241. ret = 1;
  242. }
  243. if (p2[align2 + j])
  244. {
  245. error (0, 0, "Iteration %zd - garbage after size, %s (%zd, %zd, %zd)",
  246. n, impl->name, align1, align2, len);
  247. ret = 1;
  248. break;
  249. }
  250. }
  251. }
  252. }
  253. }
  254. int
  255. test_main (void)
  256. {
  257. size_t i;
  258. test_init ();
  259. printf ("%28s", "");
  260. FOR_EACH_IMPL (impl, 0)
  261. printf ("\t%s", impl->name);
  262. putchar ('\n');
  263. for (i = 1; i < 8; ++i)
  264. {
  265. do_test (i, i, 16, 16, SMALL_CHAR);
  266. do_test (i, i, 16, 16, BIG_CHAR);
  267. do_test (i, 2 * i, 16, 16, SMALL_CHAR);
  268. do_test (2 * i, i, 16, 16, BIG_CHAR);
  269. do_test (8 - i, 2 * i, 1 << i, 2 << i, SMALL_CHAR);
  270. do_test (2 * i, 8 - i, 2 << i, 1 << i, SMALL_CHAR);
  271. do_test (8 - i, 2 * i, 1 << i, 2 << i, BIG_CHAR);
  272. do_test (2 * i, 8 - i, 2 << i, 1 << i, BIG_CHAR);
  273. }
  274. for (i = 1; i < 8; ++i)
  275. {
  276. do_test (0, 0, 4 << i, 8 << i, SMALL_CHAR);
  277. do_test (0, 0, 16 << i, 8 << i, SMALL_CHAR);
  278. do_test (8 - i, 2 * i, 4 << i, 8 << i, SMALL_CHAR);
  279. do_test (8 - i, 2 * i, 16 << i, 8 << i, SMALL_CHAR);
  280. }
  281. do_random_tests ();
  282. do_page_tests ();
  283. return ret;
  284. }
  285. #include <support/test-driver.c>