strncpy-overflow.cc revision 6a211c5814e25d6745a5058cc0e499e5235d3821
1// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
2// RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
3// RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
4// RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-%os --check-prefix=CHECK
5
6// REQUIRES: compiler-rt-optimized
7// XFAIL: arm-linux-gnueabi
8
9#include <string.h>
10#include <stdlib.h>
11int 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-Linux: {{    #0 0x.* in .*strncpy}}
18  // CHECK-Darwin: {{    #0 0x.* in wrap_strncpy}}
19  // CHECK: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-4]]
20  // CHECK: {{0x.* is located 0 bytes to the right of 9-byte region}}
21  // CHECK: {{allocated by thread T0 here:}}
22
23  // CHECK-Linux: {{    #0 0x.* in .*malloc}}
24  // CHECK-Linux: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-10]]
25
26  // CHECK-Darwin: {{    #0 0x.* in wrap_malloc.*}}
27  // CHECK-Darwin: {{    #1 0x.* in main .*strncpy-overflow.cc:}}[[@LINE-13]]
28  return short_buffer[8];
29}
30