null-deref-ps.c revision 0fe33bc94a822e315585e5cde1964d3c3b9052f9
1// RUN: clang -checker-simple -verify %s
2
3#include<stdint.h>
4
5void f1(int *p) {
6  if (p) *p = 1;
7  else *p = 0; // expected-warning{{ereference}}
8}
9
10struct foo_struct {
11  int x;
12};
13
14int f2(struct foo_struct* p) {
15
16  if (p)
17    p->x = 1;
18
19  return p->x++; // expected-warning{{Dereference of null pointer.}}
20}
21
22int f3(char* x) {
23
24  int i = 2;
25
26  if (x)
27    return x[i - 1];
28
29  return x[i+1]; // expected-warning{{Dereference of null pointer.}}
30}
31
32int f4(int *p) {
33
34  uintptr_t x = p;
35
36  if (x)
37    return 1;
38
39  int *q = (int*) x;
40  return *q; // expected-warning{{Dereference of null pointer.}}
41}