blob: fb77de22f1276a50e70fc51aa07e092e31e11aac [file] [log] [blame]
Jordan Rose89e5aaf2012-07-16 23:38:09 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -std=c++11 -verify %s
Zhongxing Xu40ab43b2010-04-20 05:48:57 +00002
Jordan Rosee38c1c22012-06-20 01:32:01 +00003void clang_analyzer_eval(bool);
4
Jordan Rose89e5aaf2012-07-16 23:38:09 +00005typedef __typeof__(sizeof(int)) size_t;
Jordan Rosee38c1c22012-06-20 01:32:01 +00006extern "C" void *malloc(size_t);
7
Jordan Roseee681112012-06-25 20:48:28 +00008int someGlobal;
9void testImplicitlyDeclaredGlobalNew() {
10 if (someGlobal != 0)
11 return;
12
13 // This used to crash because the global operator new is being implicitly
14 // declared and it does not have a valid source location. (PR13090)
15 void *x = ::operator new(0);
16 ::operator delete(x);
17
18 // Check that the new/delete did not invalidate someGlobal;
19 clang_analyzer_eval(someGlobal == 0); // expected-warning{{TRUE}}
20}
21
22
Jordan Rosee38c1c22012-06-20 01:32:01 +000023// This is the standard placement new.
24inline void* operator new(size_t, void* __p) throw()
25{
26 return __p;
Zhongxing Xu48fb3222010-04-21 02:22:25 +000027}
Zhongxing Xu40ab43b2010-04-20 05:48:57 +000028
Jordan Rosee38c1c22012-06-20 01:32:01 +000029void *testPlacementNew() {
30 int *x = (int *)malloc(sizeof(int));
31 *x = 1;
32 clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
33
34 void *y = new (x) int;
35 clang_analyzer_eval(x == y); // expected-warning{{TRUE}};
36 clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
37
38 return y;
39}
40
41void *operator new(size_t, size_t, int *);
42void *testCustomNew() {
43 int x[1] = {1};
44 clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
45
46 void *y = new (0, x) int;
47 clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
48
49 return y; // no-warning
Zhongxing Xu40ab43b2010-04-20 05:48:57 +000050}
51
Jordan Rose70cbf3c2012-07-02 22:21:47 +000052void *operator new(size_t, void *, void *);
53void *testCustomNewMalloc() {
54 int *x = (int *)malloc(sizeof(int));
55
56 // Should be no-warning (the custom allocator could have freed x).
57 void *y = new (0, x) int; // no-warning
58
59 return y;
60}
61
Jordan Rose89e5aaf2012-07-16 23:38:09 +000062void testScalarInitialization() {
63 int *n = new int(3);
64 clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}}
65
66 new (n) int();
67 clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}}
68
69 new (n) int{3};
70 clang_analyzer_eval(*n == 3); // expected-warning{{TRUE}}
71
72 new (n) int{};
73 clang_analyzer_eval(*n == 0); // expected-warning{{TRUE}}
74}
75
Jordan Rose3c4e76d2012-06-20 05:34:32 +000076
77//--------------------------------
78// Incorrectly-modelled behavior
79//--------------------------------
80
Jordan Rose89e5aaf2012-07-16 23:38:09 +000081int testNoInitialization() {
Jordan Rose3c4e76d2012-06-20 05:34:32 +000082 int *n = new int;
83
84 // Should warn that *n is uninitialized.
85 if (*n) { // no-warning
Jordan Rose89e5aaf2012-07-16 23:38:09 +000086 return 0;
Jordan Rose3c4e76d2012-06-20 05:34:32 +000087 }
Jordan Rose89e5aaf2012-07-16 23:38:09 +000088 return 1;
Jordan Rose3c4e76d2012-06-20 05:34:32 +000089}
90
Jordan Rose89e5aaf2012-07-16 23:38:09 +000091int testNoInitializationPlacement() {
92 int n;
93 new (&n) int;
Jordan Rose3c4e76d2012-06-20 05:34:32 +000094
Jordan Rose89e5aaf2012-07-16 23:38:09 +000095 // Should warn that n is uninitialized.
96 if (n) { // no-warning
97 return 0;
98 }
99 return 1;
Jordan Rose3c4e76d2012-06-20 05:34:32 +0000100}