bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24… · python/cpython@7c83eaa (original) (raw)
`@@ -1836,6 +1836,13 @@ add_features(PyObject *mod)
`
1836
1836
`}
`
1837
1837
`#endif
`
1838
1838
``
``
1839
`+
static void
`
``
1840
`+
pyexpat_destructor(PyObject *op)
`
``
1841
`+
{
`
``
1842
`+
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
`
``
1843
`+
PyMem_Free(p);
`
``
1844
`+
}
`
``
1845
+
1839
1846
`static int
`
1840
1847
`pyexpat_exec(PyObject *mod)
`
1841
1848
`{
`
`@@ -1921,40 +1928,46 @@ pyexpat_exec(PyObject *mod)
`
1921
1928
`MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
`
1922
1929
`#undef MYCONST
`
1923
1930
``
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
`+
}
`
1925
1936
`/* 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;
`
1949
1960
`#if XML_COMBINED_VERSION >= 20100
`
1950
``
`-
capi.SetHashSalt = XML_SetHashSalt;
`
``
1961
`+
capi->SetHashSalt = XML_SetHashSalt;
`
1951
1962
`#else
`
1952
``
`-
capi.SetHashSalt = NULL;
`
``
1963
`+
capi->SetHashSalt = NULL;
`
1953
1964
`#endif
`
1954
1965
``
1955
1966
`/* 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);
`
1957
1969
`if (capi_object == NULL) {
`
``
1970
`+
PyMem_Free(capi);
`
1958
1971
`return -1;
`
1959
1972
` }
`
1960
1973
``