Index: Include/pymem.h =================================================================== --- Include/pymem.h (revision 697) +++ Include/pymem.h (working copy) @@ -78,8 +78,8 @@ #include "dlmalloc.h" -#define PyMem_MALLOC(n) dlmalloc((n)) -#define PyMem_REALLOC(p, n) dlrealloc((p), (n)) +#define PyMem_MALLOC(n) dlmalloc((n) ? (n) : 1) +#define PyMem_REALLOC(p, n) dlrealloc((p), (n) ? (n) : 1) #define PyMem_FREE dlfree #endif /* WITH_DLMALLOC */ Index: Objects/obmalloc.c =================================================================== --- Objects/obmalloc.c (revision 697) +++ Objects/obmalloc.c (working copy) @@ -976,9 +976,9 @@ * last chance to serve the request) or when the max memory limit * has been reached. */ -#ifndef WITH_DLMALLOC if (nbytes == 0) nbytes = 1; +#ifndef WITH_DLMALLOC return (void *)malloc(nbytes); #else return (void *)dlmalloc(nbytes); @@ -1262,19 +1262,22 @@ * a memory fault can occur if we try to copy nbytes bytes starting * at p. Instead we punt: let C continue to manage this block. */ + if (nbytes) #ifndef WITH_DLMALLOC - if (nbytes) - return realloc(p, nbytes); + return realloc(p, nbytes); +#else + return dlrealloc(p, nbytes); +#endif /* C doesn't define the result of realloc(p, 0) (it may or may not * return NULL then), but Python's docs promise that nbytes==0 never * returns NULL. We don't pass 0 to realloc(), to avoid that endcase * to begin with. Even then, we can't be sure that realloc() won't * return NULL. */ +#ifndef WITH_DLMALLOC bp = realloc(p, 1); #else - /* dlrealloc promises it won't return NULL for nbytes == 0 */ - bp = dlrealloc(p, nbytes); + bp = dlrealloc(p, 1); #endif return bp ? bp : p;