Skip to content

Commit b034fd3

Browse files
authored
gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)" (#100745)
* gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)" This reverts commit 7c83eaa.
1 parent 53455a3 commit b034fd3

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
@@ -1878,13 +1878,6 @@ add_features(PyObject *mod)
18781878
}
18791879
#endif
18801880

1881-
static void
1882-
pyexpat_destructor(PyObject *op)
1883-
{
1884-
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
1885-
PyMem_Free(p);
1886-
}
1887-
18881881
static int
18891882
pyexpat_exec(PyObject *mod)
18901883
{
@@ -1972,46 +1965,40 @@ pyexpat_exec(PyObject *mod)
19721965
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
19731966
#undef MYCONST
19741967

1975-
struct PyExpat_CAPI *capi = PyMem_Calloc(1, sizeof(struct PyExpat_CAPI));
1976-
if (capi == NULL) {
1977-
PyErr_NoMemory();
1978-
return -1;
1979-
}
1968+
static struct PyExpat_CAPI capi;
19801969
/* initialize pyexpat dispatch table */
1981-
capi->size = sizeof(*capi);
1982-
capi->magic = PyExpat_CAPI_MAGIC;
1983-
capi->MAJOR_VERSION = XML_MAJOR_VERSION;
1984-
capi->MINOR_VERSION = XML_MINOR_VERSION;
1985-
capi->MICRO_VERSION = XML_MICRO_VERSION;
1986-
capi->ErrorString = XML_ErrorString;
1987-
capi->GetErrorCode = XML_GetErrorCode;
1988-
capi->GetErrorColumnNumber = XML_GetErrorColumnNumber;
1989-
capi->GetErrorLineNumber = XML_GetErrorLineNumber;
1990-
capi->Parse = XML_Parse;
1991-
capi->ParserCreate_MM = XML_ParserCreate_MM;
1992-
capi->ParserFree = XML_ParserFree;
1993-
capi->SetCharacterDataHandler = XML_SetCharacterDataHandler;
1994-
capi->SetCommentHandler = XML_SetCommentHandler;
1995-
capi->SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1996-
capi->SetElementHandler = XML_SetElementHandler;
1997-
capi->SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1998-
capi->SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1999-
capi->SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
2000-
capi->SetUserData = XML_SetUserData;
2001-
capi->SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
2002-
capi->SetEncoding = XML_SetEncoding;
2003-
capi->DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
1970+
capi.size = sizeof(capi);
1971+
capi.magic = PyExpat_CAPI_MAGIC;
1972+
capi.MAJOR_VERSION = XML_MAJOR_VERSION;
1973+
capi.MINOR_VERSION = XML_MINOR_VERSION;
1974+
capi.MICRO_VERSION = XML_MICRO_VERSION;
1975+
capi.ErrorString = XML_ErrorString;
1976+
capi.GetErrorCode = XML_GetErrorCode;
1977+
capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
1978+
capi.GetErrorLineNumber = XML_GetErrorLineNumber;
1979+
capi.Parse = XML_Parse;
1980+
capi.ParserCreate_MM = XML_ParserCreate_MM;
1981+
capi.ParserFree = XML_ParserFree;
1982+
capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
1983+
capi.SetCommentHandler = XML_SetCommentHandler;
1984+
capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
1985+
capi.SetElementHandler = XML_SetElementHandler;
1986+
capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
1987+
capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
1988+
capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
1989+
capi.SetUserData = XML_SetUserData;
1990+
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
1991+
capi.SetEncoding = XML_SetEncoding;
1992+
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
20041993
#if XML_COMBINED_VERSION >= 20100
2005-
capi->SetHashSalt = XML_SetHashSalt;
1994+
capi.SetHashSalt = XML_SetHashSalt;
20061995
#else
2007-
capi->SetHashSalt = NULL;
1996+
capi.SetHashSalt = NULL;
20081997
#endif
20091998

20101999
/* export using capsule */
2011-
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
2012-
pyexpat_destructor);
2000+
PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
20132001
if (capi_object == NULL) {
2014-
PyMem_Free(capi);
20152002
return -1;
20162003
}
20172004

0 commit comments

Comments
 (0)