blob: 084dedc27b854df78949b4844a4a255872e200c2 [file] [log] [blame]
Stephen Hines86277eb2015-03-23 12:06:32 -07001// RUN: %clangxx -fsanitize=nonnull-attribute -fno-sanitize-recover=all %s -O3 -o %t
Stephen Hines6d186232014-11-26 17:56:19 -08002// RUN: %run %t nc
3// RUN: %run %t nm
4// RUN: %run %t nf
5// RUN: %run %t nv
6// RUN: not %run %t 0c 2>&1 | FileCheck %s --check-prefix=CTOR
7// RUN: not %run %t 0m 2>&1 | FileCheck %s --check-prefix=METHOD
8// RUN: not %run %t 0f 2>&1 | FileCheck %s --check-prefix=FUNC
9// RUN: not %run %t 0v 2>&1 | FileCheck %s --check-prefix=VARIADIC
10
11class C {
12 int *null_;
13 int *nonnull_;
14
15public:
16 C(int *null, __attribute__((nonnull)) int *nonnull)
17 : null_(null), nonnull_(nonnull) {}
18 int value() { return *nonnull_; }
19 int method(int *nonnull, int *null) __attribute__((nonnull(2))) {
20 return *nonnull_ + *nonnull;
21 }
22};
23
24__attribute__((nonnull)) int func(int *nonnull) { return *nonnull; }
25
26#include <stdarg.h>
27__attribute__((nonnull)) int variadic(int x, ...) {
28 va_list args;
29 va_start(args, x);
30 int *nonnull = va_arg(args, int*);
31 int res = *nonnull;
32 va_end(args);
33 return res;
34}
35
36int main(int argc, char *argv[]) {
37 int local = 0;
38 int *arg = (argv[1][0] == '0') ? 0x0 : &local;
39 switch (argv[1][1]) {
40 case 'c':
41 return C(0x0, arg).value();
42 // CTOR: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:21: runtime error: null pointer passed as argument 2, which is declared to never be null
43 // CTOR-NEXT: {{.*}}nonnull-arg.cpp:16:31: note: nonnull attribute specified here
44 case 'm':
45 return C(0x0, &local).method(arg, 0x0);
46 // METHOD: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:36: runtime error: null pointer passed as argument 1, which is declared to never be null
47 // METHOD-NEXT: {{.*}}nonnull-arg.cpp:19:54: note: nonnull attribute specified here
48 case 'f':
49 return func(arg);
50 // FUNC: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:19: runtime error: null pointer passed as argument 1, which is declared to never be null
51 // FUNC-NEXT: {{.*}}nonnull-arg.cpp:24:16: note: nonnull attribute specified here
52 case 'v':
53 return variadic(42, arg);
54 // VARIADIC: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:27: runtime error: null pointer passed as argument 2, which is declared to never be null
55 // VARIADIC-NEXT: {{.*}}nonnull-arg.cpp:27:16: note: nonnull attribute specified here
56 }
57 return 0;
58}