summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/strlcat/strlcattest.c
diff options
context:
space:
mode:
authormillert <>2014-12-02 17:48:34 +0000
committermillert <>2014-12-02 17:48:34 +0000
commit28c71cd24b7756816d29f6704f72acde61ad5a8c (patch)
tree63364aa70534ddf7e0bdc437c50b1062b735afe2 /src/regress/lib/libc/strlcat/strlcattest.c
parent8ee336a82206d7cb3cd54eb6e4b0db55e613900b (diff)
downloadopenbsd-28c71cd24b7756816d29f6704f72acde61ad5a8c.tar.gz
openbsd-28c71cd24b7756816d29f6704f72acde61ad5a8c.tar.bz2
openbsd-28c71cd24b7756816d29f6704f72acde61ad5a8c.zip
Add simple strlcat regress
Diffstat (limited to 'src/regress/lib/libc/strlcat/strlcattest.c')
-rw-r--r--src/regress/lib/libc/strlcat/strlcattest.c117
1 files changed, 117 insertions, 0 deletions
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
26int 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}