From dae8984a01e03bf4828cfd5d69fe611aba447746 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 4 Jan 2023 11:51:37 +0300 Subject: [PATCH 1/7] gh-100689: Revert "bpo-41798: pyexpat: Allocate the expat_CAPI on the heap memory (GH-24061)" This reverts commit 7c83eaa536d2f436ae46211ca48692f576c732f0. --- Modules/pyexpat.c | 67 +++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 40 deletions(-) diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 2440798bff7e66..63a3392d5efe7d 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -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) { @@ -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; } From 405bdf52f0f4398a67c93ab7c02677ca6de29169 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 4 Jan 2023 12:59:11 +0300 Subject: [PATCH 2/7] Add news entry --- .../next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst diff --git a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst new file mode 100644 index 00000000000000..b9a7202fb891a2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst @@ -0,0 +1 @@ +Fix crash in ``_elementtree.c`` by reverting :gh:`24061`. From aae8493c01f169943c11b766a51645419735f78a Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Wed, 4 Jan 2023 13:11:33 +0300 Subject: [PATCH 3/7] Update 2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst --- .../next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst index b9a7202fb891a2..1fae96c9436e78 100644 --- a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst +++ b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst @@ -1 +1 @@ -Fix crash in ``_elementtree.c`` by reverting :gh:`24061`. +Fix crash in ``_elementtree.c`` by reverting ``GH-24061``. From ad868392ba3c867e4f43c172c0c6cd4c28475997 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 4 Jan 2023 23:11:33 +0530 Subject: [PATCH 4/7] Update Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst --- .../next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst index 1fae96c9436e78..d8c671ea7ff2f4 100644 --- a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst +++ b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst @@ -1 +1 @@ -Fix crash in ``_elementtree.c`` by reverting ``GH-24061``. +Fix crash in ``_elementtree.c`` by statically allocating ``PyExpat_CAPI`` capsule. From 6ff2837c368a1300bc392cb1867b831311905093 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Wed, 4 Jan 2023 23:14:00 +0530 Subject: [PATCH 5/7] Update Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst --- .../next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst index d8c671ea7ff2f4..5db8385e1968da 100644 --- a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst +++ b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst @@ -1 +1 @@ -Fix crash in ``_elementtree.c`` by statically allocating ``PyExpat_CAPI`` capsule. +Fix crash in ``_elementtree.c`` by statically allocating ``PyExpat_CAPI`` capsule. From f7c0bf36a1dd1f73c2ed2b56ff5b837d317f0c9a Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 5 Jan 2023 11:36:44 +0300 Subject: [PATCH 6/7] Update 2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst --- .../next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst index 5db8385e1968da..4734b817fa32e3 100644 --- a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst +++ b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst @@ -1 +1 @@ -Fix crash in ``_elementtree.c`` by statically allocating ``PyExpat_CAPI`` capsule. +Fix crash in ``pyexpat`` by statically allocating ``PyExpat_CAPI`` capsule. From 92a4746ce4d32d9fd095ed8b0da96bfe5599fef7 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Sun, 8 Jan 2023 13:28:48 +0530 Subject: [PATCH 7/7] Update Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst --- .../next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst index 4734b817fa32e3..09aeab7bceaa46 100644 --- a/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst +++ b/Misc/NEWS.d/next/Library/2023-01-04-12-58-59.gh-issue-100689.Ce0ITG.rst @@ -1 +1 @@ -Fix crash in ``pyexpat`` by statically allocating ``PyExpat_CAPI`` capsule. +Fix crash in :mod:`pyexpat` by statically allocating ``PyExpat_CAPI`` capsule.