• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // UNSUPPORTED: android
2 // RUN: %clang -w -fsanitize=nullability-arg,nullability-assign,nullability-return %s -O3 -o %t
3 // RUN: %run %t foo 2>&1 | FileCheck %s --check-prefix=NOERROR --allow-empty --implicit-check-not='runtime error'
4 // RUN: %run %t 2>&1 | FileCheck %s
5 
6 // RUN: echo "nullability-arg:nullability.c" > %t.supp
7 // RUN: echo "nullability-return:nullability.c" >> %t.supp
8 // RUN: echo "nullability-assign:nullability.c" >> %t.supp
9 // RUN: env UBSAN_OPTIONS=suppressions='"%t.supp"' %run %t 2>&1 | FileCheck -allow-empty -check-prefix=SUPPRESS %s
10 // SUPPRESS-NOT: runtime error
11 
12 // CHECK: nullability.c:[[@LINE+2]]:41: runtime error: null pointer returned from function declared to never return null
13 // CHECK-NEXT: nullability.c:[[@LINE+1]]:6: note: _Nonnull return type annotation specified here
nonnull_retval1(int * p)14 int *_Nonnull nonnull_retval1(int *p) { return p; }
15 
16 // CHECK: nullability.c:1001:22: runtime error: null pointer passed as argument 2, which is declared to never be null
17 // CHECK-NEXT: nullability.c:[[@LINE+1]]:56: note: _Nonnull type annotation specified here
nonnull_retval2(int * _Nonnull arg1,int * _Nonnull arg2,int * _Nullable arg3,int * arg4,int arg5,...)18 int *_Nonnull nonnull_retval2(int *_Nonnull arg1, int *_Nonnull arg2,
19                               int *_Nullable arg3, int *arg4, int arg5, ...) {
20   return arg1;
21 }
22 
23 // CHECK: nullability.c:1002:15: runtime error: null pointer passed as argument 1, which is declared to never be null
24 // CHECK-NEXT: nullability.c:[[@LINE+1]]:23: note: _Nonnull type annotation specified here
nonnull_arg(int * _Nonnull p)25 void nonnull_arg(int *_Nonnull p) {}
26 
nonnull_assign1(int * p)27 void nonnull_assign1(int *p) {
28   int *_Nonnull local;
29 // CHECK: nullability.c:[[@LINE+1]]:9: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
30   local = p;
31 }
32 
nonnull_assign2(int * p)33 void nonnull_assign2(int *p) {
34   int *_Nonnull arr[1];
35   // CHECK: nullability.c:[[@LINE+1]]:10: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
36   arr[0] = p;
37 }
38 
39 struct S1 {
40   int *_Nonnull mptr;
41 };
42 
nonnull_assign3(int * p)43 void nonnull_assign3(int *p) {
44   struct S1 s;
45   // CHECK: nullability.c:[[@LINE+1]]:10: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
46   s.mptr = p;
47 }
48 
49 // CHECK: nullability.c:[[@LINE+1]]:52: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
nonnull_init1(int * p)50 void nonnull_init1(int *p) { int *_Nonnull local = p; }
51 
52 // CHECK: nullability.c:[[@LINE+2]]:53: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
53 // CHECK: nullability.c:[[@LINE+1]]:56: runtime error: _Nonnull binding to null pointer of type 'int * _Nonnull'
nonnull_init2(int * p)54 void nonnull_init2(int *p) { int *_Nonnull arr[] = {p, p}; }
55 
main(int argc,char ** argv)56 int main(int argc, char **argv) {
57   int *p = (argc > 1) ? &argc : ((int *)0);
58 
59 #line 1000
60   nonnull_retval1(p);
61   nonnull_retval2(p, p, p, p, 0, 0, 0, 0);
62   nonnull_arg(p);
63   nonnull_assign1(p);
64   nonnull_assign2(p);
65   nonnull_assign3(p);
66   nonnull_init1(p);
67   nonnull_init2(p);
68   return 0;
69 }
70 
71 // NOERROR-NOT: runtime error
72