strdup_oob_test.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
12d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx_asan -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s
22d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx_asan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
32d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx_asan -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s
42d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines// RUN: %clangxx_asan -O3 %s -o %t && not %run %t 2>&1 | 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