Core dump revisited

Duncan Booth duncan.booth at invalid.invalid
Tue Dec 19 13:59:34 EST 2006


"Sheldon" <shejo284 at gmail.com> wrote:

> 
> Duncan Booth skrev:
> 
>> "Sheldon" <shejo284 at gmail.com> wrote:
>>
>> > I am new to this and copied this code from a colleague. So, it
>> > corrupts the pointer. How do I do this properly?
>> >
>> Here is at least part of your problem:
>>
>>     msgop = PyList_GetItem(work.msgobj, i);
>>     work.msg_scenes[i] = PyString_AsString(msgop);
>>     ppsop = PyList_GetItem(work.ppsobj, i);
>>     work.pps_scenes[i] = PyString_AsString(ppsop);
>> ...
>>     free(work.pps_scenes[i]);
>>     free(work.msg_scenes[i]);
>>
>> You initialised msg_scenes and pps_scenes with a malloc'ed block but
>> you then just overwrote the pointer with the result of
>> PyString_AsString. You don't own the memory for the string returned
>> from PyString_AsString, so freeing it will cause a corruption. You
>> should copy the string data into the malloc'ed block (with
>> appropriate length checks). 
> 
> Do you mean with: PyString_FromStringAndSize() and
> PyString_Size(PyObject *string)
> 
If you wish, or even just strlen() if you aren't concerned about embedded 
nulls.




More information about the Python-list mailing list