Skip to content

Commit

Permalink
GH-101797: allocate PyExpat_CAPI capsule on heap (#101798)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaraditya303 committed Feb 11, 2023
1 parent 17143e2 commit b652d40
Showing 1 changed file with 45 additions and 27 deletions.
72 changes: 45 additions & 27 deletions Modules/pyexpat.c
Expand Up @@ -1878,6 +1878,18 @@ add_features(PyObject *mod)
}
#endif

static void
pyexpat_capsule_destructor(PyObject *capsule)
{
void *p = PyCapsule_GetPointer(capsule, PyExpat_CAPSULE_NAME);
if (p == NULL) {
PyErr_WriteUnraisable(capsule);
return;
}
PyMem_Free(p);
}


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

static struct PyExpat_CAPI capi;
struct PyExpat_CAPI *capi = PyMem_Malloc(sizeof(*capi));
if (capi == NULL) {
PyErr_NoMemory();
return -1;
}
/* 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, NULL);
PyObject *capi_object = PyCapsule_New(capi, PyExpat_CAPSULE_NAME,
pyexpat_capsule_destructor);
if (capi_object == NULL) {
PyMem_Free(capi);
return -1;
}

Expand Down

0 comments on commit b652d40

Please sign in to comment.