Lines 66-73
PyAPI_FUNC(void) PyMem_Free(void *);
Link Here
|
66 |
for malloc(0), which would be treated as an error. Some platforms |
66 |
for malloc(0), which would be treated as an error. Some platforms |
67 |
would return a pointer with no memory behind it, which would break |
67 |
would return a pointer with no memory behind it, which would break |
68 |
pymalloc. To solve these problems, allocate an extra byte. */ |
68 |
pymalloc. To solve these problems, allocate an extra byte. */ |
69 |
#define PyMem_MALLOC(n) malloc((n) ? (n) : 1) |
69 |
/* Returns NULL to indicate error if a negative size or size larger than |
70 |
#define PyMem_REALLOC(p, n) realloc((p), (n) ? (n) : 1) |
70 |
Py_ssize_t can represent is supplied. Helps prevents security holes. */ |
|
|
71 |
#define PyMem_MALLOC(n) (((n) < 0 || (n) > INT_MAX) ? NULL \ |
72 |
: malloc((n) ? (n) : 1)) |
73 |
#define PyMem_REALLOC(p, n) (((n) < 0 || (n) > INT_MAX) ? NULL \ |
74 |
: realloc((p), (n) ? (n) : 1)) |
71 |
|
75 |
|
72 |
#endif /* PYMALLOC_DEBUG */ |
76 |
#endif /* PYMALLOC_DEBUG */ |
73 |
|
77 |
|
Lines 80-103
PyAPI_FUNC(void) PyMem_Free(void *);
Link Here
|
80 |
* Type-oriented memory interface |
84 |
* Type-oriented memory interface |
81 |
* ============================== |
85 |
* ============================== |
82 |
* |
86 |
* |
83 |
* These are carried along for historical reasons. There's rarely a good |
87 |
* Allocate memory for n objects of the given type. Returns a new pointer |
84 |
* reason to use them anymore (you can just as easily do the multiply and |
88 |
* or NULL if the request was too large or memory allocation failed. Use |
85 |
* cast yourself). |
89 |
* these macros rather than doing the multiplication yourself so that proper |
|
|
90 |
* overflow checking is always done. |
86 |
*/ |
91 |
*/ |
87 |
|
92 |
|
88 |
#define PyMem_New(type, n) \ |
93 |
#define PyMem_New(type, n) \ |
89 |
( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ |
94 |
( ((n) > INT_MAX / sizeof(type)) ? NULL : \ |
90 |
( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) |
95 |
( (type *) PyMem_Malloc((n) * sizeof(type)) ) ) |
91 |
#define PyMem_NEW(type, n) \ |
96 |
#define PyMem_NEW(type, n) \ |
92 |
( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ |
97 |
( ((n) > INT_MAX / sizeof(type)) ? NULL : \ |
93 |
( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) |
98 |
( (type *) PyMem_MALLOC((n) * sizeof(type)) ) ) |
94 |
|
99 |
|
|
|
100 |
/* |
101 |
* The value of (p) is always clobbered by this macro regardless of success. |
102 |
* The caller MUST check if (p) is NULL afterwards and deal with the memory |
103 |
* error if so. This means the original value of (p) MUST be saved for the |
104 |
* caller's memory error handler to not lose track of it. |
105 |
*/ |
95 |
#define PyMem_Resize(p, type, n) \ |
106 |
#define PyMem_Resize(p, type, n) \ |
96 |
( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ |
107 |
( (p) = ((n) > INT_MAX / sizeof(type)) ? NULL : \ |
97 |
( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) ) ) |
108 |
(type *) PyMem_Realloc((p), (n) * sizeof(type)) ) |
98 |
#define PyMem_RESIZE(p, type, n) \ |
109 |
#define PyMem_RESIZE(p, type, n) \ |
99 |
( assert((n) <= PY_SIZE_MAX / sizeof(type)) , \ |
110 |
( (p) = ((n) > INT_MAX / sizeof(type)) ? NULL : \ |
100 |
( (p) = (type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) ) |
111 |
(type *) PyMem_REALLOC((p), (n) * sizeof(type)) ) |
101 |
|
112 |
|
102 |
/* In order to avoid breaking old code mixing PyObject_{New, NEW} with |
113 |
/* In order to avoid breaking old code mixing PyObject_{New, NEW} with |
103 |
PyMem_{Del, DEL} and PyMem_{Free, FREE}, the PyMem "release memory" |
114 |
PyMem_{Del, DEL} and PyMem_{Free, FREE}, the PyMem "release memory" |