[Pyrex] Calling C varargs function with a NULL pointer

Sven Berkvens-Matthijsse sven at pyrex.berkvens.net
Wed May 23 13:10:12 UTC 2007


Hello,

It seems that Pyrex uses the constant "0" in generated C files where
a NULL pointer is meant. This is never a problem (because the C
compiler knows what it has to do), EXCEPT in C functions that take
varargs.

For example, consider the following Pyrex source code:

---------------------------------------------------------------------
cdef extern from *:
    void test(char *something, ...)

def check_test():
    test("hello", "whatever", "blah", NULL)
---------------------------------------------------------------------

The Pyrex compiler produces the following code for this (stripped
down, obviously):

---------------------------------------------------------------------
static char (__pyx_k1[]) = "hello";
static char (__pyx_k2[]) = "whatever";
static char (__pyx_k3[]) = "blah";

static PyObject *__pyx_f_12varargs_null_check_test(PyObject *__pyx_self,
               PyObject *__pyx_args, PyObject *__pyx_kwds)
{
    ....
    test(__pyx_k1,__pyx_k2,__pyx_k3,0);
    ....
}
---------------------------------------------------------------------

On 64-bit platforms, the integer 0 (often 32 bits) is pushed onto the
stack here, instead of a 64-bit NULL pointer. This causes problems for
the "test" function, which reads 64 bits and gets 32 bits of garbage
along with the 32 bits of 0.

Pyrex should use ((void *)0) for NULL pointers. This should always
work, even when the C compiler cannot infer that it should make the
0 a pointer.

-- 
Sven



More information about the Pyrex mailing list