xref: /llvm-project/clang/test/Analysis/PR46264.cpp (revision 0f1c1be1968076d6f96f8a7bcc4a15cf195ecd97)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
2 
3 struct A {
4   int a;
5   struct {
6     struct {
7       int b;
8       union {
9         int c;
10       };
11     };
12   };
13 };
14 
testCrash()15 int testCrash() {
16   int *x = 0;
17   int A::*ap = &A::a;
18 
19   if (ap)      // no crash
20     return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
21 
22   return 10;
23 }
24 
testIndirectCrash()25 int testIndirectCrash() {
26   int *x = 0;
27   int A::*cp = &A::c;
28 
29   if (cp)      // no crash
30     return *x; // expected-warning{{Dereference of null pointer (loaded from variable 'x')}}
31 
32   return 10;
33 }
34 
35 // PR46264
36 // This case shall not crash with an assertion failure about void* dereferening.
37 namespace ns1 {
38 namespace a {
39 class b {
40 public:
41   typedef int b::*c;
operator c()42   operator c() { return d ? &b::d : 0; }
43   int d;
44 };
45 } // namespace a
46 using a::b;
47 class e {
48   void f();
49   void g();
50   b h;
51 };
f()52 void e::f() {
53   e *i;
54   if (h)
55     i->g(); // expected-warning{{Called C++ object pointer is uninitialized}}
56 }
57 } // namespace ns1
58