summaryrefslogtreecommitdiff
path: root/regress/lib/libc
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2014-12-02 17:48:35 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2014-12-02 17:48:35 +0000
commit65139ecff812714f9a6be25db10af22dd241bb5c (patch)
treee900608ed2b832ff12f5f516c8ad8f20866e48fd /regress/lib/libc
parent2060ef85370e5e2ed58965fc39a1d6f4ad6e0d2c (diff)
Add simple strlcat regress
Diffstat (limited to 'regress/lib/libc')
-rw-r--r--regress/lib/libc/Makefile4
-rw-r--r--regress/lib/libc/strlcat/Makefile5
-rw-r--r--regress/lib/libc/strlcat/strlcattest.c117
3 files changed, 124 insertions, 2 deletions
diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile
index 61424eaaf89..16bd49dcd6e 100644
--- a/regress/lib/libc/Makefile
+++ b/regress/lib/libc/Makefile
@@ -1,4 +1,4 @@
-# $OpenBSD: Makefile,v 1.44 2014/12/02 17:26:30 millert Exp $
+# $OpenBSD: Makefile,v 1.45 2014/12/02 17:48:34 millert Exp $
SUBDIR+= _setjmp alloca arc4random-fork
SUBDIR+= atexit basename cephes cxa-atexit db dirname env
@@ -6,7 +6,7 @@ SUBDIR+= explicit_bzero fmemopen fnmatch fpclassify getcap getopt_long glob
SUBDIR+= hsearch longjmp locale malloc mkstemp modf netdb open_memstream
SUBDIR+= orientation popen printf
SUBDIR+= regex setjmp setjmp-signal sigreturn sigsetjmp sprintf
-SUBDIR+= stdio_threading stpncpy strerror strnlen strtod strtol strtonum
+SUBDIR+= stdio_threading stpncpy strerror strlcat strnlen strtod strtol strtonum
SUBDIR+= telldir time timingsafe vis
.if defined(REGRESS_FULL) || make(clean) || make(cleandir) || make(obj)
diff --git a/regress/lib/libc/strlcat/Makefile b/regress/lib/libc/strlcat/Makefile
new file mode 100644
index 00000000000..858cb226967
--- /dev/null
+++ b/regress/lib/libc/strlcat/Makefile
@@ -0,0 +1,5 @@
+# $OpenBSD: Makefile,v 1.1 2014/12/02 17:48:34 millert Exp $
+
+PROG= strlcattest
+
+.include <bsd.regress.mk>
diff --git a/regress/lib/libc/strlcat/strlcattest.c b/regress/lib/libc/strlcat/strlcattest.c
new file mode 100644
index 00000000000..cb3624e5a44
--- /dev/null
+++ b/regress/lib/libc/strlcat/strlcattest.c
@@ -0,0 +1,117 @@
+/* $OpenBSD: strlcattest.c,v 1.1 2014/12/02 17:48:34 millert Exp $ */
+
+/*
+ * Copyright (c) 2014 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * 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 <unistd.h>
+
+int main(int argc, char *argv[])
+{
+ char *buf, *cp, *ep;
+ int failures = 0;
+ size_t len, bufsize;
+
+ /* Enable malloc security options. */
+ setenv("MALLOC_OPTIONS", "S", 0);
+
+ bufsize = getpagesize(); /* trigger guard pages easily */
+ buf = malloc(bufsize);
+ if (buf == NULL) {
+ fprintf(stderr, "unable to allocate memory\n");
+ return 1;
+ }
+ memset(buf, 'a', bufsize);
+ ep = buf + bufsize;
+
+ /* Test appending to an unterminated string. */
+ len = strlcat(buf, "abcd", bufsize);
+ if (len != 4 + bufsize) {
+ fprintf(stderr, "strlcat: failed unterminated buffer test (1a)");
+ failures++;
+ }
+ /* Make sure we only wrote where expected. */
+ for (cp = buf; cp < ep; cp++) {
+ if (*cp != 'a') {
+ fprintf(stderr, "strlcat: failed unterminated buffer test (1b)");
+ failures++;
+ break;
+ }
+ }
+
+ /* Test appending to a full string. */
+ ep[-1] = '\0';
+ len = strlcat(buf, "abcd", bufsize);
+ if (len != 4 + bufsize - 1) {
+ fprintf(stderr, "strlcat: failed full buffer test (2a)");
+ failures++;
+ }
+ /* Make sure we only wrote where expected. */
+ for (cp = buf; cp < ep - 1; cp++) {
+ if (*cp != 'a') {
+ fprintf(stderr, "strlcat: failed full buffer test (2b)");
+ failures++;
+ break;
+ }
+ }
+
+ /* Test appending to an empty string. */
+ ep[-1] = 'a';
+ buf[0] = '\0';
+ len = strlcat(buf, "abcd", bufsize);
+ if (len != 4) {
+ fprintf(stderr, "strlcat: failed empty buffer test (3a)");
+ failures++;
+ }
+ /* Make sure we only wrote where expected. */
+ if (memcmp(buf, "abcd", sizeof("abcd")) != 0) {
+ fprintf(stderr, "strlcat: failed empty buffer test (3b)");
+ failures++;
+ }
+ for (cp = buf + len + 1; cp < ep; cp++) {
+ if (*cp != 'a') {
+ fprintf(stderr, "strlcat: failed empty buffer test (3c)");
+ failures++;
+ break;
+ }
+ }
+
+ /* Test appending to a NUL-terminated string. */
+ memcpy(buf, "abcd", sizeof("abcd"));
+ len = strlcat(buf, "efgh", bufsize);
+ if (len != 8) {
+ fprintf(stderr, "strlcat: failed empty buffer test (4a)");
+ failures++;
+ }
+ /* Make sure we only wrote where expected. */
+ if (memcmp(buf, "abcdefgh", sizeof("abcdefgh")) != 0) {
+ fprintf(stderr, "strlcat: failed empty buffer test (4b)");
+ failures++;
+ }
+ for (cp = buf + len + 1; cp < ep; cp++) {
+ if (*cp != 'a') {
+ fprintf(stderr, "strlcat: failed empty buffer test (4c)");
+ failures++;
+ break;
+ }
+ }
+
+ return failures;
+}