Home | History | Annotate | Download | only in TestCases
      1 // Regression test for https://github.com/google/sanitizers/issues/691
      2 
      3 // RUN: %clangxx_asan -O0 %s -o %t -fstack-protector
      4 // RUN: %run %t 1 2>&1 | FileCheck %s
      5 // RUN: %run %t 2 2>&1 | FileCheck %s
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 
     10 // MSVC provides _alloca instead of alloca.
     11 #if defined(_MSC_VER) && !defined(alloca)
     12 # define alloca _alloca
     13 #else
     14 #include <alloca.h>
     15 #endif
     16 
     17 
     18 void f1_alloca() {
     19   char *dynamic_buffer = (char *)alloca(200);
     20   fprintf(stderr, "dynamic_buffer = %p\n", dynamic_buffer);
     21   memset(dynamic_buffer, 'y', 200);
     22   return;
     23 }
     24 
     25 static const int kDynamicArraySize = 200;
     26 
     27 void f1_vla() {
     28   char dynamic_buffer[kDynamicArraySize];
     29   fprintf(stderr, "dynamic_buffer = %p\n", dynamic_buffer);
     30   memset(dynamic_buffer, 'y', kDynamicArraySize);
     31   return;
     32 }
     33 
     34 void f2() {
     35   char buf[1024];
     36   memset(buf, 'x', 1024);
     37 }
     38 
     39 int main(int argc, const char *argv[]) {
     40   if (!strcmp(argv[1], "1")) {
     41     f1_alloca();
     42   } else if (!strcmp(argv[1], "2")) {
     43     f1_vla();
     44   }
     45   f2();
     46   fprintf(stderr, "Done.\n");
     47   return 0;
     48 }
     49 
     50 // CHECK-NOT: ERROR: AddressSanitizer
     51 // CHECK: Done.
     52