Skip to content

bpo-42519: Replace PyMem_MALLOC() with PyMem_Malloc() #23586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyObject_Free(void *ptr);


/* Macros */
// Deprecated aliases only kept for backward compatibility.
#define PyObject_MALLOC PyObject_Malloc
#define PyObject_REALLOC PyObject_Realloc
#define PyObject_FREE PyObject_Free
Expand Down Expand Up @@ -138,8 +138,8 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
#define PyObject_NewVar(type, typeobj, n) \
( (type *) _PyObject_NewVar((typeobj), (n)) )

// Alias to PyObject_New(). In Python 3.8, PyObject_NEW() called directly
// PyObject_MALLOC() with _PyObject_VAR_SIZE().
// Alias to PyObject_NewVar(). In Python 3.8, PyObject_NEW_VAR() called
// directly PyObject_MALLOC() with _PyObject_VAR_SIZE().
#define PyObject_NEW_VAR(type, typeobj, n) PyObject_NewVar(type, typeobj, n)


Expand Down
32 changes: 9 additions & 23 deletions Include/pymem.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ PyAPI_FUNC(void *) PyMem_Malloc(size_t size);
PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);
PyAPI_FUNC(void) PyMem_Free(void *ptr);

/* Macros. */

/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL
for malloc(0), which would be treated as an error. Some platforms
would return a pointer with no memory behind it, which would break
pymalloc. To solve these problems, allocate an extra byte. */
/* Returns NULL to indicate error if a negative size or size larger than
Py_ssize_t can represent is supplied. Helps prevents security holes. */
#define PyMem_MALLOC(n) PyMem_Malloc(n)
#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
#define PyMem_FREE(p) PyMem_Free(p)

/*
* Type-oriented memory interface
* ==============================
Expand All @@ -78,9 +66,6 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
#define PyMem_New(type, n) \
( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
( (type *) PyMem_Malloc((n) * sizeof(type)) ) )
#define PyMem_NEW(type, n) \
( ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
( (type *) PyMem_MALLOC((n) * sizeof(type)) ) )

/*
* The value of (p) is always clobbered by this macro regardless of success.
Expand All @@ -91,15 +76,16 @@ PyAPI_FUNC(void) PyMem_Free(void *ptr);
#define PyMem_Resize(p, type, n) \
( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
(type *) PyMem_Realloc((p), (n) * sizeof(type)) )
#define PyMem_RESIZE(p, type, n) \
( (p) = ((size_t)(n) > PY_SSIZE_T_MAX / sizeof(type)) ? NULL : \
(type *) PyMem_REALLOC((p), (n) * sizeof(type)) )

/* PyMem{Del,DEL} are left over from ancient days, and shouldn't be used
* anymore. They're just confusing aliases for PyMem_{Free,FREE} now.
*/
#define PyMem_Del PyMem_Free
#define PyMem_DEL PyMem_FREE

// Deprecated aliases only kept for backward compatibility.
#define PyMem_MALLOC(n) PyMem_Malloc(n)
#define PyMem_NEW(type, n) PyMem_New(type, n)
#define PyMem_REALLOC(p, n) PyMem_Realloc(p, n)
#define PyMem_RESIZE(p, type, n) PyMem_Resize(p, type, n)
#define PyMem_FREE(p) PyMem_Free(p)
#define PyMem_Del(p) PyMem_Free(p)
#define PyMem_DEL(p) PyMem_Free(p)


#ifndef Py_LIMITED_API
Expand Down
4 changes: 2 additions & 2 deletions Modules/_localemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ _locale_strcoll_impl(PyObject *module, PyObject *os1, PyObject *os2)
result = PyLong_FromLong(wcscoll(ws1, ws2));
done:
/* Deallocate everything. */
if (ws1) PyMem_FREE(ws1);
if (ws2) PyMem_FREE(ws2);
if (ws1) PyMem_Free(ws1);
if (ws2) PyMem_Free(ws2);
return result;
}
#endif
Expand Down
24 changes: 12 additions & 12 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ Pdata_dealloc(Pdata *self)
while (--i >= 0) {
Py_DECREF(self->data[i]);
}
PyMem_FREE(self->data);
PyMem_Free(self->data);
PyObject_Del(self);
}

