Skip to content

Commit 0e00bce

Browse files
[3.10] gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)" (GH-100745) (#100847)
gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)" (GH-100745) * gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)" This reverts commit 7c83eaa. (cherry picked from commit b034fd3) Co-authored-by: Nikita Sobolev <mail@sobolevn.me> Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent a8702bb commit 0e00bce

File tree

2 files changed

+28
-40
lines changed

2 files changed

+28
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash in :mod:`pyexpat` by statically allocating ``PyExpat_CAPI`` capsule.

Modules/pyexpat.c

+27-40
Original file line numberDiff line numberDiff line change
@@ -1843,13 +1843,6 @@ add_features(PyObject *mod)
18431843
}
18441844
#endif
18451845

1846-
static void
1847-
pyexpat_destructor(PyObject *op)
1848-
{
1849-
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
1850-
PyMem_Free(p);
1851-
}
1852-
18531846
static int
18541847
pyexpat_exec(PyObject *mod)
18551848
{
@@ -1933,46 +1926,40 @@ pyexpat_exec(PyObject *mod)
19331926
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
19341927
#undef MYCONST
19351928

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

19711960
/* export using capsule */
1972-
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
1973-
pyexpat_destructor);
1961+
PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
19741962
if (capi_object == NULL) {
1975-
PyMem_Free(capi);
19761963
return -1;
19771964
}
19781965

0 commit comments

Comments
 (0)