summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/strlcat
diff options
context:
space:
mode:
authormillert <>2014-12-03 18:25:18 +0000
committermillert <>2014-12-03 18:25:18 +0000
commit449e625bd0d8eb04d234c487c99d7a78724d2540 (patch)
tree9e85370d2225ba4d58edab3261e3ff576519ac0f /src/regress/lib/libc/strlcat
parent52b1c6a2d123fb1a30f7a7323fb53bd7e121d2c1 (diff)
downloadopenbsd-449e625bd0d8eb04d234c487c99d7a78724d2540.tar.gz
openbsd-449e625bd0d8eb04d234c487c99d7a78724d2540.tar.bz2
openbsd-449e625bd0d8eb04d234c487c99d7a78724d2540.zip
Fill the buffer with 'z' instead of 'a' since 'a' is part of the
string we are testing. Add tests to verify that we get SIGSEGV when passed a NULL src or dst. It is better to crash than for an implementation to check for NULL and try to recover.
Diffstat (limited to 'src/regress/lib/libc/strlcat')
-rw-r--r--src/regress/lib/libc/strlcat/strlcattest.c95
1 files changed, 76 insertions, 19 deletions
diff --git a/src/regress/lib/libc/strlcat/strlcattest.c b/src/regress/lib/libc/strlcat/strlcattest.c
index cb3624e5a4..155aa6f727 100644
--- a/src/regress/lib/libc/strlcat/strlcattest.c
+++ b/src/regress/lib/libc/strlcat/strlcattest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: strlcattest.c,v 1.1 2014/12/02 17:48:34 millert Exp $ */ 1/* $OpenBSD: strlcattest.c,v 1.2 2014/12/03 18:25:18 millert Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com> 4 * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com>
@@ -21,13 +21,27 @@
21#include <stdio.h> 21#include <stdio.h>
22#include <stdlib.h> 22#include <stdlib.h>
23#include <string.h> 23#include <string.h>
24#include <setjmp.h>
25#include <signal.h>
24#include <unistd.h> 26#include <unistd.h>
25 27
26int main(int argc, char *argv[]) 28volatile sig_atomic_t got_signal;
29sigjmp_buf jmpenv;
30
31void
32handler(int signo)
33{
34 got_signal = signo;
35 siglongjmp(jmpenv, 1);
36}
37
38int
39main(int argc, char *argv[])
27{ 40{
28 char *buf, *cp, *ep; 41 char *buf, *cp, *ep;
29 int failures = 0; 42 struct sigaction sa;
30 size_t len, bufsize; 43 size_t len, bufsize;
44 int failures = 0;
31 45
32 /* Enable malloc security options. */ 46 /* Enable malloc security options. */
33 setenv("MALLOC_OPTIONS", "S", 0); 47 setenv("MALLOC_OPTIONS", "S", 0);
@@ -38,19 +52,21 @@ int main(int argc, char *argv[])
38 fprintf(stderr, "unable to allocate memory\n"); 52 fprintf(stderr, "unable to allocate memory\n");
39 return 1; 53 return 1;
40 } 54 }
41 memset(buf, 'a', bufsize); 55 memset(buf, 'z', bufsize);
42 ep = buf + bufsize; 56 ep = buf + bufsize;
43 57
44 /* Test appending to an unterminated string. */ 58 /* Test appending to an unterminated string. */
45 len = strlcat(buf, "abcd", bufsize); 59 len = strlcat(buf, "abcd", bufsize);
46 if (len != 4 + bufsize) { 60 if (len != 4 + bufsize) {
47 fprintf(stderr, "strlcat: failed unterminated buffer test (1a)"); 61 fprintf(stderr,
62 "strlcat: failed unterminated buffer test (1a)\n");
48 failures++; 63 failures++;
49 } 64 }
50 /* Make sure we only wrote where expected. */ 65 /* Make sure we only wrote where expected. */
51 for (cp = buf; cp < ep; cp++) { 66 for (cp = buf; cp < ep; cp++) {
52 if (*cp != 'a') { 67 if (*cp != 'z') {
53 fprintf(stderr, "strlcat: failed unterminated buffer test (1b)"); 68 fprintf(stderr,
69 "strlcat: failed unterminated buffer test (1b)\n");
54 failures++; 70 failures++;
55 break; 71 break;
56 } 72 }
@@ -60,34 +76,36 @@ int main(int argc, char *argv[])
60 ep[-1] = '\0'; 76 ep[-1] = '\0';
61 len = strlcat(buf, "abcd", bufsize); 77 len = strlcat(buf, "abcd", bufsize);
62 if (len != 4 + bufsize - 1) { 78 if (len != 4 + bufsize - 1) {
63 fprintf(stderr, "strlcat: failed full buffer test (2a)"); 79 fprintf(stderr, "strlcat: failed full buffer test (2a)\n");
64 failures++; 80 failures++;
65 } 81 }
66 /* Make sure we only wrote where expected. */ 82 /* Make sure we only wrote where expected. */
67 for (cp = buf; cp < ep - 1; cp++) { 83 for (cp = buf; cp < ep - 1; cp++) {
68 if (*cp != 'a') { 84 if (*cp != 'z') {
69 fprintf(stderr, "strlcat: failed full buffer test (2b)"); 85 fprintf(stderr,
86 "strlcat: failed full buffer test (2b)\n");
70 failures++; 87 failures++;
71 break; 88 break;
72 } 89 }
73 } 90 }
74 91
75 /* Test appending to an empty string. */ 92 /* Test appending to an empty string. */
76 ep[-1] = 'a'; 93 ep[-1] = 'z';
77 buf[0] = '\0'; 94 buf[0] = '\0';
78 len = strlcat(buf, "abcd", bufsize); 95 len = strlcat(buf, "abcd", bufsize);
79 if (len != 4) { 96 if (len != 4) {
80 fprintf(stderr, "strlcat: failed empty buffer test (3a)"); 97 fprintf(stderr, "strlcat: failed empty buffer test (3a)\n");
81 failures++; 98 failures++;
82 } 99 }
83 /* Make sure we only wrote where expected. */ 100 /* Make sure we only wrote where expected. */
84 if (memcmp(buf, "abcd", sizeof("abcd")) != 0) { 101 if (memcmp(buf, "abcd", sizeof("abcd")) != 0) {
85 fprintf(stderr, "strlcat: failed empty buffer test (3b)"); 102 fprintf(stderr, "strlcat: failed empty buffer test (3b)\n");
86 failures++; 103 failures++;
87 } 104 }
88 for (cp = buf + len + 1; cp < ep; cp++) { 105 for (cp = buf + len + 1; cp < ep; cp++) {
89 if (*cp != 'a') { 106 if (*cp != 'z') {
90 fprintf(stderr, "strlcat: failed empty buffer test (3c)"); 107 fprintf(stderr,
108 "strlcat: failed empty buffer test (3c)\n");
91 failures++; 109 failures++;
92 break; 110 break;
93 } 111 }
@@ -97,21 +115,60 @@ int main(int argc, char *argv[])
97 memcpy(buf, "abcd", sizeof("abcd")); 115 memcpy(buf, "abcd", sizeof("abcd"));
98 len = strlcat(buf, "efgh", bufsize); 116 len = strlcat(buf, "efgh", bufsize);
99 if (len != 8) { 117 if (len != 8) {
100 fprintf(stderr, "strlcat: failed empty buffer test (4a)"); 118 fprintf(stderr, "strlcat: failed empty buffer test (4a)\n");
101 failures++; 119 failures++;
102 } 120 }
103 /* Make sure we only wrote where expected. */ 121 /* Make sure we only wrote where expected. */
104 if (memcmp(buf, "abcdefgh", sizeof("abcdefgh")) != 0) { 122 if (memcmp(buf, "abcdefgh", sizeof("abcdefgh")) != 0) {
105 fprintf(stderr, "strlcat: failed empty buffer test (4b)"); 123 fprintf(stderr, "strlcat: failed empty buffer test (4b)\n");
106 failures++; 124 failures++;
107 } 125 }
108 for (cp = buf + len + 1; cp < ep; cp++) { 126 for (cp = buf + len + 1; cp < ep; cp++) {
109 if (*cp != 'a') { 127 if (*cp != 'z') {
110 fprintf(stderr, "strlcat: failed empty buffer test (4c)"); 128 fprintf(stderr,
129 "strlcat: failed empty buffer test (4c)\n");
111 failures++; 130 failures++;
112 break; 131 break;
113 } 132 }
114 } 133 }
115 134
135 /*
136 * The following tests should result in SIGSEGV, however some
137 * systems may erroneously report SIGBUS.
138 * These tests assume that strlcat() is signal-safe.
139 */
140 memset(&sa, 0, sizeof(sa));
141 sigemptyset(&sa.sa_mask);
142 sa.sa_handler = handler;
143 sigaction(SIGSEGV, &sa, NULL);
144 sigaction(SIGBUS, &sa, NULL);
145
146 /* Test copying to a NULL buffer with non-zero size. */
147 got_signal = 0;
148 if (sigsetjmp(jmpenv, 1) == 0) {
149 len = strlcat(NULL, "abcd", sizeof(buf));
150 fprintf(stderr, "strlcat: failed NULL dst test (5a), "
151 "expected signal %d, got len %zu\n", SIGSEGV, len);
152 failures++;
153 } else if (got_signal != SIGSEGV) {
154 fprintf(stderr, "strlcat: failed NULL dst test (5b), "
155 "expected signal %d, got %d\n", SIGSEGV, got_signal);
156 failures++;
157 }
158
159 /* Test copying from a NULL src. */
160 memcpy(buf, "abcd", sizeof("abcd"));
161 got_signal = 0;
162 if (sigsetjmp(jmpenv, 1) == 0) {
163 len = strlcat(buf, NULL, sizeof(buf));
164 fprintf(stderr, "strlcat: failed NULL src test (6a), "
165 "expected signal %d, got len %zu\n", SIGSEGV, len);
166 failures++;
167 } else if (got_signal != SIGSEGV) {
168 fprintf(stderr, "strlcat: failed NULL src test (6b), "
169 "expected signal %d, got %d\n", SIGSEGV, got_signal);
170 failures++;
171 }
172
116 return failures; 173 return failures;
117} 174}