diff options
| author | visa <> | 2021-07-24 05:45:49 +0000 |
|---|---|---|
| committer | visa <> | 2021-07-24 05:45:49 +0000 |
| commit | 00ba27f75e54357b61e5e574cd3133dfbffa8a19 (patch) | |
| tree | c5e925eb92f866ab5612b60ab96e979eaafefbc7 /src | |
| parent | fca33282dd6c3b1234ade66354819e05a3295020 (diff) | |
| download | openbsd-00ba27f75e54357b61e5e574cd3133dfbffa8a19.tar.gz openbsd-00ba27f75e54357b61e5e574cd3133dfbffa8a19.tar.bz2 openbsd-00ba27f75e54357b61e5e574cd3133dfbffa8a19.zip | |
Add basic regression tests for strchr() and strrchr().
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libc/Makefile | 4 | ||||
| -rw-r--r-- | src/regress/lib/libc/strchr/Makefile | 8 | ||||
| -rw-r--r-- | src/regress/lib/libc/strchr/strchrtest.c | 69 |
3 files changed, 79 insertions, 2 deletions
diff --git a/src/regress/lib/libc/Makefile b/src/regress/lib/libc/Makefile index 9ad3120961..7aeed7676a 100644 --- a/src/regress/lib/libc/Makefile +++ b/src/regress/lib/libc/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.56 2020/06/26 20:16:21 naddy Exp $ | 1 | # $OpenBSD: Makefile,v 1.57 2021/07/24 05:45:49 visa Exp $ |
| 2 | 2 | ||
| 3 | SUBDIR+= _setjmp | 3 | SUBDIR+= _setjmp |
| 4 | SUBDIR+= alloca arc4random-fork atexit | 4 | SUBDIR+= alloca arc4random-fork atexit |
| @@ -18,7 +18,7 @@ SUBDIR+= popen printf | |||
| 18 | SUBDIR+= qsort | 18 | SUBDIR+= qsort |
| 19 | SUBDIR+= regex | 19 | SUBDIR+= regex |
| 20 | SUBDIR+= setjmp setjmp-signal sigsetjmp sigthr sleep sprintf stdio_threading | 20 | SUBDIR+= setjmp setjmp-signal sigsetjmp sigthr sleep sprintf stdio_threading |
| 21 | SUBDIR+= stpncpy strerror strlcat strlcpy strnlen strtod strtol strtonum | 21 | SUBDIR+= stpncpy strchr strerror strlcat strlcpy strnlen strtod strtol strtonum |
| 22 | SUBDIR+= sys | 22 | SUBDIR+= sys |
| 23 | SUBDIR+= telldir time timingsafe | 23 | SUBDIR+= telldir time timingsafe |
| 24 | SUBDIR+= vis | 24 | SUBDIR+= vis |
diff --git a/src/regress/lib/libc/strchr/Makefile b/src/regress/lib/libc/strchr/Makefile new file mode 100644 index 0000000000..355c0d9421 --- /dev/null +++ b/src/regress/lib/libc/strchr/Makefile | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.1 2021/07/24 05:45:49 visa Exp $ | ||
| 2 | |||
| 3 | PROG= strchrtest | ||
| 4 | |||
| 5 | CFLAGS+= -fno-builtin-strchr | ||
| 6 | CFLAGS+= -fno-builtin-strrchr | ||
| 7 | |||
| 8 | .include <bsd.regress.mk> | ||
diff --git a/src/regress/lib/libc/strchr/strchrtest.c b/src/regress/lib/libc/strchr/strchrtest.c new file mode 100644 index 0000000000..64d6120305 --- /dev/null +++ b/src/regress/lib/libc/strchr/strchrtest.c | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | /* $OpenBSD: strchrtest.c,v 1.1 2021/07/24 05:45:49 visa Exp $ */ | ||
| 2 | |||
| 3 | /* | ||
| 4 | * Copyright (c) 2021 Visa Hankala | ||
| 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 <assert.h> | ||
| 20 | #include <stdio.h> | ||
| 21 | #include <stdlib.h> | ||
| 22 | #include <string.h> | ||
| 23 | #include <unistd.h> | ||
| 24 | |||
| 25 | int | ||
| 26 | main(void) | ||
| 27 | { | ||
| 28 | char *buf; | ||
| 29 | size_t bufsize; | ||
| 30 | |||
| 31 | /* Enable malloc security options. */ | ||
| 32 | setenv("MALLOC_OPTIONS", "S", 0); | ||
| 33 | |||
| 34 | /* Allocate buffer with guard pages. */ | ||
| 35 | bufsize = getpagesize(); | ||
| 36 | buf = malloc(bufsize); | ||
| 37 | if (buf == NULL) { | ||
| 38 | fprintf(stderr, "unable to allocate memory\n"); | ||
| 39 | return 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | memset(buf, 0, bufsize); | ||
| 43 | assert(strchr(buf, 'a') == NULL); | ||
| 44 | assert(strchr(buf, '\0') == buf); | ||
| 45 | assert(strrchr(buf, 'a') == NULL); | ||
| 46 | assert(strrchr(buf, '\0') == buf); | ||
| 47 | |||
| 48 | memcpy(buf, "haystack\xcf\x80", 10); | ||
| 49 | assert(strchr(buf, 'a') == buf + 1); | ||
| 50 | assert(strchr(buf, '\x80') == buf + 9); | ||
| 51 | assert(strchr(buf, 0x180) == buf + 9); | ||
| 52 | assert(strchr(buf, '\0') == buf + 10); | ||
| 53 | assert(strrchr(buf, 'a') == buf + 5); | ||
| 54 | assert(strrchr(buf, '\xcf') == buf + 8); | ||
| 55 | assert(strrchr(buf, 0x3cf) == buf + 8); | ||
| 56 | assert(strrchr(buf, '\0') == buf + 10); | ||
| 57 | |||
| 58 | memset(buf, 'a', bufsize - 2); | ||
| 59 | buf[0] = 'b'; | ||
| 60 | buf[bufsize - 2] = 'b'; | ||
| 61 | assert(strchr(buf, 'b') == buf); | ||
| 62 | assert(strchr(buf, 'c') == NULL); | ||
| 63 | assert(strchr(buf, '\0') == buf + bufsize - 1); | ||
| 64 | assert(strrchr(buf, 'b') == buf + bufsize - 2); | ||
| 65 | assert(strrchr(buf, 'c') == NULL); | ||
| 66 | assert(strrchr(buf, '\0') == buf + bufsize - 1); | ||
| 67 | |||
| 68 | return 0; | ||
| 69 | } | ||
