blob: 8093eff3a1c2b33ab9a4fde3e7c3c34fecd0c42d [file] [log] [blame]
Jordan Rosee38c1c22012-06-20 01:32:01 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,unix.Malloc,debug.ExprInspection -analyzer-store region -verify %s
Zhongxing Xu40ab43b2010-04-20 05:48:57 +00002
Jordan Rosee38c1c22012-06-20 01:32:01 +00003void clang_analyzer_eval(bool);
4
5typedef typeof(sizeof(int)) size_t;
6extern "C" void *malloc(size_t);
7
8// This is the standard placement new.
9inline void* operator new(size_t, void* __p) throw()
10{
11 return __p;
Zhongxing Xu48fb3222010-04-21 02:22:25 +000012}
Zhongxing Xu40ab43b2010-04-20 05:48:57 +000013
Jordan Rosee38c1c22012-06-20 01:32:01 +000014void *testPlacementNew() {
15 int *x = (int *)malloc(sizeof(int));
16 *x = 1;
17 clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
18
19 void *y = new (x) int;
20 clang_analyzer_eval(x == y); // expected-warning{{TRUE}};
21 clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
22
23 return y;
24}
25
26void *operator new(size_t, size_t, int *);
27void *testCustomNew() {
28 int x[1] = {1};
29 clang_analyzer_eval(*x == 1); // expected-warning{{TRUE}};
30
31 void *y = new (0, x) int;
32 clang_analyzer_eval(*x == 1); // expected-warning{{UNKNOWN}};
33
34 return y; // no-warning
Zhongxing Xu40ab43b2010-04-20 05:48:57 +000035}
36
Jordan Rose3c4e76d2012-06-20 05:34:32 +000037
38//--------------------------------
39// Incorrectly-modelled behavior
40//--------------------------------
41
42void testZeroInitialization() {
43 int *n = new int;
44
45 // Should warn that *n is uninitialized.
46 if (*n) { // no-warning
47 }
48}
49
50void testValueInitialization() {
51 int *n = new int(3);
52
53 // Should be TRUE (and have no uninitialized variable warning)
54 clang_analyzer_eval(*n == 3); // expected-warning{{UNKNOWN}}
55}
56
57
58void *operator new(size_t, void *, void *);
59void *testCustomNewMalloc() {
60 int *x = (int *)malloc(sizeof(int));
61
62 // Should be no-warning (the custom allocator could have freed x).
63 void *y = new (0, x) int; // expected-warning{{leak of memory pointed to by 'x'}}
64
65 return y;
66}
67