Skip to content

gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the… #100745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 8, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash in :mod:`pyexpat` by statically allocating ``PyExpat_CAPI`` capsule.
67 changes: 27 additions & 40 deletions Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1878,13 +1878,6 @@ add_features(PyObject *mod)
}
#endif

static void
pyexpat_destructor(PyObject *op)
{
void *p = PyCapsule_GetPointer(op, PyExpat_CAPSULE_NAME);
PyMem_Free(p);
}

static int
pyexpat_exec(PyObject *mod)
{
Expand Down Expand Up @@ -1972,46 +1965,40 @@ pyexpat_exec(PyObject *mod)
MYCONST(XML_PARAM_ENTITY_PARSING_ALWAYS);
#undef MYCONST

struct PyExpat_CAPI *capi = PyMem_Calloc(1, sizeof(struct PyExpat_CAPI));
if (capi == NULL) {
PyErr_NoMemory();
return -1;
}
static struct PyExpat_CAPI capi;
/* initialize pyexpat dispatch table */
capi->size = sizeof(*capi);
capi->magic = PyExpat_CAPI_MAGIC;
capi->MAJOR_VERSION = XML_MAJOR_VERSION;
capi->MINOR_VERSION = XML_MINOR_VERSION;
capi->MICRO_VERSION = XML_MICRO_VERSION;
capi->ErrorString = XML_ErrorString;
capi->GetErrorCode = XML_GetErrorCode;
capi->GetErrorColumnNumber = XML_GetErrorColumnNumber;
capi->GetErrorLineNumber = XML_GetErrorLineNumber;
capi->Parse = XML_Parse;
capi->ParserCreate_MM = XML_ParserCreate_MM;
capi->ParserFree = XML_ParserFree;
capi->SetCharacterDataHandler = XML_SetCharacterDataHandler;
capi->SetCommentHandler = XML_SetCommentHandler;
capi->SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
capi->SetElementHandler = XML_SetElementHandler;
capi->SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
capi->SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
capi->SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
capi->SetUserData = XML_SetUserData;
capi->SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
capi->SetEncoding = XML_SetEncoding;
capi->DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
capi.size = sizeof(capi);
capi.magic = PyExpat_CAPI_MAGIC;
capi.MAJOR_VERSION = XML_MAJOR_VERSION;
capi.MINOR_VERSION = XML_MINOR_VERSION;
capi.MICRO_VERSION = XML_MICRO_VERSION;
capi.ErrorString = XML_ErrorString;
capi.GetErrorCode = XML_GetErrorCode;
capi.GetErrorColumnNumber = XML_GetErrorColumnNumber;
capi.GetErrorLineNumber = XML_GetErrorLineNumber;
capi.Parse = XML_Parse;
capi.ParserCreate_MM = XML_ParserCreate_MM;
capi.ParserFree = XML_ParserFree;
capi.SetCharacterDataHandler = XML_SetCharacterDataHandler;
capi.SetCommentHandler = XML_SetCommentHandler;
capi.SetDefaultHandlerExpand = XML_SetDefaultHandlerExpand;
capi.SetElementHandler = XML_SetElementHandler;
capi.SetNamespaceDeclHandler = XML_SetNamespaceDeclHandler;
capi.SetProcessingInstructionHandler = XML_SetProcessingInstructionHandler;
capi.SetUnknownEncodingHandler = XML_SetUnknownEncodingHandler;
capi.SetUserData = XML_SetUserData;
capi.SetStartDoctypeDeclHandler = XML_SetStartDoctypeDeclHandler;
capi.SetEncoding = XML_SetEncoding;
capi.DefaultUnknownEncodingHandler = PyUnknownEncodingHandler;
#if XML_COMBINED_VERSION >= 20100
capi->SetHashSalt = XML_SetHashSalt;
capi.SetHashSalt = XML_SetHashSalt;
#else
capi->SetHashSalt = NULL;
capi.SetHashSalt = NULL;
#endif

/* export using capsule */
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
pyexpat_destructor);
PyObject *capi_object = PyCapsule_New(&capi, PyExpat_CAPSULE_NAME, NULL);
if (capi_object == NULL) {
PyMem_Free(capi);
return -1;
}

Expand Down