Skip to content

Commit

Permalink
Add basic regression tests for strchr() and strrchr().
Browse files Browse the repository at this point in the history
  • Loading branch information
vhankala committed Jul 24, 2021
1 parent 24bb5fc commit d187ad6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions regress/lib/libc/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions regress/lib/libc/strchr/Makefile
Original file line number Diff line number Diff line change
@@ -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 <bsd.regress.mk>
69 changes: 69 additions & 0 deletions regress/lib/libc/strchr/strchrtest.c
Original file line number Diff line number Diff line change
@@ -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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

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;
}

0 comments on commit d187ad6

Please sign in to comment.