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 7c83eaa536.
(cherry picked from commit b034fd3e59)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
Miss Islington (bot) 2023-01-08 05:19:37 -08:00 committed by GitHub
parent be7c19723f
commit 6c7e32f6a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 40 deletions

View file

@ -0,0 +1 @@
Fix crash in :mod:`pyexpat` by statically allocating ``PyExpat_CAPI`` capsule.

View file

@ -1886,13 +1886,6 @@ error:
} }
#endif #endif
static void
pyexpat_destructor(PyObject *op)
{
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
PyMem_Free(p);
}
static int static int
pyexpat_exec(PyObject *mod) pyexpat_exec(PyObject *mod)
{ {
@ -1980,46 +1973,40 @@ pyexpat_exec(PyObject *mod)
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS); MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
#undef MYCONST #undef MYCONST
struct PyExpat_CAPI *capi = PyMem_Calloc(1, sizeof(struct PyExpat_CAPI)); static struct PyExpat_CAPI capi;
if (capi == NULL) {
PyErr_NoMemory();
return -1;
}
/* initialize pyexpat dispatch table */ /* initialize pyexpat dispatch table */
capi->size = sizeof(*capi); capi.size = sizeof(capi);
capi->magic = PyExpat_CAPI_MAGIC; capi.magic = PyExpat_CAPI_MAGIC;
capi->MAJOR_VERSION = XML_MAJOR_VERSION; capi.MAJOR_VERSION = XML_MAJOR_VERSION;
capi->MINOR_VERSION = XML_MINOR_VERSION; capi.MINOR_VERSION = XML_MINOR_VERSION;
capi->MICRO_VERSION = XML_MICRO_VERSION; capi.MICRO_VERSION = XML_MICRO_VERSION;
capi->ErrorString = XML_ErrorString; capi.ErrorString = XML_ErrorString;
capi->GetErrorCode = XML_GetErrorCode; capi.GetErrorCode = XML_GetErrorCode;
capi->GetErrorColumnNumber = XML_GetErrorColumnNumber; capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
capi->GetErrorLineNumber = XML_GetErrorLineNumber; capi.GetErrorLineNumber = XML_GetErrorLineNumber;
capi->Parse = XML_Parse; capi.Parse = XML_Parse;
capi->ParserCreate_MM = XML_ParserCreate_MM; capi.ParserCreate_MM = XML_ParserCreate_MM;
capi->ParserFree = XML_ParserFree; capi.ParserFree = XML_ParserFree;
capi->SetCharacterDataHandler = XML_SetCharacterDataHandler; capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
capi->SetCommentHandler = XML_SetCommentHandler; capi.SetCommentHandler = XML_SetCommentHandler;
capi->SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand; capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
capi->SetElementHandler = XML_SetElementHandler; capi.SetElementHandler = XML_SetElementHandler;
capi->SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler; capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
capi->SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler; capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
capi->SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler; capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
capi->SetUserData = XML_SetUserData; capi.SetUserData = XML_SetUserData;
capi->SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler; capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
capi->SetEncoding = XML_SetEncoding; capi.SetEncoding = XML_SetEncoding;
capi->DefaultUnknownEncodingHandler = PyUnknownEncodingHandler; capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
#if XML_COMBINED_VERSION >= 20100 #if XML_COMBINED_VERSION >= 20100
capi->SetHashSalt = XML_SetHashSalt; capi.SetHashSalt = XML_SetHashSalt;
#else #else
capi->SetHashSalt = NULL; capi.SetHashSalt = NULL;
#endif #endif
/* export using capsule */ /* export using capsule */
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME, PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
pyexpat_destructor);
if (capi_object == NULL) { if (capi_object == NULL) {
PyMem_Free(capi);
return -1; return -1;
} }