From 92f4705c6707be01bc3b0b8460582b1fc0890f31 Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Sun, 13 Sep 2026 22:36:30 +0530 Subject: [PATCH] Avoid closing libxml output encoder twice --- ...t_saveHTMLFile_failed_output_encoding.phpt | 31 +++++++++++++++++++ ext/libxml/libxml.c | 4 ++- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 ext/dom/tests/DOMDocument_saveHTMLFile_failed_output_encoding.phpt diff --git a/ext/dom/tests/DOMDocument_saveHTMLFile_failed_output_encoding.phpt b/ext/dom/tests/DOMDocument_saveHTMLFile_failed_output_encoding.phpt new file mode 100644 index 000000000000..05adefb973aa --- /dev/null +++ b/ext/dom/tests/DOMDocument_saveHTMLFile_failed_output_encoding.phpt @@ -0,0 +1,31 @@ +--TEST-- +DOMDocument::saveHTMLFile() does not close the encoder twice when opening the output fails +--EXTENSIONS-- +dom +--FILE-- +loadHTML('value'); + $doc->getElementsByTagName('meta')->item(0)->setAttribute('content', 'text/html; charset=' . $encoding); + for ($i = 0; $i < 3; $i++) { + $result = @$doc->saveHTMLFile($filename); + var_dump($result === 0 || $result === false); + } + var_dump($doc->getElementsByTagName('body')->item(0)->textContent === 'value'); +} +?> +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index fa9432444366..c73bcf930cfd 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -618,8 +618,10 @@ php_libxml_output_buffer_create_filename(const char *URI, return(ret); err: - /* Similarly to __xmlOutputBufferCreateFilename we should also close the encoder on failure. */ +#if LIBXML_VERSION < 21404 + /* As of libxml 2.14.4, libxml closes the encoder after this callback fails. */ xmlCharEncCloseFunc(encoder); +#endif return NULL; }