2014-01-04 13:52:11 +08:00
|
|
|
// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -analyzer-purge=none -verify %s -Wno-error=return-type
|
|
|
|
// RUN: %clang_cc1 -triple i386-apple-darwin10 -analyze -analyzer-checker=core,deadcode,alpha.core -std=gnu99 -analyzer-store=region -analyzer-constraints=range -verify %s -Wno-error=return-type
|
2008-04-03 00:54:39 +08:00
|
|
|
|
2009-09-29 07:54:40 +08:00
|
|
|
typedef unsigned uintptr_t;
|
|
|
|
|
|
|
|
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__))
|
2008-04-23 05:10:18 +08:00
|
|
|
|
2008-04-03 00:54:39 +08:00
|
|
|
void f1(int *p) {
|
|
|
|
if (p) *p = 1;
|
|
|
|
else *p = 0; // expected-warning{{ereference}}
|
|
|
|
}
|
2008-04-22 07:44:17 +08:00
|
|
|
|
|
|
|
struct foo_struct {
|
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
|
|
|
int f2(struct foo_struct* p) {
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
p->x = 1;
|
|
|
|
|
2010-10-26 08:06:13 +08:00
|
|
|
return p->x++; // expected-warning{{Access to field 'x' results in a dereference of a null pointer (loaded from variable 'p')}}
|
2008-04-22 07:44:17 +08:00
|
|
|
}
|
2008-04-22 12:56:55 +08:00
|
|
|
|
|
|
|
int f3(char* x) {
|
|
|
|
|
|
|
|
int i = 2;
|
|
|
|
|
|
|
|
if (x)
|
|
|
|
return x[i - 1];
|
|
|
|
|
2010-10-26 08:06:13 +08:00
|
|
|
return x[i+1]; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}}
|
2008-04-22 12:56:55 +08:00
|
|
|
}
|
|
|
|
|
2008-04-30 07:25:09 +08:00
|
|
|
int f3_b(char* x) {
|
|
|
|
|
|
|
|
int i = 2;
|
|
|
|
|
|
|
|
if (x)
|
|
|
|
return x[i - 1];
|
|
|
|
|
2010-10-26 08:06:13 +08:00
|
|
|
return x[i+1]++; // expected-warning{{Array access (from variable 'x') results in a null pointer dereference}}
|
2008-04-30 07:25:09 +08:00
|
|
|
}
|
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
int f4(int *p) {
|
|
|
|
|
2008-08-05 08:07:51 +08:00
|
|
|
uintptr_t x = (uintptr_t) p;
|
2008-04-23 05:10:18 +08:00
|
|
|
|
|
|
|
if (x)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
int *q = (int*) x;
|
2010-03-23 09:11:38 +08:00
|
|
|
return *q; // expected-warning{{Dereference of null pointer (loaded from variable 'q')}}
|
2008-04-23 05:39:21 +08:00
|
|
|
}
|
|
|
|
|
2010-08-28 06:46:32 +08:00
|
|
|
int f4_b() {
|
|
|
|
short array[2];
|
|
|
|
uintptr_t x = array; // expected-warning{{incompatible pointer to integer conversion}}
|
|
|
|
short *p = x; // expected-warning{{incompatible integer to pointer conversion}}
|
|
|
|
|
|
|
|
// The following branch should be infeasible.
|
2014-01-04 13:52:11 +08:00
|
|
|
if (!(p == &array[0])) {
|
2010-08-28 06:46:32 +08:00
|
|
|
p = 0;
|
|
|
|
*p = 1; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
*p = 5; // no-warning
|
|
|
|
p = 0;
|
|
|
|
}
|
|
|
|
else return; // expected-warning {{non-void function 'f4_b' should return a value}}
|
|
|
|
|
|
|
|
*p += 10; // expected-warning{{Dereference of null pointer}}
|
|
|
|
return 0;
|
|
|
|
}
|
2009-01-13 09:04:21 +08:00
|
|
|
|
2008-04-23 05:39:21 +08:00
|
|
|
int f5() {
|
|
|
|
|
|
|
|
char *s = "hello world";
|
|
|
|
return s[0]; // no-warning
|
|
|
|
}
|
|
|
|
|
2008-09-02 03:57:52 +08:00
|
|
|
int bar(int* p, int q) __attribute__((nonnull));
|
2008-07-22 08:46:16 +08:00
|
|
|
|
|
|
|
int f6(int *p) {
|
2008-09-02 03:57:52 +08:00
|
|
|
return !p ? bar(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
|
|
|
|
: bar(p, 0); // no-warning
|
|
|
|
}
|
2008-07-22 08:46:16 +08:00
|
|
|
|
2008-12-05 02:35:53 +08:00
|
|
|
int bar2(int* p, int q) __attribute__((nonnull(1)));
|
|
|
|
|
|
|
|
int f6b(int *p) {
|
|
|
|
return !p ? bar2(p, 1) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
|
|
|
|
: bar2(p, 0); // no-warning
|
|
|
|
}
|
|
|
|
|
2008-12-05 03:39:12 +08:00
|
|
|
int bar3(int*p, int q, int *r) __attribute__((nonnull(1,3)));
|
2008-12-05 02:35:53 +08:00
|
|
|
|
2008-12-05 03:39:12 +08:00
|
|
|
int f6c(int *p, int *q) {
|
2008-12-05 03:44:23 +08:00
|
|
|
return !p ? bar3(q, 2, p) // expected-warning {{Null pointer passed as an argument to a 'nonnull' parameter}}
|
|
|
|
: bar3(p, 2, q); // no-warning
|
2008-12-05 03:39:12 +08:00
|
|
|
}
|
2008-12-05 02:35:53 +08:00
|
|
|
|
2009-07-23 06:55:09 +08:00
|
|
|
void f6d(int *p) {
|
2009-07-23 05:46:56 +08:00
|
|
|
bar(p, 0);
|
|
|
|
// At this point, 'p' cannot be null.
|
|
|
|
if (!p) {
|
|
|
|
int *q = 0;
|
|
|
|
*q = 0xDEADBEEF; // no-warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-22 04:08:28 +08:00
|
|
|
void f6e(int *p, int offset) {
|
|
|
|
// PR7406 - crash from treating an UnknownVal as defined, to see if it's 0.
|
|
|
|
bar((p+offset)+1, 0); // not crash
|
|
|
|
}
|
|
|
|
|
2008-08-01 04:31:27 +08:00
|
|
|
int* qux();
|
|
|
|
|
|
|
|
int f7(int x) {
|
|
|
|
|
|
|
|
int* p = 0;
|
|
|
|
|
|
|
|
if (0 == x)
|
|
|
|
p = qux();
|
|
|
|
|
|
|
|
if (0 == x)
|
|
|
|
*p = 1; // no-warning
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2009-05-02 08:41:02 +08:00
|
|
|
int* f7b(int *x) {
|
|
|
|
|
|
|
|
int* p = 0;
|
|
|
|
|
|
|
|
if (((void*)0) == x)
|
|
|
|
p = qux();
|
|
|
|
|
|
|
|
if (((void*)0) == x)
|
|
|
|
*p = 1; // no-warning
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2009-05-05 01:27:32 +08:00
|
|
|
int* f7c(int *x) {
|
|
|
|
|
|
|
|
int* p = 0;
|
|
|
|
|
|
|
|
if (((void*)0) == x)
|
|
|
|
p = qux();
|
|
|
|
|
|
|
|
if (((void*)0) != x)
|
|
|
|
return x;
|
2009-05-05 01:53:11 +08:00
|
|
|
|
|
|
|
// If we reach here then 'p' is not null.
|
|
|
|
*p = 1; // no-warning
|
2009-05-05 01:27:32 +08:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int* f7c2(int *x) {
|
|
|
|
|
|
|
|
int* p = 0;
|
|
|
|
|
|
|
|
if (((void*)0) == x)
|
|
|
|
p = qux();
|
|
|
|
|
|
|
|
if (((void*)0) == x)
|
|
|
|
return x;
|
|
|
|
|
|
|
|
*p = 1; // expected-warning{{null}}
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2009-05-02 08:41:02 +08:00
|
|
|
|
2009-07-22 02:51:31 +08:00
|
|
|
void f8(int *p, int *q) {
|
2008-08-16 08:45:40 +08:00
|
|
|
if (!p)
|
|
|
|
if (p)
|
|
|
|
*p = 1; // no-warning
|
|
|
|
|
|
|
|
if (q)
|
|
|
|
if (!q)
|
|
|
|
*q = 1; // no-warning
|
|
|
|
}
|
2008-09-17 07:24:45 +08:00
|
|
|
|
|
|
|
int* qux();
|
|
|
|
|
2008-09-20 02:00:36 +08:00
|
|
|
int f9(unsigned len) {
|
2008-09-17 07:24:45 +08:00
|
|
|
assert (len != 0);
|
|
|
|
int *p = 0;
|
2008-09-24 14:40:03 +08:00
|
|
|
unsigned i;
|
2008-09-17 07:24:45 +08:00
|
|
|
|
2008-09-24 14:40:03 +08:00
|
|
|
for (i = 0; i < len; ++i)
|
2008-09-17 07:25:28 +08:00
|
|
|
p = qux(i);
|
2008-09-17 07:24:45 +08:00
|
|
|
|
|
|
|
return *p++; // no-warning
|
|
|
|
}
|
2008-09-18 06:24:13 +08:00
|
|
|
|
2008-09-20 02:00:36 +08:00
|
|
|
int f9b(unsigned len) {
|
2008-09-18 06:24:13 +08:00
|
|
|
assert (len > 0); // note use of '>'
|
|
|
|
int *p = 0;
|
2008-09-24 14:40:03 +08:00
|
|
|
unsigned i;
|
2008-09-18 06:24:13 +08:00
|
|
|
|
2008-09-24 14:40:03 +08:00
|
|
|
for (i = 0; i < len; ++i)
|
2008-09-18 06:24:13 +08:00
|
|
|
p = qux(i);
|
|
|
|
|
|
|
|
return *p++; // no-warning
|
|
|
|
}
|
|
|
|
|
2008-11-15 12:44:13 +08:00
|
|
|
int* f10(int* p, signed char x, int y) {
|
|
|
|
// This line tests symbolication with compound assignments where the
|
|
|
|
// LHS and RHS have different bitwidths. The new symbolic value
|
|
|
|
// for 'x' should have a bitwidth of 8.
|
|
|
|
x &= y;
|
|
|
|
|
|
|
|
// This tests that our symbolication worked, and that we correctly test
|
|
|
|
// x against 0 (with the same bitwidth).
|
|
|
|
if (!x) {
|
2011-11-18 08:45:20 +08:00
|
|
|
if (!p) return 0;
|
2008-11-15 12:44:13 +08:00
|
|
|
*p = 10;
|
|
|
|
}
|
|
|
|
else p = 0;
|
|
|
|
|
|
|
|
if (!x)
|
|
|
|
*p = 5; // no-warning
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2008-12-04 02:56:12 +08:00
|
|
|
// Test case from <rdar://problem/6407949>
|
|
|
|
void f11(unsigned i) {
|
|
|
|
int *x = 0;
|
2010-09-08 10:01:27 +08:00
|
|
|
if (i >= 0) { // expected-warning{{always true}}
|
2008-12-04 02:56:12 +08:00
|
|
|
// always true
|
|
|
|
} else {
|
|
|
|
*x = 42; // no-warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 03:06:30 +08:00
|
|
|
void f11b(unsigned i) {
|
|
|
|
int *x = 0;
|
|
|
|
if (i <= ~(unsigned)0) {
|
|
|
|
// always true
|
|
|
|
} else {
|
|
|
|
*x = 42; // no-warning
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-17 09:54:16 +08:00
|
|
|
// Test case for switch statements with weird case arms.
|
|
|
|
typedef int BOOL, *PBOOL, *LPBOOL;
|
|
|
|
typedef long LONG_PTR, *PLONG_PTR;
|
|
|
|
typedef unsigned long ULONG_PTR, *PULONG_PTR;
|
|
|
|
typedef ULONG_PTR DWORD_PTR, *PDWORD_PTR;
|
|
|
|
typedef LONG_PTR LRESULT;
|
|
|
|
typedef struct _F12ITEM *HF12ITEM;
|
|
|
|
|
|
|
|
void f12(HF12ITEM i, char *q) {
|
|
|
|
char *p = 0;
|
|
|
|
switch ((DWORD_PTR) i) {
|
|
|
|
case 0 ... 10:
|
|
|
|
p = q;
|
|
|
|
break;
|
|
|
|
case (DWORD_PTR) ((HF12ITEM) - 65535):
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = 1; // no-warning
|
|
|
|
}
|
|
|
|
|
2009-03-05 10:42:32 +08:00
|
|
|
// Test handling of translating between integer "pointers" and back.
|
|
|
|
void f13() {
|
|
|
|
int *x = 0;
|
2010-08-19 05:17:24 +08:00
|
|
|
if (((((int) x) << 2) + 1) >> 1) *x = 1;
|
2009-03-05 10:42:32 +08:00
|
|
|
}
|
|
|
|
|
2009-08-25 06:56:32 +08:00
|
|
|
// PR 4759 - Attribute non-null checking by the analyzer was not correctly
|
|
|
|
// handling pointer values that were undefined.
|
|
|
|
void pr4759_aux(int *p) __attribute__((nonnull));
|
|
|
|
|
|
|
|
void pr4759() {
|
|
|
|
int *p;
|
2010-09-10 06:51:55 +08:00
|
|
|
pr4759_aux(p); // expected-warning{{Function call argument is an uninitialized value}}
|
2009-08-25 06:56:32 +08:00
|
|
|
}
|
|
|
|
|
2011-12-21 06:35:30 +08:00
|
|
|
// Relax function call arguments invalidation to be aware of const
|
|
|
|
// arguments. Test with function pointers. radar://10595327
|
|
|
|
void ttt(const int *nptr);
|
|
|
|
void ttt2(const int *nptr);
|
|
|
|
typedef void (*NoConstType)(int*);
|
|
|
|
int foo10595327(int b) {
|
|
|
|
void (*fp)(int *);
|
|
|
|
// We use path sensitivity to get the function declaration. Even when the
|
2013-12-05 12:47:09 +08:00
|
|
|
// function pointer is cast to non-pointer-to-const parameter type, we can
|
2011-12-21 06:35:30 +08:00
|
|
|
// find the right function declaration.
|
|
|
|
if (b > 5)
|
|
|
|
fp = (NoConstType)ttt2;
|
|
|
|
else
|
|
|
|
fp = (NoConstType)ttt;
|
|
|
|
int x = 3;
|
|
|
|
int y = x + 1;
|
|
|
|
int *p = 0;
|
|
|
|
fp(&y);
|
|
|
|
if (x == y)
|
|
|
|
return *p; // no-warning
|
|
|
|
return 0;
|
|
|
|
}
|