xref: /llvm-project/clang/test/Analysis/pointer-arithmetic.c (revision 1ea584377e7897f7df5302ed9cd378d17be14fbf)
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
2 
test1(void)3 int test1(void) {
4   int *p = (int *)sizeof(int);
5   p -= 1;
6   return *p; // expected-warning {{Dereference of null pointer}}
7 }
8 
test2(void)9 int test2(void) {
10   int *p = (int *)sizeof(int);
11   p -= 2;
12   p += 1;
13   return *p; // expected-warning {{Dereference of null pointer}}
14 }
15 
test3(void)16 int test3(void) {
17   int *p = (int *)sizeof(int);
18   p++;
19   p--;
20   p--;
21   return *p; // expected-warning {{Dereference of null pointer}}
22 }
23 
test4(void)24 int test4(void) {
25   // This is a special case where pointer arithmetic is not calculated to
26   // preserve useful warnings on dereferences of null pointers.
27   int *p = 0;
28   p += 1;
29   return *p; // expected-warning {{Dereference of null pointer}}
30 }
31