Subject: Re: Can malloc return invalid memory?
To: D'Arcy J.M. Cain <darcy@druid.net>
From: Guenther Grau <Guenther.Grau@marconicomms.com>
List: current-users
Date: 04/03/2000 21:59:00
Hi,

> PyObject *
> PyString_FromStringAndSize(str, size)
>     const char *str;
>     int size;
> {
>     register PyStringObject *op;
>     op = (PyStringObject *)
>         malloc(sizeof(PyStringObject) + size * sizeof(char));
>     if (op == NULL)
>         return PyErr_NoMemory();
>     op->ob_type = &PyString_Type;
> 
> GDB tells me the following.
> 
> #0  PyString_FromStringAndSize (
>     str=0x9164f47 "2000:04:01:13:57:10:EST 200 931 /images/refnews.gif",
>     size=23) at stringobject.c:99
> 
> So I know that there is a string at least 23 characters and the size request
> is 23.  At this point the string is irrelevant anyway.
> 
> The program crashes on that last line with:
> 
> Program terminated with signal 11, Segmentation fault.
> 
> The value of op is 0x9233000.  When I try to print op->ob_type I get
> the following.
> 
> (gdb) p op->ob_type
> Cannot access memory at address 0x9233004.

What does 

(gdb) p *op

output? How is PyStringObject defined?

> Any ideas?

What platform are you running on? Maybe this is an alignment
problem? Is it reproduceable? Did you try to remove the
cast of the result of malloc to (PyStringObject *)?
Did you try to remove the register definition of op?
Maybe there is a code generation bug with gcc involved?
Did you build with/without optimization? Did you try a different
version of the compiler?

  Guenther