additive-folding-range-constraints.c revision 9e607dd1dff375b4fa33d923ed592dad3ad43d42
1// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,unix.Malloc -verify -analyzer-constraints=range %s
2
3// These are used to trigger warnings.
4typedef typeof(sizeof(int)) size_t;
5void *malloc(size_t);
6void free(void *);
7#define NULL ((void*)0)
8#define UINT_MAX (~0U)
9
10// Each of these adjusted ranges has an adjustment small enough to split the
11// solution range across an overflow boundary (Min for <, Max for >).
12// This corresponds to one set of branches in RangeConstraintManager.
13void smallAdjustmentGT (unsigned a) {
14  void *b = NULL;
15  if (a+2 > 1)
16    b = malloc(1);
17  if (a == UINT_MAX-1 || a == UINT_MAX)
18    return; // no-warning
19  else if (a < UINT_MAX-1)
20    free(b);
21  return; // no-warning
22}
23
24void smallAdjustmentGE (unsigned a) {
25  void *b = NULL;
26  if (a+2 >= 1)
27    b = malloc(1);
28  if (a == UINT_MAX-1)
29    return; // no-warning
30  else if (a < UINT_MAX-1 || a == UINT_MAX)
31    free(b);
32  return; // no-warning
33}
34
35void smallAdjustmentLT (unsigned a) {
36  void *b = NULL;
37  if (a+1 < 2)
38    b = malloc(1);
39  if (a == 0 || a == UINT_MAX)
40    free(b);
41  return; // no-warning
42}
43
44void smallAdjustmentLE (unsigned a) {
45  void *b = NULL;
46  if (a+1 <= 2)
47    b = malloc(1);
48  if (a == 0 || a == 1 || a == UINT_MAX)
49    free(b);
50  return; // no-warning
51}
52
53
54// Each of these adjusted ranges has an adjustment large enough to push the
55// comparison value over an overflow boundary (Min for <, Max for >).
56// This corresponds to one set of branches in RangeConstraintManager.
57void largeAdjustmentGT (unsigned a) {
58  void *b = NULL;
59  if (a-2 > UINT_MAX-1)
60    b = malloc(1);
61  if (a == 1 || a == 0)
62    free(b);
63  else if (a > 1)
64    free(b);
65  return; // no-warning
66}
67
68void largeAdjustmentGE (unsigned a) {
69  void *b = NULL;
70  if (a-2 >= UINT_MAX-1)
71    b = malloc(1);
72  if (a > 1)
73    return; // no-warning
74  else if (a == 1 || a == 0)
75    free(b);
76  return; // no-warning
77}
78
79void largeAdjustmentLT (unsigned a) {
80  void *b = NULL;
81  if (a+2 < 1)
82    b = malloc(1);
83  if (a == UINT_MAX-1 || a == UINT_MAX)
84    free(b);
85  else if (a < UINT_MAX-1)
86    return; // no-warning
87  return; // no-warning
88}
89
90void largeAdjustmentLE (unsigned a) {
91  void *b = NULL;
92  if (a+2 <= 1)
93    b = malloc(1);
94  if (a < UINT_MAX-1)
95    return; // no-warning
96  else if (a == UINT_MAX-1 || a == UINT_MAX)
97    free(b);
98  return; // no-warning
99}
100