1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core -verify %s
extern void __assert_fail (__const char *__assertion, __const char *__file,
unsigned int __line, __const char *__function)
__attribute__ ((__noreturn__));
#define assert(expr) \
((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
typedef unsigned long long uintptr_t;
void f0(void) {
int *p = (int*) 0x10000; // Should not crash here.
*p = 3; // expected-warning{{Dereference of a fixed address}}
}
void f1(int *p) {
if (p != (int *)-1)
*p = 1;
else
*p = 0; // expected-warning{{Dereference of a fixed address}}
}
struct f2_struct {
int x;
};
int f2(struct f2_struct* p) {
if (p != (struct f2_struct *)1)
p->x = 1;
return p->x++; // expected-warning{{Access to field 'x' results in a dereference of a fixed address (loaded from variable 'p')}}
}
int f3_1(char* x) {
int i = 2;
if (x != (char *)1)
return x[i - 1];
return x[i+1]; // expected-warning{{Array access (from variable 'x') results in a dereference of a fixed address}}
}
int f3_2(char* x) {
int i = 2;
if (x != (char *)1)
return x[i - 1];
return x[i+1]++; // expected-warning{{Array access (from variable 'x') results in a dereference of a fixed address}}
}
int f4_1(int *p) {
uintptr_t x = (uintptr_t) p;
if (x != (uintptr_t)1)
return 1;
int *q = (int*) x;
return *q; // expected-warning{{Dereference of a fixed address (loaded from variable 'q')}}
}
int f4_2(void) {
short array[2];
uintptr_t x = (uintptr_t)array;
short *p = (short *)x;
// The following branch should be infeasible.
if (!(p == &array[0])) {
p = (short *)1;
*p = 1; // no-warning
}
if (p != (short *)1) {
*p = 5; // no-warning
p = (short *)1; // expected-warning {{Using a fixed address is not portable}}
}
else return 1;
*p += 10; // expected-warning{{Dereference of a fixed}}
return 0;
}
int f5(void) {
char *s = "hello world";
return s[0]; // no-warning
}
void f6(int *p, int *q) {
if (p != (int *)1)
if (p == (int *)1)
*p = 1; // no-warning
if (q == (int *)1)
if (q != (int *)1)
*q = 1; // no-warning
}
int* qux(int);
int f7_1(unsigned len) {
assert (len != 0);
int *p = (int *)1;
unsigned i;
for (i = 0; i < len; ++i)
p = qux(i);
return *p++; // no-warning
}
int f7_2(unsigned len) {
assert (len > 0); // note use of '>'
int *p = (int *)1;
unsigned i;
for (i = 0; i < len; ++i)
p = qux(i);
return *p++; // no-warning
}
struct f8_s {
int x;
int y[2];
};
void f8(struct f8_s *s, int coin) {
if (s != (struct f8_s *)7)
return;
if (coin)
s->x = 5; // expected-warning{{Access to field 'x' results in a dereference of a fixed address (loaded from variable 's')}}
else
s->y[1] = 6; // expected-warning{{Array access (via field 'y') results in a dereference of a fixed address}}
}
void f9() {
int (*p_function) (char, char) = (int (*)(char, char))0x04040; // FIXME: warn at this initialization
p_function = (int (*)(char, char))0x04080; // expected-warning {{Using a fixed address is not portable}}
// FIXME: there should be a warning from calling the function pointer with fixed address
int x = (*p_function) ('x', 'y');
}
|