| 1 |
// RUN: %clang_analyze_cc1 -analyzer-checker=core,unix.Malloc -fblocks -verify %s |
| 2 |
|
| 3 |
int* f1(void) { |
| 4 |
int x = 0; |
| 5 |
return &x; // expected-warning{{Address of stack memory associated with local variable 'x' returned}} expected-warning{{address of stack memory associated with local variable 'x' returned}} |
| 6 |
} |
| 7 |
|
| 8 |
int* f2(int y) { |
| 9 |
return &y; // expected-warning{{Address of stack memory associated with local variable 'y' returned}} expected-warning{{address of stack memory associated with parameter 'y' returned}} |
| 10 |
} |
| 11 |
|
| 12 |
int* f3(int x, int *y) { |
| 13 |
int w = 0; |
| 14 |
|
| 15 |
if (x) |
| 16 |
y = &w; |
| 17 |
|
| 18 |
return y; // expected-warning{{Address of stack memory associated with local variable 'w' returned to caller}} |
| 19 |
} |
| 20 |
|
| 21 |
void* compound_literal(int x, int y) { |
| 22 |
if (x) |
| 23 |
return &(unsigned short){((unsigned short)0x22EF)}; // expected-warning{{Address of stack memory}} expected-warning{{address of stack memory}} |
| 24 |
|
| 25 |
int* array[] = {}; |
| 26 |
struct s { int z; double y; int w; }; |
| 27 |
|
| 28 |
if (y) |
| 29 |
return &((struct s){ 2, 0.4, 5 * 8 }); // expected-warning{{Address of stack memory}} expected-warning{{address of stack memory}} |
| 30 |
|
| 31 |
|
| 32 |
void* p = &((struct s){ 42, 0.4, x ? 42 : 0 }); |
| 33 |
return p; // expected-warning{{Address of stack memory}} |
| 34 |
} |
| 35 |
|
| 36 |
void* alloca_test(void) { |
| 37 |
void* p = __builtin_alloca(10); |
| 38 |
return p; // expected-warning{{Address of stack memory}} |
| 39 |
} |
| 40 |
|
| 41 |
int array_test(int x[2]) { |
| 42 |
return x[0]; // no-warning |
| 43 |
} |
| 44 |
|
| 45 |
struct baz { |
| 46 |
int x; |
| 47 |
int y[2]; |
| 48 |
}; |
| 49 |
|
| 50 |
int struct_test(struct baz byVal, int flag) { |
| 51 |
if (flag) |
| 52 |
return byVal.x; // no-warning |
| 53 |
else { |
| 54 |
return byVal.y[0]; // no-warning |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
typedef int (^ComparatorBlock)(int a, int b); |
| 59 |
ComparatorBlock test_return_block(void) { |
| 60 |
// This block is a global since it has no captures. |
| 61 |
ComparatorBlock b = ^int(int a, int b){ return a > b; }; |
| 62 |
return b; // no-warning |
| 63 |
} |
| 64 |
|
| 65 |
ComparatorBlock test_return_block_with_capture(int x) { |
| 66 |
// This block is stack allocated because it has captures. |
| 67 |
ComparatorBlock b = ^int(int a, int b){ return a > b + x; }; |
| 68 |
return b; // expected-warning{{Address of stack-allocated block}} |
| 69 |
} |
| 70 |
|
| 71 |
ComparatorBlock test_return_block_neg_aux(void); |
| 72 |
ComparatorBlock test_return_block_neg(void) { |
| 73 |
ComparatorBlock b = test_return_block_neg_aux(); |
| 74 |
return b; // no-warning |
| 75 |
} |
| 76 |
|
| 77 |
int *rdar_7523821_f2(void) { |
| 78 |
int a[3]; |
| 79 |
return a; // expected-warning 2 {{ddress of stack memory associated with local variable 'a' returned}} |
| 80 |
}; |
| 81 |
|
| 82 |
// Handle blocks that have no captures or are otherwise declared 'static'. |
| 83 |
typedef int (^RDar10348049)(int value); |
| 84 |
RDar10348049 test_rdar10348049(void) { |
| 85 |
static RDar10348049 b = ^int(int x) { |
| 86 |
return x + 2; |
| 87 |
}; |
| 88 |
return b; // no-warning |
| 89 |
} |
| 90 |
|
| 91 |
void testRegister(register const char *reg) { |
| 92 |
if (reg) (void)reg[0]; |
| 93 |
} |
| 94 |
void callTestRegister(void) { |
| 95 |
char buf[20]; |
| 96 |
testRegister(buf); // no-warning |
| 97 |
} |