blob: 056110f57904afff34276e62938fec673332d98f [file] [log] [blame]
Jordy Rose9e607dd2012-05-03 07:33:56 +00001// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode,unix.Malloc -verify -analyzer-constraints=range %s
Jordy Roseba0f61c2010-06-18 22:49:11 +00002
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)
Jordy Rose9e607dd2012-05-03 07:33:56 +00008#define UINT_MAX (~0U)
Jordy Roseba0f61c2010-06-18 22:49:11 +00009
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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000014 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000015 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000025 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000026 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000036 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000037 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000045 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000046 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000058 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000059 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000069 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000070 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000080 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000081 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) {
Jordy Rose9e607dd2012-05-03 07:33:56 +000091 void *b = NULL;
Jordy Roseba0f61c2010-06-18 22:49:11 +000092 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}