1 // RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus.NewDelete,cplusplus.NewDeleteLeaks,debug.ExprInspection -analyzer-config c++-allocator-inlining=true -std=c++11 -verify %s
3 void clang_analyzer_eval(bool);
4 void clang_analyzer_dump(int);
6 typedef __typeof__(sizeof(int)) size_t;
15 // Recursive operator kinda placement new.
16 void *operator new(size_t size, S *place);
18 enum class ConstructionKind : char {
29 S(ConstructionKind k) {
31 case ConstructionKind::Recursive: { // Call one more operator new 'r'ecursively.
32 S *s = new (nullptr) S(5);
37 case ConstructionKind::Garbage: {
38 // Leaves garbage in 'x'.
45 // Do not try this at home!
46 void *operator new(size_t size, S *place) {
52 void testThatCharConstructorIndeedYieldsGarbage() {
53 S *s = new S(ConstructionKind::Garbage);
54 clang_analyzer_eval(s->x == 0); // expected-warning{{UNKNOWN}}
55 clang_analyzer_eval(s->x == 1); // expected-warning{{UNKNOWN}}
56 // FIXME: This should warn, but MallocChecker doesn't default-bind regions
57 // returned by standard operator new to garbage.
58 s->x += 1; // no-warning
63 void testChainedOperatorNew() {
65 // * Evaluate standard new.
66 // * Evaluate constructor S(3).
67 // * Bind value for standard new.
68 // * Evaluate our custom new.
69 // * Evaluate constructor S(Garbage).
70 // * Bind value for our custom new.
71 s = new (new S(3)) S(ConstructionKind::Garbage);
72 clang_analyzer_eval(s->x == 3); // expected-warning{{TRUE}}
73 // expected-warning@+9{{Potential leak of memory pointed to by 's'}}
75 // * Evaluate standard new.
76 // * Evaluate constructor S(Garbage).
77 // * Bind value for standard new.
78 // * Evaluate our custom new.
79 // * Evaluate constructor S(4).
80 // * Bind value for our custom new.
81 s = new (new S(ConstructionKind::Garbage)) S(4);
82 clang_analyzer_eval(s->x == 4); // expected-warning{{TRUE}}
85 // -> Enter our custom new (nullptr).
86 // * Evaluate standard new.
87 // * Inline constructor S().
88 // * Bind value for standard new.
89 // <- Exit our custom new (nullptr).
90 // * Evaluate constructor S(Garbage).
91 // * Bind value for our custom new.
92 s = new (nullptr) S(ConstructionKind::Garbage);
93 clang_analyzer_eval(s->x == 1); // expected-warning{{TRUE}}
96 // -> Enter our custom new (nullptr).
97 // * Evaluate standard new.
98 // * Inline constructor S().
99 // * Bind value for standard new.
100 // <- Exit our custom new (nullptr).
101 // -> Enter constructor S(Recursive).
102 // -> Enter our custom new (nullptr).
103 // * Evaluate standard new.
104 // * Inline constructor S().
105 // * Bind value for standard new.
106 // <- Exit our custom new (nullptr).
107 // * Evaluate constructor S(5).
108 // * Bind value for our custom new (nullptr).
109 // * Assign that value to global_s.
110 // <- Exit constructor S(Recursive).
111 // * Bind value for our custom new (nullptr).
113 s = new (nullptr) S(ConstructionKind::Recursive);
114 clang_analyzer_eval(global_s); // expected-warning{{TRUE}}
115 clang_analyzer_eval(global_s->x == 5); // expected-warning{{TRUE}}
116 clang_analyzer_eval(s->x == 6); // expected-warning{{TRUE}}