1// Test strchr for strict_string_checks=false does not look beyond necessary
2// char.
3// RUN: %clang_asan %s -o %t
4// RUN: %env_asan_opts=strict_string_checks=false %run %t 2>&1
5// RUN: %env_asan_opts=strict_string_checks=true not %run %t 2>&1 | FileCheck %s
6
7#include <assert.h>
8#include <stdint.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/mman.h>
12#include <unistd.h>
13
14int main(int argc, char **argv) {
15 size_t page_size = sysconf(_SC_PAGE_SIZE);
16 size_t size = 2 * page_size;
17 char *s = (char *)mmap(addr: 0, len: size, PROT_READ | PROT_WRITE,
18 MAP_PRIVATE | MAP_ANONYMOUS, fd: -1, offset: 0);
19 assert(s);
20 assert(((uintptr_t)s & (page_size - 1)) == 0);
21 memset(s: s, c: 'o', n: size);
22 s[size - 1] = 0;
23
24 char *p = s + page_size - 1;
25 *p = 'x';
26
27 if (mprotect(addr: p + 1, len: 1, PROT_NONE))
28 return 1;
29 char *r = strchr(s: s, c: 'x');
30 // CHECK: AddressSanitizer: {{SEGV|BUS}} on unknown address
31 assert(r == p);
32
33 return 0;
34}
35

source code of compiler-rt/test/asan/TestCases/Posix/strchr.c