Fix memory leak when initDocDict() fails

Related: RHEL-120801
This commit is contained in:
Charalampos Stratakis 2026-02-20 17:04:06 +01:00
parent c4a2a434f9
commit d502b1e94e
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,45 @@
From 495b40c534e63cc49d64e8369cb726b3ec88d7c3 Mon Sep 17 00:00:00 2001
From: Charalampos Stratakis <cstratak@redhat.com>
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(<unsigned char*>"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

View File

@ -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