Clang Project

clang_source_code/test/Analysis/pointer-arithmetic.c
1// RUN: %clang_analyze_cc1 -analyzer-checker=core -verify %s
2
3int test1() {
4  int *p = (int *)sizeof(int);
5  p -= 1;
6  return *p; // expected-warning {{Dereference of null pointer}}
7}
8
9int test2() {
10  int *p = (int *)sizeof(int);
11  p -= 2;
12  p += 1;
13  return *p; // expected-warning {{Dereference of null pointer}}
14}
15
16int test3() {
17  int *p = (int *)sizeof(int);
18  p++;
19  p--;
20  p--;
21  return *p; // expected-warning {{Dereference of null pointer}}
22}
23
24int test4() {
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