xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/strcasestr-2.c (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // Test needle overflow in strcasestr 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 strcasestr
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 // There's no interceptor for strcasestr on Windows
9*7c3d14c8STreehugger Robot // XFAIL: win32
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot #define _GNU_SOURCE
12*7c3d14c8STreehugger Robot #include <assert.h>
13*7c3d14c8STreehugger Robot #include <string.h>
14*7c3d14c8STreehugger Robot #include <sanitizer/asan_interface.h>
15*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)16*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
17*7c3d14c8STreehugger Robot   char *r = 0;
18*7c3d14c8STreehugger Robot   char s1[] = "ab";
19*7c3d14c8STreehugger Robot   char s2[4] = "cba";
20*7c3d14c8STreehugger Robot   __asan_poison_memory_region ((char *)&s2[2], 2);
21*7c3d14c8STreehugger Robot   r = strcasestr(s1, s2);
22*7c3d14c8STreehugger Robot   assert(r == 0);
23*7c3d14c8STreehugger Robot   // CHECK:'s2' <== Memory access at offset {{[0-9]+}} partially overflows this variable
24*7c3d14c8STreehugger Robot   return 0;
25*7c3d14c8STreehugger Robot }
26