xref: /llvm-project/compiler-rt/test/asan/TestCases/strncpy-overflow.cpp (revision 23cd8d51ad519261137a40a5bbac6e537ee7ba25)
1 // RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
2 // RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
3 // RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
4 // RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s
5 
6 // REQUIRES: compiler-rt-optimized
7 // REQUIRES: stable-runtime
8 
9 #include <string.h>
10 #include <stdlib.h>
11 int main(int argc, char **argv) {
12   char *hello = (char*)malloc(6);
13   strcpy(hello, "hello");
14   char *short_buffer = (char*)malloc(9);
15   strncpy(short_buffer, hello, 10);  // BOOM
16   // CHECK: {{WRITE of size 10 at 0x.* thread T0}}
17   // CHECK: {{    #0 0x.* in .*strncpy}}
18   // CHECK: {{    #1 0x.* in main .*strncpy-overflow.cpp:}}[[@LINE-3]]
19   // CHECK: {{0x.* is located 0 bytes to the right of 9-byte region}}
20   // CHECK: {{allocated by thread T0 here:}}
21   // CHECK: {{    #0 0x.* in .*malloc}}
22   // CHECK: {{    #1 0x.* in main .*strncpy-overflow.cpp:}}[[@LINE-8]]
23   return short_buffer[8];
24 }
25