1 /* Test strndup functions.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/check.h>
22 #include "test-string.h"
25 do_one_test (const char *src
, size_t len
, size_t n
)
27 char *dst
= strndup (src
, n
);
28 size_t s
= (len
> n
? n
: len
) * sizeof (char);
30 TEST_COMPARE_BLOB (dst
, s
, src
, s
);
36 do_test (size_t align1
, size_t align2
, size_t len
, size_t n
, int max_char
)
42 if ((align1
+ len
) * sizeof (char) >= page_size
)
46 if ((align2
+ len
) * sizeof (char) >= page_size
)
49 s1
= (char *) (buf1
) + align1
;
51 for (i
= 0; i
< len
; ++i
)
52 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
54 for (i
= len
+ 1; (i
+ align1
) * sizeof (char) < page_size
&& i
< len
+ 64;
56 s1
[i
] = 32 + 32 * i
% (max_char
- 32);
58 do_one_test (s1
, len
, n
);
65 const size_t maxoffset
= 64;
67 /* Put s1 at the maxoffset from the edge of buf1's last page. */
68 s1
= (char *) buf1
+ BUF1PAGES
* page_size
/ sizeof (char) - maxoffset
;
70 memset (s1
, 'a', maxoffset
- 1);
71 s1
[maxoffset
- 1] = '\0';
73 /* Both strings are bounded to a page with read/write access and the next
74 page is protected with PROT_NONE (meaning that any access outside of the
75 page regions will trigger an invalid memory access).
77 The loop copies the string s1 for all possible offsets up to maxoffset
78 for both inputs with a size larger than s1 (so memory access outside the
79 expected memory regions might trigger invalid access). */
81 for (size_t off1
= 0; off1
< maxoffset
; off1
++)
82 for (size_t off2
= 0; off2
< maxoffset
; off2
++)
83 do_one_test (s1
+ off1
, maxoffset
- off1
- 1,
84 maxoffset
+ (maxoffset
- off2
));
88 do_random_tests (void)
90 size_t i
, j
, n
, align1
, align2
, len
, size
, mode
;
91 char *p1
= (char *) (buf1
+ page_size
) - 512;
94 for (n
= 0; n
< ITERATIONS
; n
++)
99 size
= random () & 255;
100 align1
= 512 - size
- (random () & 15);
102 align2
= align1
- (random () & 24);
104 align2
= align1
- (random () & 31);
112 len
= size
- (random () & 31);
116 len
= random () & 511;
120 align1
= random () & 31;
122 align2
= random () & 31;
124 align2
= align1
+ (random () & 24);
125 len
= random () & 511;
131 size
= random () & 511;
133 size
= 512 - j
- (random () & 31);
137 if ((mode
& 8) && len
+ j
>= 512)
138 len
= 512 - j
- (random () & 7);
140 j
= len
+ align1
+ 64;
143 for (i
= 0; i
< j
; i
++)
145 if (i
== len
+ align1
)
149 p1
[i
] = random () & CHAR_MAX
;
150 if (i
>= align1
&& i
< len
+ align1
&& !p1
[i
])
151 p1
[i
] = (random () & 127) + 3;
155 res
= (char *) strndup ((char *) (p1
+ align1
), size
);
159 TEST_COMPARE_BLOB (res
, j
, (char *) (p1
+ align1
), j
);
172 printf ("\t%s", "strndup");
175 for (i
= 1; i
< 8; ++i
)
177 do_test (i
, i
, 16, 16, 127);
178 do_test (i
, i
, 16, 16, CHAR_MAX
);
179 do_test (i
, 2 * i
, 16, 16, 127);
180 do_test (2 * i
, i
, 16, 16, CHAR_MAX
);
181 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, 127);
182 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, 127);
183 do_test (8 - i
, 2 * i
, 1 << i
, 2 << i
, CHAR_MAX
);
184 do_test (2 * i
, 8 - i
, 2 << i
, 1 << i
, CHAR_MAX
);
187 for (i
= 1; i
< 8; ++i
)
189 do_test (0, 0, 4 << i
, 8 << i
, 127);
190 do_test (0, 0, 16 << i
, 8 << i
, 127);
191 do_test (8 - i
, 2 * i
, 4 << i
, 8 << i
, 127);
192 do_test (8 - i
, 2 * i
, 16 << i
, 8 << i
, 127);
200 #include <support/test-driver.c>