2017-09-27 17:50:45 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -std=c++11 -Wno-conversion-null -analyzer-checker=core,debug.ExprInspection -analyzer-store region -analyzer-output=text -verify %s
|
2015-12-04 23:02:30 +08:00
|
|
|
|
|
|
|
void clang_analyzer_eval(int);
|
2011-04-23 02:01:30 +08:00
|
|
|
|
|
|
|
// test to see if nullptr is detected as a null pointer
|
|
|
|
void foo1(void) {
|
2017-09-27 17:50:45 +08:00
|
|
|
char *np = nullptr; // expected-note{{'np' initialized to a null pointer value}}
|
2011-04-23 02:01:30 +08:00
|
|
|
*np = 0; // expected-warning{{Dereference of null pointer}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
2011-04-23 02:01:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
};
|
2017-09-27 17:50:45 +08:00
|
|
|
char *np = nullptr; // expected-note{{'np' initialized to a null pointer value}}
|
2011-04-23 02:01:30 +08:00
|
|
|
// casting a nullptr to anything should be caught eventually
|
2017-09-27 17:50:45 +08:00
|
|
|
int *ip = &(((struct foo *)np)->f); // expected-note{{'ip' initialized to a null pointer value}}
|
2012-10-02 03:07:15 +08:00
|
|
|
*ip = 0; // expected-warning{{Dereference of null pointer}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
2012-10-02 03:07:15 +08:00
|
|
|
// 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() {
|
2017-09-27 17:50:45 +08:00
|
|
|
char **p = 0; // expected-note{{'p' initialized to a null pointer value}}
|
2012-02-29 16:42:57 +08:00
|
|
|
delete *(p + 0); // expected-warning{{Dereference of null pointer}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void zoo1backwards() {
|
|
|
|
char **p = 0; // expected-note{{'p' initialized to a null pointer value}}
|
|
|
|
delete *(0 + p); // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef __INTPTR_TYPE__ intptr_t;
|
|
|
|
void zoo1multiply() {
|
|
|
|
char **p = 0; // FIXME-should-be-note:{{'p' initialized to a null pointer value}}
|
|
|
|
delete *((char **)((intptr_t)p * 2)); // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
2012-02-29 16:42:57 +08:00
|
|
|
}
|
2012-03-05 02:12:21 +08:00
|
|
|
|
|
|
|
void zoo2() {
|
|
|
|
int **a = 0;
|
2017-09-27 17:50:45 +08:00
|
|
|
int **b = 0; // expected-note{{'b' initialized to a null pointer value}}
|
2012-03-05 02:12:21 +08:00
|
|
|
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}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
2012-03-05 02:12:21 +08:00
|
|
|
);
|
|
|
|
}
|
2012-03-15 02:01:43 +08:00
|
|
|
|
|
|
|
int exprWithCleanups() {
|
|
|
|
struct S {
|
|
|
|
S(int a):a(a){}
|
|
|
|
~S() {}
|
|
|
|
|
|
|
|
int a;
|
|
|
|
};
|
|
|
|
|
2017-09-27 17:50:45 +08:00
|
|
|
int *x = 0; // expected-note{{'x' initialized to a null pointer value}}
|
2012-03-15 02:01:43 +08:00
|
|
|
return S(*x).a; // expected-warning{{Dereference of null pointer}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
2012-03-15 02:01:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int materializeTempExpr() {
|
2017-09-27 17:50:45 +08:00
|
|
|
int *n = 0; // expected-note{{'n' initialized to a null pointer value}}
|
2012-03-15 02:01:43 +08:00
|
|
|
struct S {
|
|
|
|
int a;
|
|
|
|
S(int i): a(i) {}
|
|
|
|
};
|
|
|
|
const S &s = S(*n); // expected-warning{{Dereference of null pointer}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Dereference of null pointer}}
|
2012-03-15 02:01:43 +08:00
|
|
|
return s.a;
|
|
|
|
}
|
2013-07-13 01:58:33 +08:00
|
|
|
|
|
|
|
typedef decltype(nullptr) nullptr_t;
|
|
|
|
void testMaterializeTemporaryExprWithNullPtr() {
|
|
|
|
// Create MaterializeTemporaryExpr with a nullptr inside.
|
|
|
|
const nullptr_t &r = nullptr;
|
|
|
|
}
|
2015-12-04 23:02:30 +08:00
|
|
|
|
|
|
|
int getSymbol();
|
|
|
|
|
|
|
|
struct X {
|
|
|
|
virtual void f() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
void invokeF(X* x) {
|
|
|
|
x->f(); // expected-warning{{Called C++ object pointer is null}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Called C++ object pointer is null}}
|
2015-12-04 23:02:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Type {
|
|
|
|
decltype(nullptr) x;
|
|
|
|
};
|
|
|
|
|
|
|
|
void shouldNotCrash() {
|
2018-12-15 06:22:29 +08:00
|
|
|
decltype(nullptr) p; // expected-note{{'p' initialized to a null pointer value}}
|
2017-09-27 17:50:45 +08:00
|
|
|
if (getSymbol()) // expected-note {{Assuming the condition is false}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
|
|
|
// expected-note@-2{{Assuming the condition is true}}
|
|
|
|
// expected-note@-3{{Taking true branch}}
|
2018-12-15 06:22:29 +08:00
|
|
|
invokeF(p); // expected-note{{Passing null pointer value via 1st parameter 'x'}}
|
|
|
|
// expected-note@-1{{Calling 'invokeF'}}
|
|
|
|
if (getSymbol()) // expected-note {{Assuming the condition is false}}
|
|
|
|
// expected-note@-1{{Taking false branch}}
|
|
|
|
invokeF(nullptr);
|
2017-09-27 17:50:45 +08:00
|
|
|
if (getSymbol()) { // expected-note {{Assuming the condition is true}}
|
|
|
|
// expected-note@-1{{Taking true branch}}
|
2017-12-20 08:47:17 +08:00
|
|
|
X *xx = Type().x; // expected-note {{Null pointer value stored to field 'x'}}
|
|
|
|
// expected-note@-1{{'xx' initialized to a null pointer value}}
|
|
|
|
xx->f(); // expected-warning{{Called C++ object pointer is null}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Called C++ object pointer is null}}
|
2015-12-04 23:02:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void f(decltype(nullptr) p) {
|
|
|
|
int *q = nullptr;
|
|
|
|
clang_analyzer_eval(p == 0); // expected-warning{{TRUE}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{TRUE}}
|
2015-12-04 23:02:30 +08:00
|
|
|
clang_analyzer_eval(q == 0); // expected-warning{{TRUE}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{TRUE}}
|
2015-12-04 23:02:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
decltype(nullptr) returnsNullPtrType();
|
|
|
|
void fromReturnType() {
|
|
|
|
((X *)returnsNullPtrType())->f(); // expected-warning{{Called C++ object pointer is null}}
|
2017-09-27 17:50:45 +08:00
|
|
|
// expected-note@-1{{Called C++ object pointer is null}}
|
2015-12-04 23:02:30 +08:00
|
|
|
}
|
2016-01-06 08:32:49 +08:00
|
|
|
|
|
|
|
#define AS_ATTRIBUTE __attribute__((address_space(256)))
|
|
|
|
class AS1 {
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
~AS1() {
|
|
|
|
int AS_ATTRIBUTE *x = 0;
|
|
|
|
*x = 3; // no-warning
|
|
|
|
}
|
|
|
|
};
|
|
|
|
void test_address_space_field_access() {
|
|
|
|
AS1 AS_ATTRIBUTE *pa = 0;
|
|
|
|
pa->x = 0; // no-warning
|
|
|
|
}
|
|
|
|
void test_address_space_bind() {
|
|
|
|
AS1 AS_ATTRIBUTE *pa = 0;
|
|
|
|
AS1 AS_ATTRIBUTE &r = *pa;
|
|
|
|
r.x = 0; // no-warning
|
|
|
|
}
|