Expand All @@ -465,7 +465,7 @@ Pdata_New(void)
self->mark_set = 0;
self->fence = 0;
self->allocated = 8;
self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *));
self->data = PyMem_Malloc(self->allocated * sizeof(PyObject *));
if (self->data)
return (PyObject *)self;
Py_DECREF(self);
Expand Down Expand Up @@ -726,7 +726,7 @@ static PyTypeObject Unpickler_Type;
static PyMemoTable *
PyMemoTable_New(void)
{
PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable));
PyMemoTable *memo = PyMem_Malloc(sizeof(PyMemoTable));
if (memo == NULL) {
PyErr_NoMemory();
return NULL;
Expand All @@ -735,9 +735,9 @@ PyMemoTable_New(void)
memo->mt_used = 0;
memo->mt_allocated = MT_MINSIZE;
memo->mt_mask = MT_MINSIZE - 1;
memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry));
memo->mt_table = PyMem_Malloc(MT_MINSIZE * sizeof(PyMemoEntry));
if (memo->mt_table == NULL) {
PyMem_FREE(memo);
PyMem_Free(memo);
PyErr_NoMemory();
return NULL;
}
Expand All @@ -758,10 +758,10 @@ PyMemoTable_Copy(PyMemoTable *self)
new->mt_mask = self->mt_mask;
/* The table we get from _New() is probably smaller than we wanted.
Free it and allocate one that's the right size. */
PyMem_FREE(new->mt_table);
PyMem_Free(new->mt_table);
new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated);
if (new->mt_table == NULL) {
PyMem_FREE(new);
PyMem_Free(new);
PyErr_NoMemory();
return NULL;
}
Expand Down Expand Up @@ -800,8 +800,8 @@ PyMemoTable_Del(PyMemoTable *self)
return;
PyMemoTable_Clear(self);

PyMem_FREE(self->mt_table);
PyMem_FREE(self);
PyMem_Free(self->mt_table);
PyMem_Free(self);
}

/* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup()
Expand Down Expand Up @@ -880,7 +880,7 @@ _PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size)
}

/* Deallocate the old table. */
PyMem_FREE(oldtable);
PyMem_Free(oldtable);
return 0;
}

Expand Down Expand Up @@ -1582,7 +1582,7 @@ _Unpickler_MemoCleanup(UnpicklerObject *self)
while (--i >= 0) {
Py_XDECREF(memo[i]);
}
PyMem_FREE(memo);
PyMem_Free(memo);
}

static UnpicklerObject *
Expand Down Expand Up @@ -7544,7 +7544,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored
for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) {
Py_XDECREF(new_memo[i]);
}
PyMem_FREE(new_memo);
PyMem_Free(new_memo);
}
return -1;
}
Expand Down
8 changes: 4 additions & 4 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ static void
data_stack_dealloc(SRE_STATE* state)
{
if (state->data_stack) {
PyMem_FREE(state->data_stack);
PyMem_Free(state->data_stack);
state->data_stack = NULL;
}
state->data_stack_size = state->data_stack_base = 0;
Expand All @@ -213,7 +213,7 @@ data_stack_grow(SRE_STATE* state, Py_ssize_t size)
void* stack;
cursize = minsize+minsize/4+1024;
TRACE(("allocate/grow stack %zd\n", cursize));
stack = PyMem_REALLOC(state->data_stack, cursize);
stack = PyMem_Realloc(state->data_stack, cursize);
if (!stack) {
data_stack_dealloc(state);
return SRE_ERROR_MEMORY;
Expand Down Expand Up @@ -472,7 +472,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
/* We add an explicit cast here because MSVC has a bug when
compiling C code where it believes that `const void**` cannot be
safely casted to `void*`, see bpo-39943 for details. */
PyMem_Del((void*) state->mark);
PyMem_Free((void*) state->mark);
state->mark = NULL;
if (state->buffer.buf)
PyBuffer_Release(&state->buffer);
Expand All @@ -487,7 +487,7 @@ state_fini(SRE_STATE* state)
Py_XDECREF(state->string);
data_stack_dealloc(state);
/* See above PyMem_Del for why we explicitly cast here. */
PyMem_Del((void*) state->mark);
PyMem_Free((void*) state->mark);
state->mark = NULL;
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3306,10 +3306,10 @@ context_dealloc(PySSLContext *self)
context_clear(self);
SSL_CTX_free(self->ctx);
#if HAVE_NPN
PyMem_FREE(self->npn_protocols);
PyMem_Free(self->npn_protocols);
#endif
#if HAVE_ALPN
PyMem_FREE(self->alpn_protocols);
PyMem_Free(self->alpn_protocols);
#endif
Py_TYPE(self)->tp_free(self);
Py_DECREF(tp);
Expand Down Expand Up @@ -3510,7 +3510,7 @@ _ssl__SSLContext__set_alpn_protocols_impl(PySSLContext *self,
return NULL;
}

PyMem_FREE(self->alpn_protocols);
PyMem_Free(self->alpn_protocols);
self->alpn_protocols = PyMem_Malloc(protos->len);
if (!self->alpn_protocols)
return PyErr_NoMemory();
Expand Down
6 changes: 3 additions & 3 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -1373,14 +1373,14 @@ prepare_s(PyStructObject *self)

self->s_size = size;
self->s_len = len;
codes = PyMem_MALLOC((ncodes + 1) * sizeof(formatcode));
codes = PyMem_Malloc((ncodes + 1) * sizeof(formatcode));
if (codes == NULL) {
PyErr_NoMemory();
return -1;
}
/* Free any s_codes value left over from a previous initialization. */
if (self->s_codes != NULL)
PyMem_FREE(self->s_codes);
PyMem_Free(self->s_codes);
self->s_codes = codes;

s = fmt;
Expand Down Expand Up @@ -1502,7 +1502,7 @@ s_dealloc(PyStructObject *s)
if (s->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *)s);
if (s->s_codes != NULL) {
PyMem_FREE(s->s_codes);
PyMem_Free(s->s_codes);
}
Py_XDECREF(s->s_format);
freefunc free_func = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1988,12 +1988,12 @@ unicode_asucs4(PyObject *self, PyObject *args)
buffer[str_len] = 0xffffU;

if (!PyUnicode_AsUCS4(unicode, buffer, buf_len, copy_null)) {
PyMem_FREE(buffer);
PyMem_Free(buffer);
return NULL;
}

result = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, buf_len);
PyMem_FREE(buffer);
PyMem_Free(buffer);
return result;
}

