xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/strchr_strict.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test strict_string_checks option in strchr function
2*7c3d14c8STreehugger Robot // RUN: %clang_asan %s -o %t && %run %t 2>&1
3*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=strict_string_checks=false %run %t 2>&1
4*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=strict_string_checks=true not %run %t 2>&1 | FileCheck %s
5*7c3d14c8STreehugger Robot 
6*7c3d14c8STreehugger Robot #include <assert.h>
7*7c3d14c8STreehugger Robot #include <stdlib.h>
8*7c3d14c8STreehugger Robot #include <string.h>
9*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)10*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
11*7c3d14c8STreehugger Robot   size_t size = 100;
12*7c3d14c8STreehugger Robot   char fill = 'o';
13*7c3d14c8STreehugger Robot   char *s = (char*)malloc(size);
14*7c3d14c8STreehugger Robot   memset(s, fill, size);
15*7c3d14c8STreehugger Robot   char c = 'o';
16*7c3d14c8STreehugger Robot   char* r = strchr(s, c);
17*7c3d14c8STreehugger Robot   // CHECK: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}}
18*7c3d14c8STreehugger Robot   // CHECK: READ of size 101
19*7c3d14c8STreehugger Robot   assert(r == s);
20*7c3d14c8STreehugger Robot   free(s);
21*7c3d14c8STreehugger Robot   return 0;
22*7c3d14c8STreehugger Robot }
23