Clang Project

clang_source_code/test/Analysis/null-deref-ps-region.c
1// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core,unix,alpha.unix -std=gnu99 -analyzer-store=region -verify %s
2
3#include "Inputs/system-header-simulator.h"
4
5typedef __typeof(sizeof(int)) size_t;
6void *memset(void *__s, int __c, size_t __n);
7void *malloc(size_t __size);
8void free(void *__ptr);
9
10// The store for 'a[1]' should not be removed mistakenly. SymbolicRegions may
11// also be live roots.
12void f14(int *a) {
13  int i;
14  a[1] = 1;
15  i = a[1];
16  if (i != 1) {
17    int *p = 0;
18    i = *p; // no-warning
19  }
20}
21
22void foo() {
23  int *x = malloc(sizeof(int));
24  memset(x, 0, sizeof(int));
25  int n = 1 / *x; // expected-warning {{Division by zero}}
26  free(x);
27}
28
29void bar() {
30  int *x = malloc(sizeof(int));
31  memset(x, 0, 1);
32  int n = 1 / *x; // no-warning
33  free(x);
34}
35
36void testConcreteNull() {
37  int *x = 0;
38  memset(x, 0, 1); // expected-warning {{Null pointer argument in call to memory set function}}
39}
40
41void testStackArray() {
42  char buf[13];
43  memset(buf, 0, 1); // no-warning
44}
45
46void testHeapSymbol() {
47  char *buf = (char *)malloc(13);
48  memset(buf, 0, 1); // no-warning
49  free(buf);
50}
51
52void testStackArrayOutOfBound() {
53  char buf[1];
54  memset(buf, 0, 1024); // expected-warning {{Memory set function accesses out-of-bound array element}} expected-warning {{'memset' will always overflow; destination buffer has size 1, but size argument is 1024}}
55}
56
57void testHeapSymbolOutOfBound() {
58  char *buf = (char *)malloc(1);
59  memset(buf, 0, 1024); // expected-warning {{Memory set function accesses out-of-bound array element}}
60  free(buf);
61}
62
63void testStackArraySameSize() {
64  char buf[1];
65  memset(buf, 0, sizeof(buf)); // no-warning
66}
67
68void testHeapSymbolSameSize() {
69  char *buf = (char *)malloc(1);
70  memset(buf, 0, 1); // no-warning
71  free(buf);
72}
73