gh-114569: Use PyMem_* APIs for most non-PyObject uses (#114574)

Fix usage in Modules, Objects, and Parser subdirectories.
This commit is contained in:
Erlend E. Aasland 2024-01-26 11:11:35 +01:00 committed by GitHub
parent d0f7f5c41d
commit dcd28b5c35
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 33 deletions

View file

@ -267,7 +267,7 @@ typedef struct {
LOCAL(int)
create_extra(ElementObject* self, PyObject* attrib)
{
self->extra = PyObject_Malloc(sizeof(ElementObjectExtra));
self->extra = PyMem_Malloc(sizeof(ElementObjectExtra));
if (!self->extra) {
PyErr_NoMemory();
return -1;
@ -295,10 +295,11 @@ dealloc_extra(ElementObjectExtra *extra)
for (i = 0; i < extra->length; i++)
Py_DECREF(extra->children[i]);
if (extra->children != extra->_children)
PyObject_Free(extra->children);
if (extra->children != extra->_children) {
PyMem_Free(extra->children);
}
PyObject_Free(extra);
PyMem_Free(extra);
}
LOCAL(void)
@ -495,14 +496,16 @@ element_resize(ElementObject* self, Py_ssize_t extra)
* "children", which needs at least 4 bytes. Although it's a
* false alarm always assume at least one child to be safe.
*/
children = PyObject_Realloc(self->extra->children,
size * sizeof(PyObject*));
if (!children)
children = PyMem_Realloc(self->extra->children,
size * sizeof(PyObject*));
if (!children) {
goto nomemory;
}
} else {
children = PyObject_Malloc(size * sizeof(PyObject*));
if (!children)
children = PyMem_Malloc(size * sizeof(PyObject*));
if (!children) {
goto nomemory;
}
/* copy existing children from static area to malloc buffer */
memcpy(children, self->extra->children,
self->extra->length * sizeof(PyObject*));
@ -3044,7 +3047,7 @@ _elementtree_TreeBuilder_start_impl(TreeBuilderObject *self, PyObject *tag,
#define EXPAT(st, func) ((st)->expat_capi->func)
static XML_Memory_Handling_Suite ExpatMemoryHandler = {
PyObject_Malloc, PyObject_Realloc, PyObject_Free};
PyMem_Malloc, PyMem_Realloc, PyMem_Free};
typedef struct {
PyObject_HEAD