summaryrefslogtreecommitdiff
path: root/src/regress/lib/libc/strchr/strchrtest.c
blob: 0be4c8df63fc3b5932fb06b6623d3f9be16e73be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*	$OpenBSD: strchrtest.c,v 1.2 2021/09/01 09:26:32 jasper 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;

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