strdup_oob_test.cc revision d530d892b4958a9ae54e57472d5d0a0bae1f6ad8
1d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov// RUN: %clangxx_asan -O0 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
2d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov// RUN: %clangxx_asan -O1 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
3d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov// RUN: %clangxx_asan -O2 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
4d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov// RUN: %clangxx_asan -O3 %s -o %t && %t 2>&1 | %symbolize | FileCheck %s
5d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov
6d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov#include <string.h>
7d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov
8d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonovchar kString[] = "foo";
9d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov
10d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonovint main(int argc, char **argv) {
11d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  char *copy = strdup(kString);
12d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  int x = copy[4 + argc];  // BOOM
13d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  // CHECK: AddressSanitizer: heap-buffer-overflow
14d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  // CHECK: #0 {{.*}}main {{.*}}strdup_oob_test.cc:[[@LINE-2]]
15d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  // CHECK: allocated by thread T{{.*}} here:
16d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  // CHECK: #0 {{.*}}strdup
17d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  // CHECK: strdup_oob_test.cc:[[@LINE-6]]
18d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov  return x;
19d530d892b4958a9ae54e57472d5d0a0bae1f6ad8Alexey Samsonov}
20