xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/strstr-2.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test needle overflow in strstr function
2*7c3d14c8STreehugger Robot // RUN: %clang_asan %s -o %t && %env_asan_opts=strict_string_checks=true not %run %t 2>&1 | FileCheck %s
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // Test intercept_strstr asan option
5*7c3d14c8STreehugger Robot // Disable other interceptors because strlen may be called inside strstr
6*7c3d14c8STreehugger Robot // RUN: %env_asan_opts=intercept_strstr=false:replace_str=false:intercept_strlen=false %run %t 2>&1
7*7c3d14c8STreehugger Robot 
8*7c3d14c8STreehugger Robot #include <assert.h>
9*7c3d14c8STreehugger Robot #include <string.h>
10*7c3d14c8STreehugger Robot #include <sanitizer/asan_interface.h>
11*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)12*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
13*7c3d14c8STreehugger Robot   char *r = 0;
14*7c3d14c8STreehugger Robot   char s1[] = "ab";
15*7c3d14c8STreehugger Robot   char s2[4] = "cab";
16*7c3d14c8STreehugger Robot   __asan_poison_memory_region ((char *)&s2[2], 2);
17*7c3d14c8STreehugger Robot   r = strstr(s1, s2);
18*7c3d14c8STreehugger Robot   // CHECK:'s2' <== Memory access at offset {{[0-9]+}} partially overflows this variable
19*7c3d14c8STreehugger Robot   assert(r == 0);
20*7c3d14c8STreehugger Robot   return 0;
21*7c3d14c8STreehugger Robot }
22