From: visa Date: Sat, 24 Jul 2021 05:45:49 +0000 (+0000) Subject: Add basic regression tests for strchr() and strrchr(). X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=d187ad69882de8b9dfafcae909bed56a838d82cc;p=openbsd Add basic regression tests for strchr() and strrchr(). --- diff --git a/regress/lib/libc/Makefile b/regress/lib/libc/Makefile index 9ad31209619..7aeed7676af 100644 --- a/regress/lib/libc/Makefile +++ b/regress/lib/libc/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.56 2020/06/26 20:16:21 naddy Exp $ +# $OpenBSD: Makefile,v 1.57 2021/07/24 05:45:49 visa Exp $ SUBDIR+= _setjmp SUBDIR+= alloca arc4random-fork atexit @@ -18,7 +18,7 @@ SUBDIR+= popen printf SUBDIR+= qsort SUBDIR+= regex SUBDIR+= setjmp setjmp-signal sigsetjmp sigthr sleep sprintf stdio_threading -SUBDIR+= stpncpy strerror strlcat strlcpy strnlen strtod strtol strtonum +SUBDIR+= stpncpy strchr strerror strlcat strlcpy strnlen strtod strtol strtonum SUBDIR+= sys SUBDIR+= telldir time timingsafe SUBDIR+= vis diff --git a/regress/lib/libc/strchr/Makefile b/regress/lib/libc/strchr/Makefile new file mode 100644 index 00000000000..355c0d94210 --- /dev/null +++ b/regress/lib/libc/strchr/Makefile @@ -0,0 +1,8 @@ +# $OpenBSD: Makefile,v 1.1 2021/07/24 05:45:49 visa Exp $ + +PROG= strchrtest + +CFLAGS+= -fno-builtin-strchr +CFLAGS+= -fno-builtin-strrchr + +.include diff --git a/regress/lib/libc/strchr/strchrtest.c b/regress/lib/libc/strchr/strchrtest.c new file mode 100644 index 00000000000..64d61203059 --- /dev/null +++ b/regress/lib/libc/strchr/strchrtest.c @@ -0,0 +1,69 @@ +/* $OpenBSD: strchrtest.c,v 1.1 2021/07/24 05:45:49 visa Exp $ */ + +/* + * Copyright (c) 2021 Visa Hankala + * + * 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 +#include +#include +#include +#include + +int +main(void) +{ + char *buf; + size_t bufsize; + + /* Enable malloc security options. */ + setenv("MALLOC_OPTIONS", "S", 0); + + /* Allocate buffer with guard pages. */ + bufsize = getpagesize(); + buf = malloc(bufsize); + if (buf == NULL) { + fprintf(stderr, "unable to allocate memory\n"); + return 1; + } + + memset(buf, 0, bufsize); + assert(strchr(buf, 'a') == NULL); + assert(strchr(buf, '\0') == buf); + assert(strrchr(buf, 'a') == NULL); + assert(strrchr(buf, '\0') == buf); + + memcpy(buf, "haystack\xcf\x80", 10); + assert(strchr(buf, 'a') == buf + 1); + assert(strchr(buf, '\x80') == buf + 9); + assert(strchr(buf, 0x180) == buf + 9); + assert(strchr(buf, '\0') == buf + 10); + assert(strrchr(buf, 'a') == buf + 5); + assert(strrchr(buf, '\xcf') == buf + 8); + assert(strrchr(buf, 0x3cf) == buf + 8); + assert(strrchr(buf, '\0') == buf + 10); + + memset(buf, 'a', bufsize - 2); + buf[0] = 'b'; + buf[bufsize - 2] = 'b'; + assert(strchr(buf, 'b') == buf); + assert(strchr(buf, 'c') == NULL); + assert(strchr(buf, '\0') == buf + bufsize - 1); + assert(strrchr(buf, 'b') == buf + bufsize - 2); + assert(strrchr(buf, 'c') == NULL); + assert(strrchr(buf, '\0') == buf + bufsize - 1); + + return 0; +}