Dominic Chen | 184c624 | 2017-03-03 18:02:02 +0000 | [diff] [blame] | 1 | // RUN: %clang_analyze_cc1 -analyzer-store=region -analyzer-checker=core,unix.Malloc -fblocks -verify %s |
| 2 | // RUN: %clang_analyze_cc1 -analyzer-store=region -analyzer-checker=core,unix.Malloc -fblocks -verify -analyzer-config unix.Malloc:Optimistic=true %s |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 3 | typedef __typeof(sizeof(int)) size_t; |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 4 | void free(void *); |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 5 | void *alloca(size_t); |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 6 | |
| 7 | void t1 () { |
| 8 | int a[] = { 1 }; |
| 9 | free(a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}} |
| 10 | } |
| 11 | |
| 12 | void t2 () { |
| 13 | int a = 1; |
| 14 | free(&a); // expected-warning {{Argument to free() is the address of the local variable 'a', which is not memory allocated by malloc()}} |
| 15 | } |
| 16 | |
| 17 | void t3 () { |
| 18 | static int a[] = { 1 }; |
| 19 | free(a); // expected-warning {{Argument to free() is the address of the static variable 'a', which is not memory allocated by malloc()}} |
| 20 | } |
| 21 | |
| 22 | void t4 (char *x) { |
| 23 | free(x); // no-warning |
| 24 | } |
| 25 | |
| 26 | void t5 () { |
| 27 | extern char *ptr(); |
| 28 | free(ptr()); // no-warning |
| 29 | } |
| 30 | |
| 31 | void t6 () { |
| 32 | free((void*)1000); // expected-warning {{Argument to free() is a constant address (1000), which is not memory allocated by malloc()}} |
| 33 | } |
| 34 | |
| 35 | void t7 (char **x) { |
| 36 | free(*x); // no-warning |
| 37 | } |
| 38 | |
| 39 | void t8 (char **x) { |
| 40 | // ugh |
| 41 | free((*x)+8); // no-warning |
| 42 | } |
| 43 | |
| 44 | void t9 () { |
| 45 | label: |
| 46 | free(&&label); // expected-warning {{Argument to free() is the address of the label 'label', which is not memory allocated by malloc()}} |
| 47 | } |
| 48 | |
| 49 | void t10 () { |
| 50 | free((void*)&t10); // expected-warning {{Argument to free() is the address of the function 't10', which is not memory allocated by malloc()}} |
| 51 | } |
| 52 | |
| 53 | void t11 () { |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 54 | char *p = (char*)alloca(2); |
Anton Yartsev | 0578959 | 2013-03-28 17:05:19 +0000 | [diff] [blame] | 55 | free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}} |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void t12 () { |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 59 | char *p = (char*)__builtin_alloca(2); |
| 60 | free(p); // expected-warning {{Memory allocated by alloca() should not be deallocated}} |
| 61 | } |
| 62 | |
| 63 | void t13 () { |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 64 | free(^{return;}); // expected-warning {{Argument to free() is a block, which is not memory allocated by malloc()}} |
| 65 | } |
| 66 | |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 67 | void t14 (char a) { |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 68 | free(&a); // expected-warning {{Argument to free() is the address of the parameter 'a', which is not memory allocated by malloc()}} |
| 69 | } |
| 70 | |
| 71 | static int someGlobal[2]; |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 72 | void t15 () { |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 73 | free(someGlobal); // expected-warning {{Argument to free() is the address of the global variable 'someGlobal', which is not memory allocated by malloc()}} |
| 74 | } |
| 75 | |
Anton Yartsev | 5b5c7ce | 2015-02-19 13:36:20 +0000 | [diff] [blame] | 76 | void t16 (char **x, int offset) { |
Jordy Rose | 3597b21 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 77 | // Unknown value |
| 78 | free(x[offset]); // no-warning |
| 79 | } |