summaryrefslogtreecommitdiffstats
path: root/pacemaker/rewrite_snprintf.cocci
blob: 809363640d15ba1a52e188361f690a42233bcc17 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Switch certain class of snprintf to use internal macro crm_snprintf_max
// jpokorny@redhat.com

@ has_crm_internal @
@@

#include <crm_internal.h>


@ snprintf_eligible_1 exists @
expression E1, E2;
identifier fn, id_str, id_max, id_offset;
type t_max, t_offset;
@@
fn(...) {
...
(
t_offset id_offset = E1;
...
- static t_max id_max = E2;
+ static const t_max id_max = E2;
|
- static t_max id_max = E2;
+ static const t_max id_max = E2;
...
t_offset id_offset = E1;
|
t_offset id_offset = E1;
...
t_max id_max = E2;
|
t_max id_max = E2;
...
t_offset id_offset = E1;
//|
//t_offset id_offset = E1;
//|
//t_offset id_offset = E1, id_max = E2;
//|
//t_offset id_max = E2, id_offset = E1;
)
<+...
id_offset += snprintf(id_str + id_offset, id_max - id_offset, ...);
...+>
}


@ snprintf_eligible_2 exists @
identifier fn, id_str, id_max, id_offset;
type t_max, t_offset;
@@
fn(..., t_offset id_offset, t_max id_max,...)
{
<+...
id_offset += snprintf(id_str + id_offset, id_max - id_offset, ...);
...+>
}


@ snprintf_rewrite_1 depends on snprintf_eligible_1 forall @
identifier snprintf_eligible_1.fn;
identifier snprintf_eligible_1.id_str;
identifier snprintf_eligible_1.id_max;
identifier snprintf_eligible_1.id_offset;
@@
fn(...) {
<...
id_offset +=
- snprintf(id_str + id_offset, id_max - id_offset
+ crm_snprintf_offset(id_str, id_offset, id_max
, ...);
...>
}

@ snprintf_rewrite_2 depends on snprintf_eligible_2 forall @
identifier snprintf_eligible_2.fn;
identifier snprintf_eligible_2.id_str;
identifier snprintf_eligible_2.id_max;
identifier snprintf_eligible_2.id_offset;
@@
fn(...) {
<...
id_offset +=
- snprintf(id_str + id_offset, id_max - id_offset
+ crm_snprintf_offset(id_str, id_offset, id_max
, ...);
...>
}


@ snprintf_assert_1 depends on snprintf_eligible_1 forall @
identifier snprintf_eligible_1.fn;
identifier snprintf_eligible_1.id_max;
identifier snprintf_eligible_1.id_offset;
@@
fn(...) {
<...
- CRM_LOG_ASSERT(id_offset > 0);
+ CRM_LOG_ASSERT(id_offset > 0 && id_offset < id_max);
...>
}


@ snprintf_assert_2 depends on snprintf_eligible_2 forall @
identifier snprintf_eligible_2.fn;
identifier snprintf_eligible_2.id_max;
identifier snprintf_eligible_2.id_offset;
@@
fn(...) {
<...
- CRM_LOG_ASSERT(id_offset > 0);
+ CRM_LOG_ASSERT(id_offset > 0 && id_offset < id_max);
...>
}


// this is not the best placement, it serves a subsequent manual review
// to denote necessity to add the include at all, and the reviewer will
// (hopefully) move the include to the correct location
@ snprintf_include_1 depends on (snprintf_eligible_1 && !(has_crm_internal)) exists @
identifier snprintf_eligible_1.fn;
@@
+ #include <crm_internal.h>  /* crm_snprintf_offset */

fn(...) { ... }


@ snprintf_include_2 depends on (snprintf_eligible_2 && !(has_crm_internal)) exists @
identifier snprintf_eligible_2.fn;
@@
+ #include <crm_internal.h>  /* crm_snprintf_offset */

fn(...) { ... }