This program makes Python segfault - no other does

Juho Saarikko sorry at but.no.spam
Mon May 17 17:33:35 EDT 2004


On Mon, 17 May 2004 15:28:02 -0400, Tim Peters wrote:

>> PyObject *unQuoteBytea(char *sin)
>> {
>>     int i, j, slen, byte;
>>     char *sout;
>>     PyObject *result;
>> 
>>     slen = strlen(sin);
>>     sout = (char *)PyMem_Malloc(slen);
> 
> You're in trouble already here.  strlen(sin) does not count the trailing NUL
> byte, so you haven't allocated enough memory for sout to hold a
> NUL-terminated copy of sin.  There may or may not be other C bugs here, but
> for starters change the last line to
> 
>>     sout = (char *)PyMem_Malloc(slen + 1);

YES ! It works ! After making this change and recompiling the module, the
segfaults stopped. Thank you, you saved me days of work. I'll send the
fix to the pyPgSQL project.

> BTW, running under a debug-build Python would have told you that the program
> wrote beyond the bounds of the memory allocated for sout.

It didn't. But, as I said, it was the first time I tried using a debugger.
And I only read enough instructions to figure out how to get a stack trace.



More information about the Python-list mailing list