[Cocci] Extracting types passed to variadic (varargs) functions

Julia Lawall julia at diku.dk
Mon Nov 16 23:14:32 CET 2009


On Mon, 16 Nov 2009, David Malcolm wrote:

> I'm new to Coccinelle/spatch and have been experimenting with using it
> upon CPython code [1]
> 
> I'm attempting to validate a mini-language used in format strings by a
> variadic API call.  My code examines the types of the
> variables passed as varargs, and attempts to check that they are
> pointers of the correct types, according to a set of rules [2].
> 
> Unfortunately, I couldn't see a way to match a variable-length list of
> expressions, capturing the type of each expression.

If you really want to analyze all of the arguments at once, I don't see 
how to do it.  But perhaps the following would be acceptable (not tested):

@check_PyArg_ParseTuple@
position pos;
expression args;
expresion fmt;
type t; t e;
expression list[n] E;
@@

PyArg_ParseTuple(args at pos, fmt, E, e, ...)

@script:python@
fmt << check_PyArg_ParseTuple.fmt;
pos << check_PyArg_ParseTuple.pos;
t << check_PyArg_ParseTuple.t;
n << check_PyArg_ParseTuple.n;
@@

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

(I'm not sure that the .expr is needed on fmt; or in any case I don't know 
what it does).

Now validate_types takes a position, a format string, the number of 
arguments between the format string and some argument, and that argument.  
The python code will be called on each argument individually.


> Also, I noticed that positions are passed to Python code as a 1-tuple
> containing a coccilibs.elems.Location, rather than just the Location
> itself.  Is this deliberate (e.g. to support ranges as well as
> positions)?

A single position variable can be bound to more than one position when 
using a nest (<... ...> for 0 or more occurrences of the contained pattren 
and <+... ...+> for 1 or more occurrences of the contained pattern).  For 
example, if f is a one-argument function, then the following will bind p 
to each occurrence of e in that argument:

f(<+...e at p...+>)

Then you can use for to iterate over them.

julia


More information about the Cocci mailing list