Skip to content

Commit 7c83eaa

Browse files
authored
bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)
1 parent b8eb376 commit 7c83eaa

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

Modules/pyexpat.c

+40-27
Original file line numberDiff line numberDiff line change
@@ -1836,6 +1836,13 @@ add_features(PyObject *mod)
18361836
}
18371837
#endif
18381838

1839+
static void
1840+
pyexpat_destructor(PyObject *op)
1841+
{
1842+
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
1843+
PyMem_Free(p);
1844+
}
1845+
18391846
static int
18401847
pyexpat_exec(PyObject *mod)
18411848
{
@@ -1921,40 +1928,46 @@ pyexpat_exec(PyObject *mod)
19211928
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
19221929
#undef MYCONST
19231930

1924-
static struct PyExpat_CAPI capi;
1931+
struct PyExpat_CAPI *capi = PyMem_Calloc(1, sizeof(struct PyExpat_CAPI));
1932+
if (capi == NULL) {
1933+
PyErr_NoMemory();
1934+
return -1;
1935+
}
19251936
/* initialize pyexpat dispatch table */
1926-
capi.size = sizeof(capi);
1927-
capi.magic = PyExpat_CAPI_MAGIC;
1928-
capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1929-
capi.MINOR_VERSION = XML_MINOR_VERSION;
1930-
capi.MICRO_VERSION = XML_MICRO_VERSION;
1931-
capi.ErrorString = XML_ErrorString;
1932-
capi.GetErrorCode = XML_GetErrorCode;
1933-
capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1934-
capi.GetErrorLineNumber = XML_GetErrorLineNumber;
1935-
capi.Parse = XML_Parse;
1936-
capi.ParserCreate_MM = XML_ParserCreate_MM;
1937-
capi.ParserFree = XML_ParserFree;
1938-
capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1939-
capi.SetCommentHandler = XML_SetCommentHandler;
1940-
capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1941-
capi.SetElementHandler = XML_SetElementHandler;
1942-
capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1943-
capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1944-
capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1945-
capi.SetUserData = XML_SetUserData;
1946-
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
1947-
capi.SetEncoding = XML_SetEncoding;
1948-
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
1937+
capi->size = sizeof(*capi);
1938+
capi->magic = PyExpat_CAPI_MAGIC;
1939+
capi->MAJOR_VERSION = XML_MAJOR_VERSION;
1940+
capi->MINOR_VERSION = XML_MINOR_VERSION;
1941+
capi->MICRO_VERSION = XML_MICRO_VERSION;
1942+
capi->ErrorString = XML_ErrorString;
1943+
capi->GetErrorCode = XML_GetErrorCode;
1944+
capi->GetErrorColumnNumber = XML_GetErrorColumnNumber;
1945+
capi->GetErrorLineNumber = XML_GetErrorLineNumber;
1946+
capi->Parse = XML_Parse;
1947+
capi->ParserCreate_MM = XML_ParserCreate_MM;
1948+
capi->ParserFree = XML_ParserFree;
1949+
capi->SetCharacterDataHandler = XML_SetCharacterDataHandler;
1950+
capi->SetCommentHandler = XML_SetCommentHandler;
1951+
capi->SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1952+
capi->SetElementHandler = XML_SetElementHandler;
1953+
capi->SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1954+
capi->SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1955+
capi->SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1956+
capi->SetUserData = XML_SetUserData;
1957+
capi->SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
1958+
capi->SetEncoding = XML_SetEncoding;
1959+
capi->DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
19491960
#if XML_COMBINED_VERSION >= 20100
1950-
capi.SetHashSalt = XML_SetHashSalt;
1961+
capi->SetHashSalt = XML_SetHashSalt;
19511962
#else
1952-
capi.SetHashSalt = NULL;
1963+
capi->SetHashSalt = NULL;
19531964
#endif
19541965

19551966
/* export using capsule */
1956-
PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
1967+
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
1968+
pyexpat_destructor);
19571969
if (capi_object == NULL) {
1970+
PyMem_Free(capi);
19581971
return -1;
19591972
}
19601973

0 commit comments

Comments
 (0)