diff options
author | millert <> | 2014-12-02 17:48:34 +0000 |
---|---|---|
committer | millert <> | 2014-12-02 17:48:34 +0000 |
commit | 28c71cd24b7756816d29f6704f72acde61ad5a8c (patch) | |
tree | 63364aa70534ddf7e0bdc437c50b1062b735afe2 /src | |
parent | 8ee336a82206d7cb3cd54eb6e4b0db55e613900b (diff) | |
download | openbsd-28c71cd24b7756816d29f6704f72acde61ad5a8c.tar.gz openbsd-28c71cd24b7756816d29f6704f72acde61ad5a8c.tar.bz2 openbsd-28c71cd24b7756816d29f6704f72acde61ad5a8c.zip |
Add simple strlcat regress
Diffstat (limited to 'src')
-rw-r--r-- | src/regress/lib/libc/Makefile | 4 | ||||
-rw-r--r-- | src/regress/lib/libc/strlcat/Makefile | 5 | ||||
-rw-r--r-- | src/regress/lib/libc/strlcat/strlcattest.c | 117 |
3 files changed, 124 insertions, 2 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 61424eaaf8..16bd49dcd6 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.44 2014/12/02 17:26:30 millert Exp $ | 1 | # $OpenBSD: Makefile,v 1.45 2014/12/02 17:48:34 millert Exp $ |
2 | 2 | ||
3 | SUBDIR+= _setjmp alloca arc4random-fork | 3 | SUBDIR+= _setjmp alloca arc4random-fork |
4 | SUBDIR+= atexit basename cephes cxa-atexit db dirname env | 4 | SUBDIR+= atexit basename cephes cxa-atexit db dirname env |
@@ -6,7 +6,7 @@ SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob | |||
6 | SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream | 6 | SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream |
7 | SUBDIR+= orientation popen printf | 7 | SUBDIR+= orientation popen printf |
8 | SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf | 8 | SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf |
9 | SUBDIR+= stdio_threading stpncpy strerror strnlen strtod strtol strtonum | 9 | SUBDIR+= stdio_threading stpncpy strerror strlcat strnlen strtod strtol strtonum |
10 | SUBDIR+= telldir time timingsafe vis | 10 | SUBDIR+= telldir time timingsafe vis |
11 | 11 | ||
12 | .if defined(REGRESS_FULL) || make(clean) || make(cleandir) || make(obj) | 12 | .if defined(REGRESS_FULL) || make(clean) || make(cleandir) || make(obj) |
diff --git a/src/regress/lib/libc/strlcat/Makefile b/src/regress/lib/libc/strlcat/Makefile new file mode 100644 index 0000000000..858cb22696 --- /dev/null +++ b/src/regress/lib/libc/strlcat/Makefile | |||
@@ -0,0 +1,5 @@ | |||
1 | # $OpenBSD: Makefile,v 1.1 2014/12/02 17:48:34 millert Exp $ | ||
2 | |||
3 | PROG= strlcattest | ||
4 | |||
5 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/strlcat/strlcattest.c b/src/regress/lib/libc/strlcat/strlcattest.c new file mode 100644 index 0000000000..cb3624e5a4 --- /dev/null +++ b/src/regress/lib/libc/strlcat/strlcattest.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* $OpenBSD: strlcattest.c,v 1.1 2014/12/02 17:48:34 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #include <sys/types.h> | ||
20 | |||
21 | #include <stdio.h> | ||
22 | #include <stdlib.h> | ||
23 | #include <string.h> | ||
24 | #include <unistd.h> | ||
25 | |||
26 | int main(int argc, char *argv[]) | ||
27 | { | ||
28 | char *buf, *cp, *ep; | ||
29 | int failures = 0; | ||
30 | size_t len, bufsize; | ||
31 | |||
32 | /* Enable malloc security options. */ | ||
33 | setenv("MALLOC_OPTIONS", "S", 0); | ||
34 | |||
35 | bufsize = getpagesize(); /* trigger guard pages easily */ | ||
36 | buf = malloc(bufsize); | ||
37 | if (buf == NULL) { | ||
38 | fprintf(stderr, "unable to allocate memory\n"); | ||
39 | return 1; | ||
40 | } | ||
41 | memset(buf, 'a', bufsize); | ||
42 | ep = buf + bufsize; | ||
43 | |||
44 | /* Test appending to an unterminated string. */ | ||
45 | len = strlcat(buf, "abcd", bufsize); | ||
46 | if (len != 4 + bufsize) { | ||
47 | fprintf(stderr, "strlcat: failed unterminated buffer test (1a)"); | ||
48 | failures++; | ||
49 | } | ||
50 | /* Make sure we only wrote where expected. */ | ||
51 | for (cp = buf; cp < ep; cp++) { | ||
52 | if (*cp != 'a') { | ||
53 | fprintf(stderr, "strlcat: failed unterminated buffer test (1b)"); | ||
54 | failures++; | ||
55 | break; | ||
56 | } | ||
57 | } | ||
58 | |||
59 | /* Test appending to a full string. */ | ||
60 | ep[-1] = '\0'; | ||
61 | len = strlcat(buf, "abcd", bufsize); | ||
62 | if (len != 4 + bufsize - 1) { | ||
63 | fprintf(stderr, "strlcat: failed full buffer test (2a)"); | ||
64 | failures++; | ||
65 | } | ||
66 | /* Make sure we only wrote where expected. */ | ||
67 | for (cp = buf; cp < ep - 1; cp++) { | ||
68 | if (*cp != 'a') { | ||
69 | fprintf(stderr, "strlcat: failed full buffer test (2b)"); | ||
70 | failures++; | ||
71 | break; | ||
72 | } | ||
73 | } | ||
74 | |||
75 | /* Test appending to an empty string. */ | ||
76 | ep[-1] = 'a'; | ||
77 | buf[0] = '\0'; | ||
78 | len = strlcat(buf, "abcd", bufsize); | ||
79 | if (len != 4) { | ||
80 | fprintf(stderr, "strlcat: failed empty buffer test (3a)"); | ||
81 | failures++; | ||
82 | } | ||
83 | /* Make sure we only wrote where expected. */ | ||
84 | if (memcmp(buf, "abcd", sizeof("abcd")) != 0) { | ||
85 | fprintf(stderr, "strlcat: failed empty buffer test (3b)"); | ||
86 | failures++; | ||
87 | } | ||
88 | for (cp = buf + len + 1; cp < ep; cp++) { | ||
89 | if (*cp != 'a') { | ||
90 | fprintf(stderr, "strlcat: failed empty buffer test (3c)"); | ||
91 | failures++; | ||
92 | break; | ||
93 | } | ||
94 | } | ||
95 | |||
96 | /* Test appending to a NUL-terminated string. */ | ||
97 | memcpy(buf, "abcd", sizeof("abcd")); | ||
98 | len = strlcat(buf, "efgh", bufsize); | ||
99 | if (len != 8) { | ||
100 | fprintf(stderr, "strlcat: failed empty buffer test (4a)"); | ||
101 | failures++; | ||
102 | } | ||
103 | /* Make sure we only wrote where expected. */ | ||
104 | if (memcmp(buf, "abcdefgh", sizeof("abcdefgh")) != 0) { | ||
105 | fprintf(stderr, "strlcat: failed empty buffer test (4b)"); | ||
106 | failures++; | ||
107 | } | ||
108 | for (cp = buf + len + 1; cp < ep; cp++) { | ||
109 | if (*cp != 'a') { | ||
110 | fprintf(stderr, "strlcat: failed empty buffer test (4c)"); | ||
111 | failures++; | ||
112 | break; | ||
113 | } | ||
114 | } | ||
115 | |||
116 | return failures; | ||
117 | } | ||