summaryrefslogtreecommitdiffstats
path: root/pyarg-parsetuple.cocci
blob: 111d91ae2f839e4c06e5c7732a04bba44033ce0f (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
@initialize:python@
"""
Analyze format strings, compare to vararg types actually passed

FIXME: generalize this to varargs; can coccinelle do this?
"""
import sys
sys.path.append('.')
from validate import validate_types
num_errors = 0

@ParseTuple_1@
position pos;
expression args;
expression fmt;
type t1;
t1 e1;
@@

PyArg_ParseTuple@pos(args, fmt, e1)

@script:python@
pos << ParseTuple_1.pos;
args << ParseTuple_1.args;
fmt << ParseTuple_1.fmt;
t1 << ParseTuple_1.t1;
@@

# For some reason, locations are coming as a 1-tuple containing a Location (from
# coccilibs.elems), rather than the location itself
# Hence we use p1[0], not p1

num_errors += validate_types(pos[0], fmt.expr, [t1])



@ParseTuple_2@
position pos;
expression args;
expression fmt;
type t1;
t1 e1;
type t2;
t2 e2;
@@

PyArg_ParseTuple(args@pos, fmt, e1, e2)

@script:python@
args << ParseTuple_2.args;
fmt << ParseTuple_2.fmt;
pos << ParseTuple_2.pos;
t1 << ParseTuple_2.t1;
t2 << ParseTuple_2.t2;
@@
num_errors += validate_types(pos[0], fmt.expr, [t1, t2])



@ParseTuple_3@
position pos;
expression args;
expression fmt;
type t1; t1 e1;
type t2; t2 e2;
type t3; t3 e3;
@@

PyArg_ParseTuple(args@pos, fmt, e1, e2, e3)

@script:python@
pos << ParseTuple_3.pos;
args << ParseTuple_3.args;
fmt << ParseTuple_3.fmt;
pos << ParseTuple_3.pos;
t1 << ParseTuple_3.t1;
t2 << ParseTuple_3.t2;
t3 << ParseTuple_3.t3;
@@
num_errors += validate_types(pos[0], fmt.expr, [t1, t2, t3])


@script:python @
@@
sys.exit(num_errors)