strdup_oob_test.cc revision 2d1fdb26e458c4ddc04155c1d421bced3ba90cd0
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#include <string.h>
7
8char kString[] = "foo";
9
10int main(int argc, char **argv) {
11  char *copy = strdup(kString);
12  int x = copy[4 + argc];  // BOOM
13  // CHECK: AddressSanitizer: heap-buffer-overflow
14  // CHECK: #0 {{.*}}main {{.*}}strdup_oob_test.cc:[[@LINE-2]]
15  // CHECK: allocated by thread T{{.*}} here:
16  // CHECK: #0 {{.*}}strdup
17  // CHECK: strdup_oob_test.cc:[[@LINE-6]]
18  return x;
19}
20