Navigation Menu

Skip to content

Commit

Permalink
Add strnlen test.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristaps committed Feb 26, 2018
1 parent 0e1e87d commit 4e3480b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile
Expand Up @@ -9,6 +9,7 @@ COMPATS = compat_err.c \
compat_strlcat.c \
compat_strlcpy.c \
compat_strndup.c \
compat_strnlen.c \
compat_strtonum.c
TESTS = test-__progname.c \
test-arc4random.c \
Expand All @@ -32,6 +33,7 @@ TESTS = test-__progname.c \
test-strlcat.c \
test-strlcpy.c \
test-strndup.c \
test-strnlen.c \
test-strtonum.c \
test-systrace.c \
test-zlib.c
Expand Down
31 changes: 31 additions & 0 deletions compat_strnlen.c
@@ -0,0 +1,31 @@
/* $OpenBSD$ */

/*
* Copyright (c) 2010 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 <string.h>

size_t
strnlen(const char *str, size_t maxlen)
{
const char *cp;

for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
;

return (size_t)(cp - str);
}
9 changes: 8 additions & 1 deletion configure
Expand Up @@ -266,6 +266,7 @@ runtest SOCK_NONBLOCK SOCK_NONBLOCK || true
runtest strlcat STRLCAT || true
runtest strlcpy STRLCPY || true
runtest strndup STRNDUP || true
runtest strnlen STRNLEN || true
runtest strtonum STRTONUM || true
runtest systrace SYSTRACE || true
runtest zlib ZLIB "" "-lz" || true
Expand Down Expand Up @@ -308,7 +309,8 @@ __HEREDOC__
${HAVE_RECALLOCARRAY} -eq 0 -o \
${HAVE_STRLCAT} -eq 0 -o \
${HAVE_STRLCPY} -eq 0 -o \
${HAVE_STRNDUP} -eq 0 ] \
${HAVE_STRNDUP} -eq 0 -o \
${HAVE_STRNLEN} -eq 0 ] \
&& echo "#include <sys/types.h>"

# Now we handle our HAVE_xxxx values.
Expand Down Expand Up @@ -342,6 +344,7 @@ cat << __HEREDOC__
#define HAVE_STRLCAT ${HAVE_STRLCAT}
#define HAVE_STRLCPY ${HAVE_STRLCPY}
#define HAVE_STRNDUP ${HAVE_STRNDUP}
#define HAVE_STRNLEN ${HAVE_STRNLEN}
#define HAVE_STRTONUM ${HAVE_STRTONUM}
#define HAVE_SYSTRACE ${HAVE_SYSTRACE}
#define HAVE_ZLIB ${HAVE_ZLIB}
Expand Down Expand Up @@ -427,6 +430,10 @@ if [ ${HAVE_STRNDUP} -eq 0 ]; then
echo "extern char *strndup(const char *, size_t);"
fi

if [ ${HAVE_STRNLEN} -eq 0 ]; then
echo "extern size_t strnlen(const char *, size_t);"
fi

if [ ${HAVE_STRTONUM} -eq 0 ]; then
echo "extern long long strtonum(const char *, long long, long long, const char **);"
fi
Expand Down
11 changes: 11 additions & 0 deletions test-strnlen.c
@@ -0,0 +1,11 @@
#include <string.h>

int
main(void)
{
const char *foo = "bar";
size_t sz;

sz = strnlen(foo, 1);
return(1 != sz);
}

0 comments on commit 4e3480b

Please sign in to comment.