PyMem_MALLOC (was [Python-Dev] Snake farm)

M.-A. Lemburg mal@lemburg.com
Fri, 22 Nov 2002 16:52:20 +0100


arc Recht wrote:

> >> > So it looks like PyOblect_Free() was called with 0x800 as an argument,
> >> > which is a bogus pointer value.  Can you go up one stack level and see
> >> > what the value of k in function_call() is?
> >> 713        if (ADDRESS_IN_RANGE(p, pool->arenaindex)) {
> >> (gdb) up
> >> # 1  0x080dfef4 in function_call (func=0x826317c, arg=0x8256aac,
> >> kw=0x8269bdc) at Objects/funcobject.c:481
> >> 481            PyMem_DEL(k);
> >> (gdb) p k
> >> $1 = (struct _object **) 0x800
> >
> >
> > Well, then maybe you can follow MvL's suggestion and find out how come
> > this value was returned by PyMem_NEW(PyObject *, 2*nk)???
>
> The problem seems to be in the FreeBSD malloc implementation. malloc(0)
> returns 0x800 in all my tests. malloc(1) and up seems to work properly..

Maybe we have to relax the configure test a bit and set
the MALLOC_ZERO_RETURNS_NULL #define not only on NULL
returns, but on returns < 0x1000 as well ?! (or add
something to pyport.h along these lines specific to
FreeBSD)...

# check whether malloc(0) returns NULL or not
AC_MSG_CHECKING(what malloc(0) returns)
AC_CACHE_VAL(ac_cv_malloc_zero,
[AC_TRY_RUN([#include <stdio.h>
#ifdef HAVE_STDLIB
#include <stdlib.h>
#else
char *malloc(), *realloc();
int *free();
#endif
main() {
	char *p;
	p = malloc(0);
	if ((unsigned long)p < 0x1000) exit(1);
	p = realloc(p, 0);
	if ((unsigned long)p < 0x1000) exit(1);
	free(p);
	exit(0);
}],
ac_cv_malloc_zero=nonnull,
ac_cv_malloc_zero=null,
ac_cv_malloc_zero=nonnull)]) # XXX arm cross-compile?
AC_MSG_RESULT($ac_cv_malloc_zero)
if test "$ac_cv_malloc_zero" = null
then
   AC_DEFINE(MALLOC_ZERO_RETURNS_NULL, 1,
   [Define if malloc(0) returns a NULL pointer.])
fi

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
_______________________________________________________________________
eGenix.com -- Makers of the Python mx Extensions: mxDateTime,mxODBC,...
Python Consulting:                               http://www.egenix.com/
Python Software:                    http://www.egenix.com/files/python/