summaryrefslogtreecommitdiffstats
path: root/Include/pymem.h
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2002-03-02 08:43:19 (GMT)
committerTim Peters <tim.peters@gmail.com>2002-03-02 08:43:19 (GMT)
commita5d78cc208c24be62b50b6d1540e03be6612d3f2 (patch)
treeeda122d61fdabb1c1579ab4d8ff0898872a15f7c /Include/pymem.h
parentb77d343bc846c2049a4cffb1dfd65eb49d1728b4 (diff)
downloadcpython-a5d78cc208c24be62b50b6d1540e03be6612d3f2.zip
cpython-a5d78cc208c24be62b50b6d1540e03be6612d3f2.tar.gz
cpython-a5d78cc208c24be62b50b6d1540e03be6612d3f2.tar.bz2
Whether platform malloc(0) returns NULL has nothing to do with whether
platform realloc(p, 0) returns NULL, so MALLOC_ZERO_RETURNS_NULL can be correctly undefined yet realloc(p, 0) can return NULL anyway. Prevent realloc(p, 0) doing free(p) and returning NULL via a different hack. Would probably be better to get rid of MALLOC_ZERO_RETURNS_NULL entirely. Bugfix candidate.
Diffstat (limited to 'Include/pymem.h')
-rw-r--r--Include/pymem.h19
1 files changed, 12 insertions, 7 deletions
diff --git a/Include/pymem.h b/Include/pymem.h
index 86a69e4..d09f38c 100644
--- a/Include/pymem.h
+++ b/Include/pymem.h
@@ -111,13 +111,18 @@ extern DL_IMPORT(void) PyMem_Free(void *);
/* Macros */
#define PyMem_NEW(type, n) \
( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
-#define PyMem_RESIZE(p, type, n) \
- if ((p) == NULL) \
- (p) = (type *)(PyMem_MALLOC( \
- _PyMem_EXTRA + (n) * sizeof(type))); \
- else \
- (p) = (type *)(PyMem_REALLOC((p), \
- _PyMem_EXTRA + (n) * sizeof(type)))
+
+/* See comment near MALLOC_ZERO_RETURNS_NULL in pyport.h. */
+#define PyMem_RESIZE(p, type, n) \
+ do { \
+ size_t _sum = (n) * sizeof(type); \
+ if (!_sum) \
+ _sum = 1; \
+ (p) = (type *)((p) ? \
+ PyMem_REALLOC(p, _sum) : \
+ PyMem_MALLOC(_sum)); \
+ } while (0)
+
#define PyMem_DEL(p) PyMem_FREE(p)
/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,