summaryrefslogtreecommitdiffstats
path: root/pyarg-parsetuple.cocci
blob: dd317029bcf51b6e9c2c5bdd03031b7bf05275fc (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
// Copyright © 2009 Red Hat, Inc.
//
// This software is licensed to you under the GNU Lesser General Public
// License, version 2.1 (LGPLv2.1). There is NO WARRANTY for this software,
// express or implied, including the implied warranties of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. You should have received a copy of
// LGPLv2.1 along with this software; if not, see
// http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
//
// Red Hat trademarks are not licensed under LGPLv2.1. No permission is
// granted to use or replicate Red Hat trademarks that are incorporated in
// this software or its documentation.
// 
// Red Hat Author(s): David Hugh Malcolm <dmalcolm@redhat.com>

@initialize:python@
"""
Analyze format strings passed to variadic function, compare to vararg types actually passed
"""
import sys
sys.path.append('.')
from validate import validate_type
num_errors = 0

@ check_PyArg_ParseTuple @
position pos;
expression args;
expression fmt;
expression list[len_E] E;
expression list[len_F] F;
type t;
t e;
@@

PyArg_ParseTuple@pos(args, fmt, E, e, F)

@script:python@
pos << check_PyArg_ParseTuple.pos;
fmt << check_PyArg_ParseTuple.fmt;
len_E << check_PyArg_ParseTuple.len_E;
len_F << check_PyArg_ParseTuple.len_F;
t << check_PyArg_ParseTuple.t;
@@
num_errors += validate_type(pos[0], 
    fmt.expr,
    int(len_E), 
    int(len_E) + 1 + int(len_F),
    t)



# likewise for PyArg_Parse etc:

@ check_PyArg_Parse @
position pos;
expression args;
expression fmt;
expression list[len_E] E;
expression list[len_F] F;
type t;
t e;
@@

PyArg_Parse@pos(args, fmt, E, e, F)

@script:python@
pos << check_PyArg_Parse.pos;
fmt << check_PyArg_Parse.fmt;
len_E << check_PyArg_Parse.len_E;
len_F << check_PyArg_Parse.len_F;
t << check_PyArg_Parse.t;
@@
num_errors += validate_type(pos[0], 
    fmt.expr,
    int(len_E), 
    int(len_E) + 1 + int(len_F),
    t)



@ check_PyArg_ParseTupleAndKeywords @
position pos;
expression args;
expression keywords;
expression fmt;
expression argnames;
expression list[len_E] E;
expression list[len_F] F;
type t;
t e;
@@

PyArg_ParseTupleAndKeywords@pos(args, keywords, fmt, argnames, E, e, F)

@script:python@
pos << check_PyArg_ParseTupleAndKeywords.pos;
fmt << check_PyArg_ParseTupleAndKeywords.fmt;
len_E << check_PyArg_ParseTupleAndKeywords.len_E;
len_F << check_PyArg_ParseTupleAndKeywords.len_F;
t << check_PyArg_ParseTupleAndKeywords.t;
@@
num_errors += validate_type(pos[0], 
    fmt.expr,
    int(len_E), 
    int(len_E) + 1 + int(len_F),
    t)




@script:python @
@@
# Shutdown hook: report the number of errors for use in Makefiles etc:
sys.exit(num_errors)