null-deref-ps.c revision a548846b471f7ca05ec6038c7d9d3b4d0de777cc
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} 42 43int f5() { 44 45 char *s = "hello world"; 46 return s[0]; // no-warning 47} 48 49