2017-10-10 19:01:49 +08:00
|
|
|
// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
int test1(void) {
|
2017-10-10 19:01:49 +08:00
|
|
|
int *p = (int *)sizeof(int);
|
|
|
|
p -= 1;
|
|
|
|
return *p; // expected-warning {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
int test2(void) {
|
2017-10-10 19:01:49 +08:00
|
|
|
int *p = (int *)sizeof(int);
|
|
|
|
p -= 2;
|
|
|
|
p += 1;
|
|
|
|
return *p; // expected-warning {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
int test3(void) {
|
2017-10-10 19:01:49 +08:00
|
|
|
int *p = (int *)sizeof(int);
|
|
|
|
p++;
|
|
|
|
p--;
|
|
|
|
p--;
|
|
|
|
return *p; // expected-warning {{Dereference of null pointer}}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:02:46 +08:00
|
|
|
int test4(void) {
|
2017-10-10 19:01:49 +08:00
|
|
|
// This is a special case where pointer arithmetic is not calculated to
|
|
|
|
// preserve useful warnings on dereferences of null pointers.
|
|
|
|
int *p = 0;
|
|
|
|
p += 1;
|
|
|
|
return *p; // expected-warning {{Dereference of null pointer}}
|
|
|
|
}
|