No empty .Rs/.Re
[netbsd-mini2440.git] / regress / lib / libc / string / strcpy / strcpy_test.c
blob5c332df54e5a50c777784e4981efe21893bae3c4
1 /*
2 * Written by J.T. Conklin <jtc@acorntoolworks.com>
3 * Public domain.
4 */
6 /*
7 * str*() regression suite
9 * Trivial str*() implementations can be audited by hand. Optimized
10 * versions that unroll loops, use naturally-aligned memory acesses,
11 * and "magic" arithmetic sequences to detect zero-bytes, written in
12 * assembler are much harder to validate. This program attempts to
13 * catch the corner cases.
15 * BUGS:
16 * Misssing checks for strncpy, strncat, strncmp, etc.
18 * TODO:
19 * Use mmap/mprotect to ensure the functions don't access memory
20 * across page boundaries.
22 * Consider generating tables programmatically. It would reduce
23 * the size, but it's also one more thing to go wrong.
25 * Share tables between strlen, strcpy, and strcat?
26 * Share tables between strchr and strrchr?
29 #include <assert.h>
30 #include <string.h>
32 void check_strcpy(void);
34 void
35 check_strcpy(void)
37 /* try to trick the compiler */
38 char * (*f)(char *, const char *s) = strcpy;
40 int a0, a1;
41 int t;
42 char buf0[64];
43 char buf1[64];
44 char *ret;
46 struct tab {
47 const char* val;
48 size_t len;
51 const struct tab tab[] = {
53 * patterns that check for all combinations of leading and
54 * trailing unaligned characters (on a 64 bit processor)
57 { "", 0 },
58 { "a", 1 },
59 { "ab", 2 },
60 { "abc", 3 },
61 { "abcd", 4 },
62 { "abcde", 5 },
63 { "abcdef", 6 },
64 { "abcdefg", 7 },
65 { "abcdefgh", 8 },
66 { "abcdefghi", 9 },
67 { "abcdefghij", 10 },
68 { "abcdefghijk", 11 },
69 { "abcdefghijkl", 12 },
70 { "abcdefghijklm", 13 },
71 { "abcdefghijklmn", 14 },
72 { "abcdefghijklmno", 15 },
73 { "abcdefghijklmnop", 16 },
74 { "abcdefghijklmnopq", 17 },
75 { "abcdefghijklmnopqr", 18 },
76 { "abcdefghijklmnopqrs", 19 },
77 { "abcdefghijklmnopqrst", 20 },
78 { "abcdefghijklmnopqrstu", 21 },
79 { "abcdefghijklmnopqrstuv", 22 },
80 { "abcdefghijklmnopqrstuvw", 23 },
83 * patterns that check for the cases where the expression:
85 * ((word - 0x7f7f..7f) & 0x8080..80)
87 * returns non-zero even though there are no zero bytes in the
88 * word.
91 { "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh", 16 },
92 { "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh", 16 },
93 { "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh", 16 },
94 { "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh", 16 },
95 { "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh", 16 },
96 { "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh", 16 },
97 { "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh", 16 },
98 { "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h", 16 },
99 { "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "", 16 },
102 for (a0 = 0; a0 < sizeof(long); ++a0) {
103 for (a1 = 0; a1 < sizeof(long); ++a1) {
104 for (t = 0; t < (sizeof(tab) / sizeof(tab[0])); ++t) {
106 memcpy(&buf1[a1], tab[t].val, tab[t].len + 1);
107 ret = f(&buf0[a0], &buf1[a1]);
109 // verify strcpy returns address of first parameter
110 assert(&buf0[a0] == ret);
112 // verify string was copied correctly
113 assert(memcmp(&buf0[a0], &buf1[a1], tab[t].len + 1) == 0);
120 main(void)
122 check_strcpy();
123 return 0;