Skip to content

Commit 6c7e32f

Browse files
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>
1 parent be7c197 commit 6c7e32f

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
@@ -1886,13 +1886,6 @@ add_features(PyObject *mod)
18861886
}
18871887
#endif
18881888

1889-
static void
1890-
pyexpat_destructor(PyObject *op)
1891-
{
1892-
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
1893-
PyMem_Free(p);
1894-
}
1895-
18961889
static int
18971890
pyexpat_exec(PyObject *mod)
18981891
{
@@ -1980,46 +1973,40 @@ pyexpat_exec(PyObject *mod)
19801973
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
19811974
#undef MYCONST
19821975

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

20182007
/* export using capsule */
2019-
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
2020-
pyexpat_destructor);
2008+
PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
20212009
if (capi_object == NULL) {
2022-
PyMem_Free(capi);
20232010
return -1;
20242011
}
20252012

0 commit comments

Comments
 (0)