diff options
| author | millert <> | 2014-12-03 18:25:18 +0000 |
|---|---|---|
| committer | millert <> | 2014-12-03 18:25:18 +0000 |
| commit | 449e625bd0d8eb04d234c487c99d7a78724d2540 (patch) | |
| tree | 9e85370d2225ba4d58edab3261e3ff576519ac0f /src/regress/lib/libc | |
| parent | 52b1c6a2d123fb1a30f7a7323fb53bd7e121d2c1 (diff) | |
| download | openbsd-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')
| -rw-r--r-- | src/regress/lib/libc/strlcat/strlcattest.c | 95 | ||||
| -rw-r--r-- | src/regress/lib/libc/strlcpy/strlcpytest.c | 93 |
2 files changed, 150 insertions, 38 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 | ||
| 26 | int main(int argc, char *argv[]) | 28 | volatile sig_atomic_t got_signal; |
| 29 | sigjmp_buf jmpenv; | ||
| 30 | |||
| 31 | void | ||
| 32 | handler(int signo) | ||
| 33 | { | ||
| 34 | got_signal = signo; | ||
| 35 | siglongjmp(jmpenv, 1); | ||
| 36 | } | ||
| 37 | |||
| 38 | int | ||
| 39 | main(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 | } |
diff --git a/src/regress/lib/libc/strlcpy/strlcpytest.c b/src/regress/lib/libc/strlcpy/strlcpytest.c index f2760c2f99..df5d36366c 100644 --- a/src/regress/lib/libc/strlcpy/strlcpytest.c +++ b/src/regress/lib/libc/strlcpy/strlcpytest.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: strlcpytest.c,v 1.1 2014/12/02 20:23:05 millert Exp $ */ | 1 | /* $OpenBSD: strlcpytest.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 <signal.h> | ||
| 25 | #include <setjmp.h> | ||
| 24 | #include <unistd.h> | 26 | #include <unistd.h> |
| 25 | 27 | ||
| 26 | int main(int argc, char *argv[]) | 28 | volatile sig_atomic_t got_signal; |
| 29 | sigjmp_buf jmpenv; | ||
| 30 | |||
| 31 | void | ||
| 32 | handler(int signo) | ||
| 33 | { | ||
| 34 | got_signal = signo; | ||
| 35 | siglongjmp(jmpenv, 1); | ||
| 36 | } | ||
| 37 | |||
| 38 | int | ||
| 39 | main(int argc, char *argv[]) | ||
| 27 | { | 40 | { |
| 28 | char *buf, *buf2, *cp, *ep; | 41 | char *buf, *buf2, *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); |
| @@ -39,77 +53,118 @@ int main(int argc, char *argv[]) | |||
| 39 | fprintf(stderr, "unable to allocate memory\n"); | 53 | fprintf(stderr, "unable to allocate memory\n"); |
| 40 | return 1; | 54 | return 1; |
| 41 | } | 55 | } |
| 42 | memset(buf, 'a', bufsize); | 56 | memset(buf, 'z', bufsize); |
| 43 | ep = buf + bufsize; | 57 | ep = buf + bufsize; |
| 44 | 58 | ||
| 45 | /* Test copying to a zero-length NULL buffer. */ | 59 | /* Test copying to a zero-length NULL buffer. */ |
| 46 | len = strlcpy(NULL, "abcd", 0); | 60 | len = strlcpy(NULL, "abcd", 0); |
| 47 | if (len != 4) { | 61 | if (len != 4) { |
| 48 | fprintf(stderr, "strlcpy: failed NULL buffer test (1a)"); | 62 | fprintf(stderr, |
| 63 | "strlcpy: failed zero-length buffer test (1a)\n"); | ||
| 49 | failures++; | 64 | failures++; |
| 50 | } | 65 | } |
| 51 | 66 | ||
| 52 | /* Test copying small string to a large buffer. */ | 67 | /* Test copying small string to a large buffer. */ |
| 53 | len = strlcpy(buf, "abcd", bufsize); | 68 | len = strlcpy(buf, "abcd", bufsize); |
| 54 | if (len != 4) { | 69 | if (len != 4) { |
| 55 | fprintf(stderr, "strlcpy: failed large buffer test (2a)"); | 70 | fprintf(stderr, "strlcpy: failed large buffer test (2a)\n"); |
| 56 | failures++; | 71 | failures++; |
| 57 | } | 72 | } |
| 58 | /* Make sure we only wrote where expected. */ | 73 | /* Make sure we only wrote where expected. */ |
| 59 | if (memcmp(buf, "abcd", sizeof("abcd")) != 0) { | 74 | if (memcmp(buf, "abcd", sizeof("abcd")) != 0) { |
| 60 | fprintf(stderr, "strlcpy: failed large buffer test (2b)"); | 75 | fprintf(stderr, "strlcpy: failed large buffer test (2b)\n"); |
| 61 | failures++; | 76 | failures++; |
| 62 | } | 77 | } |
| 63 | for (cp = buf + len + 1; cp < ep; cp++) { | 78 | for (cp = buf + len + 1; cp < ep; cp++) { |
| 64 | if (*cp != 'a') { | 79 | if (*cp != 'z') { |
| 65 | fprintf(stderr, "strlcpy: failed large buffer test (2c)"); | 80 | fprintf(stderr, |
| 81 | "strlcpy: failed large buffer test (2c)\n"); | ||
| 66 | failures++; | 82 | failures++; |
| 67 | break; | 83 | break; |
| 68 | } | 84 | } |
| 69 | } | 85 | } |
| 70 | 86 | ||
| 71 | /* Test copying large string to a small buffer. */ | 87 | /* Test copying large string to a small buffer. */ |
| 72 | memset(buf, 'a', bufsize); | 88 | memset(buf, 'z', bufsize); |
| 73 | memset(buf2, 'x', bufsize - 1); | 89 | memset(buf2, 'x', bufsize - 1); |
| 74 | buf2[bufsize - 1] = '\0'; | 90 | buf2[bufsize - 1] = '\0'; |
| 75 | len = strlcpy(buf, buf2, bufsize / 2); | 91 | len = strlcpy(buf, buf2, bufsize / 2); |
| 76 | if (len != bufsize - 1) { | 92 | if (len != bufsize - 1) { |
| 77 | fprintf(stderr, "strlcpy: failed small buffer test (3a)"); | 93 | fprintf(stderr, "strlcpy: failed small buffer test (3a)\n"); |
| 78 | failures++; | 94 | failures++; |
| 79 | } | 95 | } |
| 80 | /* Make sure we only wrote where expected. */ | 96 | /* Make sure we only wrote where expected. */ |
| 81 | len = (bufsize / 2) - 1; | 97 | len = (bufsize / 2) - 1; |
| 82 | if (memcmp(buf, buf2, len) != 0 || buf[len] != '\0') { | 98 | if (memcmp(buf, buf2, len) != 0 || buf[len] != '\0') { |
| 83 | fprintf(stderr, "strlcpy: failed small buffer test (3b)"); | 99 | fprintf(stderr, "strlcpy: failed small buffer test (3b)\n"); |
| 84 | failures++; | 100 | failures++; |
| 85 | } | 101 | } |
| 86 | for (cp = buf + len + 1; cp < ep; cp++) { | 102 | for (cp = buf + len + 1; cp < ep; cp++) { |
| 87 | if (*cp != 'a') { | 103 | if (*cp != 'z') { |
| 88 | fprintf(stderr, "strlcpy: failed small buffer test (3c)"); | 104 | fprintf(stderr, |
| 105 | "strlcpy: failed small buffer test (3c)\n"); | ||
| 89 | failures++; | 106 | failures++; |
| 90 | break; | 107 | break; |
| 91 | } | 108 | } |
| 92 | } | 109 | } |
| 93 | 110 | ||
| 94 | /* Test copying to a 1-byte buffer. */ | 111 | /* Test copying to a 1-byte buffer. */ |
| 95 | memset(buf, 'a', bufsize); | 112 | memset(buf, 'z', bufsize); |
| 96 | len = strlcpy(buf, "abcd", 1); | 113 | len = strlcpy(buf, "abcd", 1); |
| 97 | if (len != 4) { | 114 | if (len != 4) { |
| 98 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4a)"); | 115 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4a)\n"); |
| 99 | failures++; | 116 | failures++; |
| 100 | } | 117 | } |
| 101 | /* Make sure we only wrote where expected. */ | 118 | /* Make sure we only wrote where expected. */ |
| 102 | if (buf[0] != '\0') { | 119 | if (buf[0] != '\0') { |
| 103 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4b)"); | 120 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4b)\n"); |
| 104 | failures++; | 121 | failures++; |
| 105 | } | 122 | } |
| 106 | for (cp = buf + 1; cp < ep; cp++) { | 123 | for (cp = buf + 1; cp < ep; cp++) { |
| 107 | if (*cp != 'a') { | 124 | if (*cp != 'z') { |
| 108 | fprintf(stderr, "strlcpy: failed 1-byte buffer test (4c)"); | 125 | fprintf(stderr, |
| 126 | "strlcpy: failed 1-byte buffer test (4c)\n"); | ||
| 109 | failures++; | 127 | failures++; |
| 110 | break; | 128 | break; |
| 111 | } | 129 | } |
| 112 | } | 130 | } |
| 113 | 131 | ||
| 132 | /* | ||
| 133 | * The following tests should result in SIGSEGV, however some | ||
| 134 | * systems may erroneously report SIGBUS. | ||
| 135 | * These tests assume that strlcpy() is signal-safe. | ||
| 136 | */ | ||
| 137 | memset(&sa, 0, sizeof(sa)); | ||
| 138 | sigemptyset(&sa.sa_mask); | ||
| 139 | sa.sa_handler = handler; | ||
| 140 | sigaction(SIGSEGV, &sa, NULL); | ||
| 141 | sigaction(SIGBUS, &sa, NULL); | ||
| 142 | |||
| 143 | /* Test copying to a NULL buffer with non-zero size. */ | ||
| 144 | got_signal = 0; | ||
| 145 | if (sigsetjmp(jmpenv, 1) == 0) { | ||
| 146 | len = strlcpy(NULL, "abcd", sizeof(buf)); | ||
| 147 | fprintf(stderr, "strlcpy: failed NULL dst test (5a), " | ||
| 148 | "expected signal %d, got len %zu\n", SIGSEGV, len); | ||
| 149 | failures++; | ||
| 150 | } else if (got_signal != SIGSEGV) { | ||
| 151 | fprintf(stderr, "strlcpy: failed NULL dst test (5b), " | ||
| 152 | "expected signal %d, got %d\n", SIGSEGV, got_signal); | ||
| 153 | failures++; | ||
| 154 | } | ||
| 155 | |||
| 156 | /* Test copying from a NULL src. */ | ||
| 157 | got_signal = 0; | ||
| 158 | if (sigsetjmp(jmpenv, 1) == 0) { | ||
| 159 | len = strlcpy(buf, NULL, sizeof(buf)); | ||
| 160 | fprintf(stderr, "strlcpy: failed NULL src test (6a), " | ||
| 161 | "expected signal %d, got len %zu\n", SIGSEGV, len); | ||
| 162 | failures++; | ||
| 163 | } else if (got_signal != SIGSEGV) { | ||
| 164 | fprintf(stderr, "strlcpy: failed NULL src test (6b), " | ||
| 165 | "expected signal %d, got %d\n", SIGSEGV, got_signal); | ||
| 166 | failures++; | ||
| 167 | } | ||
| 168 | |||
| 114 | return failures; | 169 | return failures; |
| 115 | } | 170 | } |
