From d502b1e94eb8aacaf8475d1869cad8a5f651bc2e Mon Sep 17 00:00:00 2001 From: Charalampos Stratakis Date: Fri, 20 Feb 2026 17:04:06 +0100 Subject: [PATCH] Fix memory leak when initDocDict() fails Related: RHEL-120801 --- fix-initDocDict-memory-leak.patch | 45 +++++++++++++++++++++++++++++++ python3.14-lxml.spec | 6 +++++ 2 files changed, 51 insertions(+) create mode 100644 fix-initDocDict-memory-leak.patch diff --git a/fix-initDocDict-memory-leak.patch b/fix-initDocDict-memory-leak.patch new file mode 100644 index 0000000..0e7d29d --- /dev/null +++ b/fix-initDocDict-memory-leak.patch @@ -0,0 +1,45 @@ +From 495b40c534e63cc49d64e8369cb726b3ec88d7c3 Mon Sep 17 00:00:00 2001 +From: Charalampos Stratakis +Date: Fri, 20 Feb 2026 16:58:28 +0100 +Subject: [PATCH] Free xmlDoc on initDocDict failure in _newXMLDoc/_newHTMLDoc + +If __GLOBAL_PARSER_CONTEXT.initDocDict() raises an exception after +xmlNewDoc()/htmlNewDoc() has already allocated the document, the +error path never frees it. +--- + src/lxml/parser.pxi | 12 ++++++++++-- + 1 file changed, 10 insertions(+), 2 deletions(-) + +diff --git a/src/lxml/parser.pxi b/src/lxml/parser.pxi +index 8d53277..43e6476 100644 +--- a/src/lxml/parser.pxi ++++ b/src/lxml/parser.pxi +@@ -1972,7 +1972,11 @@ cdef xmlDoc* _newXMLDoc() except NULL: + raise MemoryError() + if result.encoding is NULL: + result.encoding = tree.xmlStrdup("UTF-8") +- __GLOBAL_PARSER_CONTEXT.initDocDict(result) ++ try: ++ __GLOBAL_PARSER_CONTEXT.initDocDict(result) ++ except: ++ tree.xmlFreeDoc(result) ++ raise + return result + + cdef xmlDoc* _newHTMLDoc() except NULL: +@@ -1980,7 +1984,11 @@ cdef xmlDoc* _newHTMLDoc() except NULL: + result = tree.htmlNewDoc(NULL, NULL) + if result is NULL: + raise MemoryError() +- __GLOBAL_PARSER_CONTEXT.initDocDict(result) ++ try: ++ __GLOBAL_PARSER_CONTEXT.initDocDict(result) ++ except: ++ tree.xmlFreeDoc(result) ++ raise + return result + + cdef xmlDoc* _copyDoc(xmlDoc* c_doc, int recursive) except NULL: +-- +2.53.0 + diff --git a/python3.14-lxml.spec b/python3.14-lxml.spec index 9db61f4..a5b5d24 100644 --- a/python3.14-lxml.spec +++ b/python3.14-lxml.spec @@ -24,6 +24,12 @@ Source1: get-lxml-source.sh # https://github.com/lxml/lxml/commit/39cdf11f5d2cf6e4f810202b6e415afd350d3df2 Patch: fix-memory-leaks.patch +# Free xmlDoc when initDocDict() fails in _newXMLDoc/_newHTMLDoc. +# Downstream only +# Fixed upstream in 0b512418 by removing these initDocDict() calls entirely +# but this is backwards incompatible. +Patch: fix-initDocDict-memory-leak.patch + BuildRequires: gcc BuildRequires: libxml2-devel BuildRequires: libxslt-devel