2012-03-15 12:50:32 +08:00
|
|
|
// RUN: %clang_cc1 -std=c++11 -Wno-conversion-null -analyze -analyzer-checker=core -analyzer-store region -verify %s
|
2011-04-23 02:01:30 +08:00
|
|
|
|
|
|
|
// test to see if nullptr is detected as a null pointer
|
|
|
|
void foo1(void) {
|
|
|
|
char *np = nullptr;
|
|
|
|
*np = 0; // expected-warning{{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if comparing nullptr to nullptr is detected properly
|
|
|
|
void foo2(void) {
|
|
|
|
char *np1 = nullptr;
|
|
|
|
char *np2 = np1;
|
|
|
|
char c;
|
|
|
|
if (np1 == np2)
|
|
|
|
np1 = &c;
|
|
|
|
*np1 = 0; // no-warning
|
|
|
|
}
|
|
|
|
|
|
|
|
// invoving a nullptr in a more complex operation should be cause a warning
|
|
|
|
void foo3(void) {
|
|
|
|
struct foo {
|
|
|
|
int a, f;
|
|
|
|
};
|
|
|
|
char *np = nullptr;
|
|
|
|
// casting a nullptr to anything should be caught eventually
|
2012-10-02 03:07:15 +08:00
|
|
|
int *ip = &(((struct foo *)np)->f);
|
|
|
|
*ip = 0; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// should be error here too, but analysis gets stopped
|
|
|
|
// *np = 0;
|
2011-04-23 02:01:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// nullptr is implemented as a zero integer value, so should be able to compare
|
|
|
|
void foo4(void) {
|
|
|
|
char *np = nullptr;
|
|
|
|
if (np != 0)
|
|
|
|
*np = 0; // no-warning
|
|
|
|
char *cp = 0;
|
|
|
|
if (np != cp)
|
|
|
|
*np = 0; // no-warning
|
|
|
|
}
|
|
|
|
|
2011-07-16 04:29:02 +08:00
|
|
|
int pr10372(void *& x) {
|
|
|
|
// GNU null is a pointer-sized integer, not a pointer.
|
|
|
|
x = __null;
|
|
|
|
// This used to crash.
|
|
|
|
return __null;
|
|
|
|
}
|
|
|
|
|
2012-02-29 16:42:57 +08:00
|
|
|
void zoo1() {
|
|
|
|
char **p = 0;
|
|
|
|
delete *(p + 0); // expected-warning{{Dereference of null pointer}}
|
|
|
|
}
|
2012-03-05 02:12:21 +08:00
|
|
|
|
|
|
|
void zoo2() {
|
|
|
|
int **a = 0;
|
|
|
|
int **b = 0;
|
|
|
|
asm ("nop"
|
2012-05-22 19:03:10 +08:00
|
|
|
:"=r"(*a)
|
2012-03-05 02:12:21 +08:00
|
|
|
:"0"(*b) // expected-warning{{Dereference of null pointer}}
|
|
|
|
);
|
|
|
|
}
|
2012-03-15 02:01:43 +08:00
|
|
|
|
|
|
|
int exprWithCleanups() {
|
|
|
|
struct S {
|
|
|
|
S(int a):a(a){}
|
|
|
|
~S() {}
|
|
|
|
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
|
|
|
int *x = 0;
|
|
|
|
return S(*x).a; // expected-warning{{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
|
|
|
int materializeTempExpr() {
|
|
|
|
int *n = 0;
|
|
|
|
struct S {
|
|
|
|
int a;
|
|
|
|
S(int i): a(i) {}
|
|
|
|
};
|
|
|
|
const S &s = S(*n); // expected-warning{{Dereference of null pointer}}
|
|
|
|
return s.a;
|
|
|
|
}
|