aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/string/t_strcat.c
blob: bd98691da6dcbf3b5741ea7c2cfc3733f2249a66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* $NetBSD: t_strcat.c,v 1.2 2011/07/14 05:46:04 jruoho Exp $ */

/*
 * Written by J.T. Conklin <jtc@acorntoolworks.com>
 * Public domain.
 */

#include <atf-c.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

ATF_TC(strcat_basic);
ATF_TC_HEAD(strcat_basic, tc)
{
        atf_tc_set_md_var(tc, "descr", "Test strcat(3) results");
}

ATF_TC_BODY(strcat_basic, tc)
{
	/* try to trick the compiler */
	char * (*f)(char *, const char *s) = strcat;

	unsigned int a0, a1, t0, t1;
	char buf0[64];
	char buf1[64];
	char *ret;

	struct tab {
		const char*	val;
		size_t		len;
	};

	const struct tab tab[] = {
	/*
	 * patterns that check for all combinations of leading and
	 * trailing unaligned characters (on a 64 bit processor)
	 */

		{ "",				0 },
		{ "a",				1 },
		{ "ab",				2 },
		{ "abc",			3 },
		{ "abcd",			4 },
		{ "abcde",			5 },
		{ "abcdef",			6 },
		{ "abcdefg",			7 },
		{ "abcdefgh",			8 },
		{ "abcdefghi",			9 },
		{ "abcdefghij",			10 },
		{ "abcdefghijk",		11 },
		{ "abcdefghijkl",		12 },
		{ "abcdefghijklm",		13 },
		{ "abcdefghijklmn",		14 },
		{ "abcdefghijklmno",		15 },
		{ "abcdefghijklmnop",		16 },
		{ "abcdefghijklmnopq",		17 },
		{ "abcdefghijklmnopqr",		18 },
		{ "abcdefghijklmnopqrs",	19 },
		{ "abcdefghijklmnopqrst",	20 },
		{ "abcdefghijklmnopqrstu",	21 },
		{ "abcdefghijklmnopqrstuv",	22 },
		{ "abcdefghijklmnopqrstuvw",	23 },

		/*
		 * patterns that check for the cases where the expression:
		 *
		 *	((word - 0x7f7f..7f) & 0x8080..80)
		 *
		 * returns non-zero even though there are no zero bytes in
		 * the word.
		 */

		{ "" "\xff\xff\xff\xff\xff\xff\xff\xff" "abcdefgh",	16 },
		{ "a" "\xff\xff\xff\xff\xff\xff\xff\xff" "bcdefgh",	16 },
		{ "ab" "\xff\xff\xff\xff\xff\xff\xff\xff" "cdefgh",	16 },
		{ "abc" "\xff\xff\xff\xff\xff\xff\xff\xff" "defgh",	16 },
		{ "abcd" "\xff\xff\xff\xff\xff\xff\xff\xff" "efgh",	16 },
		{ "abcde" "\xff\xff\xff\xff\xff\xff\xff\xff" "fgh",	16 },
		{ "abcdef" "\xff\xff\xff\xff\xff\xff\xff\xff" "gh",	16 },
		{ "abcdefg" "\xff\xff\xff\xff\xff\xff\xff\xff" "h",	16 },
		{ "abcdefgh" "\xff\xff\xff\xff\xff\xff\xff\xff" "",	16 },
	};

	for (a0 = 0; a0 < sizeof(long); ++a0) {
		for (a1 = 0; a1 < sizeof(long); ++a1) {
			for (t0 = 0; t0 < __arraycount(tab); ++t0) {
				for (t1 = 0; t1 < __arraycount(tab); ++t1) {

					memcpy(&buf0[a0], tab[t0].val,
					    tab[t0].len + 1);
					memcpy(&buf1[a1], tab[t1].val,
					    tab[t1].len + 1);

					ret = f(&buf0[a0], &buf1[a1]);

					/*
					 * verify strcat returns address
					 * of first parameter
					 */
					if (&buf0[a0] != ret) {
						fprintf(stderr, "a0 %d, a1 %d, "
						    "t0 %d, t1 %d\n",
						    a0, a1, t0, t1);
						atf_tc_fail("strcat did not "
						    "return its first arg");
					}

					/* verify string copied correctly */
					if (memcmp(&buf0[a0] + tab[t0].len,
						   &buf1[a1],
						   tab[t1].len + 1) != 0) {
						fprintf(stderr, "a0 %d, a1 %d, "
						    "t0 %d, t1 %d\n",
						    a0, a1, t0, t1);
						atf_tc_fail("string not copied "
						    "correctly");
					}
				}
			}
		}
	}
}

ATF_TC(strncat_simple);
ATF_TC_HEAD(strncat_simple, tc)
{
        atf_tc_set_md_var(tc, "descr", "Test strncat(3) results");
}

ATF_TC_BODY(strncat_simple, tc)
{
	char buf[100] = "abcdefg";

	ATF_CHECK(strncat(buf, "xxx", 0) == buf);
	ATF_CHECK(strcmp(buf, "abcdefg") == 0);
	ATF_CHECK(strncat(buf, "xxx", 1) == buf);
	ATF_CHECK(strcmp(buf, "abcdefgx") == 0);
	ATF_CHECK(strncat(buf, "xxx", 2) == buf);
	ATF_CHECK(strcmp(buf, "abcdefgxxx") == 0);
	ATF_CHECK(strncat(buf, "\0", 1) == buf);
	ATF_CHECK(strcmp(buf, "abcdefgxxx") == 0);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, strcat_basic);
	ATF_TP_ADD_TC(tp, strncat_simple);

	return atf_no_error();
}