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