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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/* $OpenBSD: strlcpytest.c,v 1.3 2019/01/25 00:19:26 millert Exp $ */
/*
* Copyright (c) 2014 Todd C. Miller <millert@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <unistd.h>
volatile sig_atomic_t got_signal;
sigjmp_buf jmpenv;
void
handler(int signo)
{
got_signal = signo;
siglongjmp(jmpenv, 1);
}
int
main(int argc, char *argv[])
{
char *buf, *buf2, *cp, *ep;
struct sigaction sa;
size_t len, bufsize;
int failures = 0;
/* Enable malloc security options. */
setenv("MALLOC_OPTIONS", "S", 0);
bufsize = getpagesize(); /* trigger guard pages easily */
buf = malloc(bufsize);
buf2 = malloc(bufsize);
if (buf == NULL || buf2 == NULL) {
fprintf(stderr, "unable to allocate memory\n");
return 1;
}
memset(buf, 'z', bufsize);
ep = buf + bufsize;
/* Test copying to a zero-length NULL buffer. */
len = strlcpy(NULL, "abcd", 0);
if (len != 4) {
fprintf(stderr,
"strlcpy: failed zero-length buffer test (1a)\n");
failures++;
}
/* Test copying small string to a large buffer. */
len = strlcpy(buf, "abcd", bufsize);
if (len != 4) {
fprintf(stderr, "strlcpy: failed large buffer test (2a)\n");
failures++;
}
/* Make sure we only wrote where expected. */
if (memcmp(buf, "abcd", sizeof("abcd")) != 0) {
fprintf(stderr, "strlcpy: failed large buffer test (2b)\n");
failures++;
}
for (cp = buf + len + 1; cp < ep; cp++) {
if (*cp != 'z') {
fprintf(stderr,
"strlcpy: failed large buffer test (2c)\n");
failures++;
break;
}
}
/* Test copying large string to a small buffer. */
memset(buf, 'z', bufsize);
memset(buf2, 'x', bufsize - 1);
buf2[bufsize - 1] = '\0';
len = strlcpy(buf, buf2, bufsize / 2);
if (len != bufsize - 1) {
fprintf(stderr, "strlcpy: failed small buffer test (3a)\n");
failures++;
}
/* Make sure we only wrote where expected. */
len = (bufsize / 2) - 1;
if (memcmp(buf, buf2, len) != 0 || buf[len] != '\0') {
fprintf(stderr, "strlcpy: failed small buffer test (3b)\n");
failures++;
}
for (cp = buf + len + 1; cp < ep; cp++) {
if (*cp != 'z') {
fprintf(stderr,
"strlcpy: failed small buffer test (3c)\n");
failures++;
break;
}
}
/* Test copying to a 1-byte buffer. */
memset(buf, 'z', bufsize);
len = strlcpy(buf, "abcd", 1);
if (len != 4) {
fprintf(stderr, "strlcpy: failed 1-byte buffer test (4a)\n");
failures++;
}
/* Make sure we only wrote where expected. */
if (buf[0] != '\0') {
fprintf(stderr, "strlcpy: failed 1-byte buffer test (4b)\n");
failures++;
}
for (cp = buf + 1; cp < ep; cp++) {
if (*cp != 'z') {
fprintf(stderr,
"strlcpy: failed 1-byte buffer test (4c)\n");
failures++;
break;
}
}
/*
* The following tests should result in SIGSEGV, however some
* systems may erroneously report SIGBUS.
* These tests assume that strlcpy() is signal-safe.
*/
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
sa.sa_handler = handler;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
/* Test copying to a NULL buffer with non-zero size. */
got_signal = 0;
if (sigsetjmp(jmpenv, 1) == 0) {
len = strlcpy(NULL, "abcd", sizeof(buf));
fprintf(stderr, "strlcpy: failed NULL dst test (5a), "
"expected signal %d, got len %zu\n", SIGSEGV, len);
failures++;
} else if (got_signal != SIGSEGV) {
fprintf(stderr, "strlcpy: failed NULL dst test (5b), "
"expected signal %d, got %d\n", SIGSEGV, got_signal);
failures++;
}
/* Test copying from a NULL src. */
got_signal = 0;
if (sigsetjmp(jmpenv, 1) == 0) {
len = strlcpy(buf, NULL, sizeof(buf));
fprintf(stderr, "strlcpy: failed NULL src test (6a), "
"expected signal %d, got len %zu\n", SIGSEGV, len);
failures++;
} else if (got_signal != SIGSEGV) {
fprintf(stderr, "strlcpy: failed NULL src test (6b), "
"expected signal %d, got %d\n", SIGSEGV, got_signal);
failures++;
}
return failures;
}
|