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#include <stdlib.h>
10#ifdef _MSC_VER
11# include <malloc.h>
12#endif
13
14// MSVC provides _alloca instead of alloca.
15#if defined(_MSC_VER) && !defined(alloca)
16# define alloca _alloca
17#endif
18
19#if defined(__sun__) && defined(__svr4__)
20#include <alloca.h>
21#endif
22
23
24void f1_alloca() {
25 char *dynamic_buffer = (char *)alloca(200);
26 fprintf(stderr, format: "dynamic_buffer = %p\n", dynamic_buffer);
27 memset(s: dynamic_buffer, c: 'y', n: 200);
28 return;
29}
30
31static const int kDynamicArraySize = 200;
32
33void f1_vla() {
34 char dynamic_buffer[kDynamicArraySize];
35 fprintf(stderr, format: "dynamic_buffer = %p\n", dynamic_buffer);
36 memset(s: dynamic_buffer, c: 'y', n: kDynamicArraySize);
37 return;
38}
39
40void f2() {
41 char buf[1024];
42 memset(s: buf, c: 'x', n: 1024);
43}
44
45int main(int argc, const char *argv[]) {
46 if (!strcmp(s1: argv[1], s2: "1")) {
47 f1_alloca();
48 } else if (!strcmp(s1: argv[1], s2: "2")) {
49 f1_vla();
50 }
51 f2();
52 fprintf(stderr, format: "Done.\n");
53 return 0;
54}
55
56// CHECK-NOT: ERROR: AddressSanitizer
57// CHECK: Done.
58

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of compiler-rt/test/asan/TestCases/alloca_constant_size.cpp