2017-03-04 02:02:02 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=text -analyzer-config suppress-null-return-paths=false -verify %s
|
2018-06-13 03:07:41 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-output=plist-multi-file -analyzer-config suppress-null-return-paths=false %s -o %t.plist
|
2019-06-11 22:21:32 +08:00
|
|
|
// RUN: %normalize_plist <%t.plist | diff -ub %S/Inputs/expected-plists/path-notes.c.plist -
|
2012-08-04 07:08:54 +08:00
|
|
|
|
|
|
|
void zero(int **p) {
|
|
|
|
*p = 0;
|
|
|
|
// expected-note@-1 {{Null pointer value stored to 'a'}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testZero(int *a) {
|
|
|
|
zero(&a);
|
|
|
|
// expected-note@-1 {{Calling 'zero'}}
|
|
|
|
// expected-note@-2 {{Returning from 'zero'}}
|
|
|
|
*a = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
|
2012-08-04 07:09:01 +08:00
|
|
|
}
|
|
|
|
|
2013-03-02 11:20:52 +08:00
|
|
|
void testCheck(int *a) {
|
|
|
|
if (a) {
|
|
|
|
// expected-note@-1 + {{Assuming 'a' is null}}
|
2012-10-26 06:07:10 +08:00
|
|
|
// expected-note@-2 + {{Taking false branch}}
|
2013-03-02 11:20:52 +08:00
|
|
|
;
|
2012-08-04 07:09:01 +08:00
|
|
|
}
|
|
|
|
*a = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int *getPointer();
|
|
|
|
|
|
|
|
void testInitCheck() {
|
|
|
|
int *a = getPointer();
|
2013-02-27 03:44:38 +08:00
|
|
|
// expected-note@-1 {{'a' initialized here}}
|
2013-03-02 11:20:52 +08:00
|
|
|
if (a) {
|
|
|
|
// expected-note@-1 + {{Assuming 'a' is null}}
|
|
|
|
// expected-note@-2 + {{Taking false branch}}
|
|
|
|
;
|
|
|
|
}
|
2012-08-04 07:09:01 +08:00
|
|
|
*a = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testStoreCheck(int *a) {
|
|
|
|
a = getPointer();
|
|
|
|
// expected-note@-1 {{Value assigned to 'a'}}
|
2013-03-02 11:20:52 +08:00
|
|
|
if (a) {
|
|
|
|
// expected-note@-1 + {{Assuming 'a' is null}}
|
|
|
|
// expected-note@-2 + {{Taking false branch}}
|
|
|
|
;
|
|
|
|
}
|
2012-08-04 07:09:01 +08:00
|
|
|
*a = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
|
|
|
|
}
|
2012-08-07 05:28:14 +08:00
|
|
|
|
|
|
|
|
2012-08-25 00:34:31 +08:00
|
|
|
int *getZero() {
|
|
|
|
int *p = 0;
|
2013-02-27 03:44:38 +08:00
|
|
|
// expected-note@-1 + {{'p' initialized to a null pointer value}}
|
2012-08-25 00:34:31 +08:00
|
|
|
// ^ This note checks that we add a second visitor for the return value.
|
|
|
|
return p;
|
2012-08-28 04:18:30 +08:00
|
|
|
// expected-note@-1 + {{Returning null pointer (loaded from 'p')}}
|
2012-08-25 00:34:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void testReturnZero() {
|
|
|
|
*getZero() = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Calling 'getZero'}}
|
|
|
|
// expected-note@-2 {{Returning from 'getZero'}}
|
|
|
|
// expected-note@-3 {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
2012-08-28 04:18:30 +08:00
|
|
|
int testReturnZero2() {
|
|
|
|
return *getZero(); // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Calling 'getZero'}}
|
|
|
|
// expected-note@-2 {{Returning from 'getZero'}}
|
|
|
|
// expected-note@-3 {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
2012-08-25 00:34:31 +08:00
|
|
|
void testInitZero() {
|
|
|
|
int *a = getZero();
|
2012-08-28 08:50:42 +08:00
|
|
|
// expected-note@-1 {{Calling 'getZero'}}
|
|
|
|
// expected-note@-2 {{Returning from 'getZero'}}
|
2013-02-27 03:44:38 +08:00
|
|
|
// expected-note@-3 {{'a' initialized to a null pointer value}}
|
2012-08-25 00:34:31 +08:00
|
|
|
*a = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testStoreZero(int *a) {
|
|
|
|
a = getZero();
|
2012-08-28 08:50:42 +08:00
|
|
|
// expected-note@-1 {{Calling 'getZero'}}
|
|
|
|
// expected-note@-2 {{Returning from 'getZero'}}
|
|
|
|
// expected-note@-3 {{Null pointer value stored to 'a'}}
|
2012-08-25 00:34:31 +08:00
|
|
|
*a = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from variable 'a')}}
|
|
|
|
}
|
|
|
|
|
2012-09-22 09:25:00 +08:00
|
|
|
void usePointer(int *p) {
|
|
|
|
*p = 1; // expected-warning{{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
|
|
|
void testUseOfNullPointer() {
|
|
|
|
// Test the case where an argument expression is itself a call.
|
|
|
|
usePointer(getZero());
|
|
|
|
// expected-note@-1 {{Calling 'getZero'}}
|
|
|
|
// expected-note@-2 {{Returning from 'getZero'}}
|
|
|
|
// expected-note@-3 {{Passing null pointer value via 1st parameter 'p'}}
|
|
|
|
// expected-note@-4 {{Calling 'usePointer'}}
|
|
|
|
}
|
|
|
|
|
2013-04-13 02:40:21 +08:00
|
|
|
struct X { char *p; };
|
|
|
|
|
|
|
|
void setFieldToNull(struct X *x) {
|
|
|
|
x->p = 0; // expected-note {{Null pointer value stored to field 'p'}}
|
|
|
|
}
|
|
|
|
|
|
|
|
int testSetFieldToNull(struct X *x) {
|
|
|
|
setFieldToNull(x); // expected-note {{Calling 'setFieldToNull'}}
|
|
|
|
// expected-note@-1{{Returning from 'setFieldToNull'}}
|
|
|
|
return *x->p;
|
|
|
|
// expected-warning@-1 {{Dereference of null pointer (loaded from field 'p')}}
|
|
|
|
// expected-note@-2 {{Dereference of null pointer (loaded from field 'p')}}
|
|
|
|
}
|
|
|
|
|
2013-04-16 06:37:59 +08:00
|
|
|
struct Outer {
|
|
|
|
struct Inner {
|
|
|
|
int *p;
|
|
|
|
} inner;
|
|
|
|
};
|
|
|
|
|
|
|
|
void test(struct Outer *wrapperPtr) {
|
|
|
|
wrapperPtr->inner.p = 0; // expected-note {{Null pointer value stored to field 'p'}}
|
|
|
|
*wrapperPtr->inner.p = 1; //expected-warning {{Dereference of null pointer (loaded from field 'p')}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer (loaded from field 'p')}}
|
|
|
|
}
|
|
|
|
|
2013-04-18 06:29:51 +08:00
|
|
|
void test4(int **p) {
|
|
|
|
if (*p) return; // expected-note {{Taking false branch}}
|
|
|
|
// expected-note@-1 {{Assuming pointer value is null}}
|
|
|
|
**p = 1; // expected-warning {{Dereference of null pointer}}
|
|
|
|
// expected-note@-1 {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
2018-04-04 02:52:30 +08:00
|
|
|
void boringCallee() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void interestingCallee(int *x) {
|
|
|
|
*x = 0; // expected-note{{The value 0 is assigned to 'x'}}
|
|
|
|
boringCallee(); // no-note
|
|
|
|
}
|
|
|
|
|
|
|
|
int testBoringCalleeOfInterestingCallee() {
|
|
|
|
int x;
|
|
|
|
interestingCallee(&x); // expected-note{{Calling 'interestingCallee'}}
|
|
|
|
// expected-note@-1{{Returning from 'interestingCallee'}}
|
|
|
|
return 1 / x; // expected-warning{{Division by zero}}
|
|
|
|
// expected-note@-1{{Division by zero}}
|
|
|
|
}
|
|
|
|
|