2008-11-11 09:25:18 +08:00
|
|
|
// RUN: clang -std=gnu99 -checker-simple -verify %s &&
|
2008-11-10 17:43:12 +08:00
|
|
|
// RUN: clang -std=gnu99 -checker-simple -analyzer-store-region -verify %s
|
2008-04-03 00:54:39 +08:00
|
|
|
|
2008-04-23 05:10:18 +08:00
|
|
|
#include<stdint.h>
|
2008-09-17 07:24:45 +08:00
|
|
|
#include <assert.h>
|
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;
|
|
|
|
|
2008-04-22 07:45:26 +08:00
|
|
|
return p->x++; // expected-warning{{Dereference of null pointer.}}
|
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];
|
|
|
|
|
|
|
|
return x[i+1]; // expected-warning{{Dereference of null pointer.}}
|
|
|
|
}
|
|
|
|
|
2008-04-30 07:25:09 +08:00
|
|
|
int f3_b(char* x) {
|
|
|
|
|
|
|
|
int i = 2;
|
|
|
|
|
|
|
|
if (x)
|
|
|
|
return x[i - 1];
|
|
|
|
|
|
|
|
return x[i+1]++; // expected-warning{{Dereference of null pointer.}}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return *q; // expected-warning{{Dereference of null pointer.}}
|
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-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;
|
|
|
|
}
|
|
|
|
|
2008-08-16 08:45:40 +08:00
|
|
|
int f8(int *p, int *q) {
|
|
|
|
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) {
|
|
|
|
if (!p) return;
|
|
|
|
*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;
|
|
|
|
if (i >= 0) {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|