xref: /llvm-project/compiler-rt/test/asan/TestCases/strstr_strict.c (revision abd09754edf156bf8c36ac6f52bf003706977b03)
1 // Test strict_string_checks option in strstr function
2 // RUN: %clang_asan %s -o %t && %run %t 2>&1
3 
4 // Newer versions of Android's strstr() uses memchr() internally, which actually
5 // does trigger a heap-buffer-overflow (as it tries to find the
6 // null-terminator). The same applies to FreeBSD.
7 // UNSUPPORTED: android, target={{.*freebsd.*}}
8 // RUN: %env_asan_opts=strict_string_checks=false %run %t 2>&1
9 
10 // RUN: %env_asan_opts=strict_string_checks=true not %run %t 2>&1 | FileCheck %s
11 
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
main(int argc,char ** argv)16 int main(int argc, char **argv) {
17   size_t size = 100;
18   char fill = 'o';
19   char *s1 = (char*)malloc(size);
20   char *s2 = (char*)malloc(size);
21   memset(s1, fill, size);
22   memset(s2, fill, size);
23   s2[size - 1]='\0';
24   char* r = strstr(s1, s2);
25   // CHECK: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}}
26   // CHECK: READ of size {{101|100}}
27   assert(r == s1);
28   free(s1);
29   free(s2);
30   return 0;
31 }
32