xref: /aosp_15_r20/external/compiler-rt/test/asan/TestCases/strncpy-overflow.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
2*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
3*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
4*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
5*7c3d14c8STreehugger Robot 
6*7c3d14c8STreehugger Robot // REQUIRES: compiler-rt-optimized
7*7c3d14c8STreehugger Robot // XFAIL: arm-linux-gnueabi
8*7c3d14c8STreehugger Robot // XFAIL: armv7l-unknown-linux-gnueabihf
9*7c3d14c8STreehugger Robot 
10*7c3d14c8STreehugger Robot #include <string.h>
11*7c3d14c8STreehugger Robot #include <stdlib.h>
main(int argc,char ** argv)12*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
13*7c3d14c8STreehugger Robot   char *hello = (char*)malloc(6);
14*7c3d14c8STreehugger Robot   strcpy(hello, "hello");
15*7c3d14c8STreehugger Robot   char *short_buffer = (char*)malloc(9);
16*7c3d14c8STreehugger Robot   strncpy(short_buffer, hello, 10);  // BOOM
17*7c3d14c8STreehugger Robot   // CHECK: {{WRITE of size 10 at 0x.* thread T0}}
18*7c3d14c8STreehugger Robot   // CHECK-Linux: {{    #0 0x.* in .*strncpy}}
19*7c3d14c8STreehugger Robot   // CHECK-Darwin: {{    #0 0x.* in wrap_strncpy}}
20*7c3d14c8STreehugger Robot   // CHECK: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-4]]
21*7c3d14c8STreehugger Robot   // CHECK: {{0x.* is located 0 bytes to the right of 9-byte region}}
22*7c3d14c8STreehugger Robot   // CHECK: {{allocated by thread T0 here:}}
23*7c3d14c8STreehugger Robot 
24*7c3d14c8STreehugger Robot   // CHECK-Linux: {{    #0 0x.* in .*malloc}}
25*7c3d14c8STreehugger Robot   // CHECK-Linux: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-10]]
26*7c3d14c8STreehugger Robot 
27*7c3d14c8STreehugger Robot   // CHECK-Darwin: {{    #0 0x.* in wrap_malloc.*}}
28*7c3d14c8STreehugger Robot   // CHECK-Darwin: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-13]]
29*7c3d14c8STreehugger Robot   return short_buffer[8];
30*7c3d14c8STreehugger Robot }
31