Expand Down
6 changes: 3 additions & 3 deletions Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ t_bootstrap(void *boot_raw)
Py_DECREF(boot->func);
Py_DECREF(boot->args);
Py_XDECREF(boot->keyw);
PyMem_DEL(boot_raw);
PyMem_Free(boot_raw);
tstate->interp->num_threads--;
PyThreadState_Clear(tstate);
_PyThreadState_DeleteCurrent(tstate);
Expand Down Expand Up @@ -1107,7 +1107,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
boot->tstate = _PyThreadState_Prealloc(boot->interp);
boot->runtime = runtime;
if (boot->tstate == NULL) {
PyMem_DEL(boot);
PyMem_Free(boot);
return PyErr_NoMemory();
}
Py_INCREF(func);
Expand All @@ -1121,7 +1121,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Py_DECREF(args);
Py_XDECREF(keyw);
PyThreadState_Clear(boot->tstate);
PyMem_DEL(boot);
PyMem_Free(boot);
return NULL;
}
return PyLong_FromUnsignedLong(ident);
Expand Down
8 changes: 4 additions & 4 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2472,7 +2472,7 @@ PythonCmdDelete(ClientData clientData)
ENTER_PYTHON
Py_XDECREF(data->self);
Py_XDECREF(data->func);
PyMem_DEL(data);
PyMem_Free(data);
LEAVE_PYTHON
}

Expand Down Expand Up @@ -2545,7 +2545,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
CommandEvent *ev = (CommandEvent*)attemptckalloc(sizeof(CommandEvent));
if (ev == NULL) {
PyErr_NoMemory();
PyMem_DEL(data);
PyMem_Free(data);
return NULL;
}
ev->ev.proc = (Tcl_EventProc*)Tkapp_CommandProc;
Expand All @@ -2568,7 +2568,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
}
if (err) {
PyErr_SetString(Tkinter_TclError, "can't create Tcl command");
PyMem_DEL(data);
PyMem_Free(data);
return NULL;
}

Expand Down Expand Up @@ -2666,7 +2666,7 @@ DeleteFHCD(int id)
*pp = p->next;
Py_XDECREF(p->func);
Py_XDECREF(p->file);
PyMem_DEL(p);
PyMem_Free(p);
}
else
pp = &p->next;
Expand Down
4 changes: 2 additions & 2 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ array_resize(arrayobject *self, Py_ssize_t newsize)
}

if (newsize == 0) {
PyMem_FREE(self->ob_item);
PyMem_Free(self->ob_item);
self->ob_item = NULL;
Py_SET_SIZE(self, 0);
self->allocated = 0;
Expand Down Expand Up @@ -652,7 +652,7 @@ array_dealloc(arrayobject *op)
if (op->weakreflist != NULL)
PyObject_ClearWeakRefs((PyObject *) op);
if (op->ob_item != NULL)
PyMem_DEL(op->ob_item);
PyMem_Free(op->ob_item);
Py_TYPE(op)->tp_free((PyObject *)op);
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/cjkcodecs/multibytecodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1191,13 +1191,13 @@ _multibytecodec_MultibyteIncrementalDecoder_decode_impl(MultibyteIncrementalDeco
goto errorexit;

if (wdata != data)
PyMem_Del(wdata);
PyMem_Free(wdata);
Py_XDECREF(buf.excobj);
return res;

errorexit:
if (wdata != NULL && wdata != data)
PyMem_Del(wdata);
PyMem_Free(wdata);
Py_XDECREF(buf.excobj);
_PyUnicodeWriter_Dealloc(&buf.writer);
return NULL;
Expand Down
Loading