1*c13ff7acSjasper /* $OpenBSD: strnlentest.c,v 1.3 2021/09/01 09:26:32 jasper Exp $ */
2a5f0e024Smillert
3a5f0e024Smillert /*
4bf198cc6Smillert * Copyright (c) 2010 Todd C. Miller <millert@openbsd.org>
5a5f0e024Smillert *
6a5f0e024Smillert * Permission to use, copy, modify, and distribute this software for any
7a5f0e024Smillert * purpose with or without fee is hereby granted, provided that the above
8a5f0e024Smillert * copyright notice and this permission notice appear in all copies.
9a5f0e024Smillert *
10a5f0e024Smillert * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11a5f0e024Smillert * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12a5f0e024Smillert * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13a5f0e024Smillert * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14a5f0e024Smillert * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15a5f0e024Smillert * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16a5f0e024Smillert * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17a5f0e024Smillert */
18a5f0e024Smillert
19a5f0e024Smillert #include <sys/types.h>
20a5f0e024Smillert
21a5f0e024Smillert #include <stdio.h>
22a5f0e024Smillert #include <stdlib.h>
23a5f0e024Smillert #include <string.h>
24a5f0e024Smillert #include <unistd.h>
25a5f0e024Smillert
main(int argc,char * argv[])26a5f0e024Smillert int main(int argc, char *argv[])
27a5f0e024Smillert {
28a5f0e024Smillert char *buf;
29a5f0e024Smillert int failures = 0;
30a5f0e024Smillert size_t len, bufsize;
31a5f0e024Smillert
32a5f0e024Smillert bufsize = getpagesize(); /* trigger guard pages easily */
33a5f0e024Smillert buf = malloc(bufsize);
34a5f0e024Smillert if (buf == NULL) {
35a5f0e024Smillert fprintf(stderr, "unable to allocate memory\n");
36a5f0e024Smillert return 1;
37a5f0e024Smillert }
38a5f0e024Smillert memset(buf, 'a', bufsize);
39a5f0e024Smillert
40a5f0e024Smillert len = strnlen(buf, bufsize);
41a5f0e024Smillert if (len != bufsize) {
42a5f0e024Smillert fprintf(stderr, "strnlen: failed unterminated buffer test (1)");
43a5f0e024Smillert failures++;
44a5f0e024Smillert }
45a5f0e024Smillert
46a5f0e024Smillert len = strnlen(buf, bufsize / 2);
47a5f0e024Smillert if (len != bufsize / 2) {
48a5f0e024Smillert fprintf(stderr, "strnlen: failed unterminated buffer test (2)");
49a5f0e024Smillert failures++;
50a5f0e024Smillert }
51a5f0e024Smillert
52a5f0e024Smillert buf[bufsize - 1] = '\0';
53a5f0e024Smillert len = strnlen(buf, bufsize);
54a5f0e024Smillert if (len != bufsize - 1) {
55a5f0e024Smillert fprintf(stderr, "strnlen: failed NUL-terminated buffer test (1)");
56a5f0e024Smillert failures++;
57a5f0e024Smillert }
58a5f0e024Smillert
59a5f0e024Smillert len = strnlen(buf, (size_t)-1);
60a5f0e024Smillert if (len != bufsize - 1) {
61a5f0e024Smillert fprintf(stderr, "strnlen: failed NUL-terminated buffer test (2)");
62a5f0e024Smillert failures++;
63a5f0e024Smillert }
64a5f0e024Smillert
65a5f0e024Smillert return failures;
66a5f0e024Smillert }
67