summaryrefslogtreecommitdiff
path: root/clang/test/Analysis/additive-folding-range-constraints.c
blob: 32e0cfe142ad78818291d90344bf4a534dbd2d0e (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.core -verify -analyzer-constraints=range %s

// These are used to trigger warnings.
typedef typeof(sizeof(int)) size_t;
void *malloc(size_t);
void free(void *);
#define NULL ((void*)0)
#define UINT_MAX (__INT_MAX__  *2U +1U)

// Each of these adjusted ranges has an adjustment small enough to split the
// solution range across an overflow boundary (Min for <, Max for >).
// This corresponds to one set of branches in RangeConstraintManager.
void smallAdjustmentGT (unsigned a) {
  char* b = NULL;
  if (a+2 > 1)
    b = malloc(1);
  if (a == UINT_MAX-1 || a == UINT_MAX)
    return; // no-warning
  else if (a < UINT_MAX-1)
    free(b);
  return; // no-warning
}

void smallAdjustmentGE (unsigned a) {
  char* b = NULL;
  if (a+2 >= 1)
    b = malloc(1);
  if (a == UINT_MAX-1)
    return; // no-warning
  else if (a < UINT_MAX-1 || a == UINT_MAX)
    free(b);
  return; // no-warning
}

void smallAdjustmentLT (unsigned a) {
  char* b = NULL;
  if (a+1 < 2)
    b = malloc(1);
  if (a == 0 || a == UINT_MAX)
    free(b);
  return; // no-warning
}

void smallAdjustmentLE (unsigned a) {
  char* b = NULL;
  if (a+1 <= 2)
    b = malloc(1);
  if (a == 0 || a == 1 || a == UINT_MAX)
    free(b);
  return; // no-warning
}


// Each of these adjusted ranges has an adjustment large enough to push the
// comparison value over an overflow boundary (Min for <, Max for >).
// This corresponds to one set of branches in RangeConstraintManager.
void largeAdjustmentGT (unsigned a) {
  char* b = NULL;
  if (a-2 > UINT_MAX-1)
    b = malloc(1);
  if (a == 1 || a == 0)
    free(b);
  else if (a > 1)
    free(b);
  return; // no-warning
}

void largeAdjustmentGE (unsigned a) {
  char* b = NULL;
  if (a-2 >= UINT_MAX-1)
    b = malloc(1);
  if (a > 1)
    return; // no-warning
  else if (a == 1 || a == 0)
    free(b);
  return; // no-warning
}

void largeAdjustmentLT (unsigned a) {
  char* b = NULL;
  if (a+2 < 1)
    b = malloc(1);
  if (a == UINT_MAX-1 || a == UINT_MAX)
    free(b);
  else if (a < UINT_MAX-1)
    return; // no-warning
  return; // no-warning
}

void largeAdjustmentLE (unsigned a) {
  char* b = NULL;
  if (a+2 <= 1)
    b = malloc(1);
  if (a < UINT_MAX-1)
    return; // no-warning
  else if (a == UINT_MAX-1 || a == UINT_MAX)
    free(b);
  return; // no-warning
}