]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Analysis/free.c
Vendor import of clang release_30 branch r142614:
[FreeBSD/FreeBSD.git] / test / Analysis / free.c
1 // RUN: %clang_cc1 -analyze -analyzer-store=region -analyzer-checker=core,experimental.unix.Malloc -fblocks -verify %s
2 void free(void *);
3
4 void t1 () {
5   int a[] = { 1 };
6   free(a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
7 }
8
9 void t2 () {
10   int a = 1;
11   free(&a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}}
12 }
13
14 void t3 () {
15   static int a[] = { 1 };
16   free(a); // expected-warning {{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}}
17 }
18
19 void t4 (char *x) {
20   free(x); // no-warning
21 }
22
23 void t5 () {
24   extern char *ptr();
25   free(ptr()); // no-warning
26 }
27
28 void t6 () {
29   free((void*)1000); // expected-warning {{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}}
30 }
31
32 void t7 (char **x) {
33   free(*x); // no-warning
34 }
35
36 void t8 (char **x) {
37   // ugh
38   free((*x)+8); // no-warning
39 }
40
41 void t9 () {
42 label:
43   free(&&label); // expected-warning {{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}}
44 }
45
46 void t10 () {
47   free((void*)&t10); // expected-warning {{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}}
48 }
49
50 void t11 () {
51   char *p = (char*)__builtin_alloca(2);
52   free(p); // expected-warning {{Argument to free() was allocated by alloca(), not malloc()}}
53 }
54
55 void t12 () {
56   free(^{return;}); // expected-warning {{Argument to free() is a block, which is not memory allocated by malloc()}}
57 }
58
59 void t13 (char a) {
60   free(&a); // expected-warning {{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}}
61 }
62
63 static int someGlobal[2];
64 void t14 () {
65   free(someGlobal); // expected-warning {{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}}
66 }
67
68 void t15 (char **x, int offset) {
69   // Unknown value
70   free(x[offset]); // no-warning
71 }