[Cython] Wrong order of __Pyx_DECREF when calling a function with an implicit str → char* conversion.

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Jun 1 02:06:38 CEST 2014


Stefan Behnel wrote:
> Stefan Behnel, 30.05.2014 15:01:
> 
>>Emmanuel Gil Peyrot, 28.05.2014 13:25:
>>
>>>I was testing my cython codebase on top of pypy/cpyext, and I found a
>>>memory corruption.  After investigating it with the pypy guys (thanks
>>>arigato!), we identified it as a cython bug:
>>>
>>> cdef void test(char *string):
>>>     print(string)
>>>
>>> def run(array):
>>>     test(array[0])

I wouldn't call it a bug, more a limitation. When casting a
Python object to char *, it's the programmer's responsibility
to ensure that a reference is kept to the underlying object
for as long as necessary.

Having said that, keeping the reference for the duration of
the call would result in less surprising behaviour in cases
like this.

>>Cython has a mechanism to reject this kind of code, not sure why it
>>wouldn't strike here.

Not sure about Cython, but Pyrex only catches the most glaring
cases of this, such as adding two string and then immediately
casting the result to char *, which is almost guaranteed to
fail. Trying to catch anything more would have resulted in huge
numbers of false positives and been very annoying, so I
didn't try.

-- 
Greg


More information about the cython-devel mailing list