Skip to content

Commit 4bee574

Browse files
committed
Fix GH-11792: LIBXML_NOXMLDECL is not implemented or broken
Fixes GH-11792. Closes GH-11794.
1 parent 8ac0dcc commit 4bee574

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ PHP NEWS
88
- Core:
99
. Fixed oss-fuzz #60741 (Leak in open_basedir). (ilutov)
1010

11+
- DOM:
12+
. Fixed bug GH-11792 (LIBXML_NOXMLDECL is not implemented or broken).
13+
(nielsdos)
14+
1115
- FFI:
1216
. Fix leaking definitions when using FFI::cdef()->new(...). (ilutov)
1317

ext/dom/document.c

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#if defined(HAVE_LIBXML) && defined(HAVE_DOM)
2424
#include "php_dom.h"
2525
#include <libxml/SAX.h>
26+
#include <libxml/xmlsave.h>
2627
#ifdef LIBXML_SCHEMAS_ENABLED
2728
#include <libxml/relaxng.h>
2829
#include <libxml/xmlschemas.h>
@@ -1462,9 +1463,9 @@ PHP_METHOD(DOMDocument, saveXML)
14621463
xmlDoc *docp;
14631464
xmlNode *node;
14641465
xmlBufferPtr buf;
1465-
xmlChar *mem;
1466+
const xmlChar *mem;
14661467
dom_object *intern, *nodeobj;
1467-
int size, format, saveempty = 0;
1468+
int size, format, old_xml_save_no_empty_tags;
14681469
zend_long options = 0;
14691470

14701471
id = ZEND_THIS;
@@ -1484,42 +1485,59 @@ PHP_METHOD(DOMDocument, saveXML)
14841485
php_dom_throw_error(WRONG_DOCUMENT_ERR, dom_get_strict_error(intern->document));
14851486
RETURN_FALSE;
14861487
}
1488+
14871489
buf = xmlBufferCreate();
14881490
if (!buf) {
14891491
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
14901492
RETURN_FALSE;
14911493
}
1492-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1493-
saveempty = xmlSaveNoEmptyTags;
1494-
xmlSaveNoEmptyTags = 1;
1495-
}
1494+
/* Save libxml2 global, override its vaule, and restore after saving. */
1495+
old_xml_save_no_empty_tags = xmlSaveNoEmptyTags;
1496+
xmlSaveNoEmptyTags = (options & LIBXML_SAVE_NOEMPTYTAG) ? 1 : 0;
14961497
xmlNodeDump(buf, docp, node, 0, format);
1497-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1498-
xmlSaveNoEmptyTags = saveempty;
1499-
}
1500-
mem = (xmlChar*) xmlBufferContent(buf);
1501-
if (!mem) {
1502-
xmlBufferFree(buf);
1498+
xmlSaveNoEmptyTags = old_xml_save_no_empty_tags;
1499+
} else {
1500+
buf = xmlBufferCreate();
1501+
if (!buf) {
1502+
php_error_docref(NULL, E_WARNING, "Could not fetch buffer");
15031503
RETURN_FALSE;
15041504
}
1505-
RETVAL_STRING((char *) mem);
1506-
xmlBufferFree(buf);
1507-
} else {
1508-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1509-
saveempty = xmlSaveNoEmptyTags;
1510-
xmlSaveNoEmptyTags = 1;
1505+
1506+
int converted_options = XML_SAVE_AS_XML;
1507+
if (options & XML_SAVE_NO_DECL) {
1508+
converted_options |= XML_SAVE_NO_DECL;
1509+
}
1510+
if (format) {
1511+
converted_options |= XML_SAVE_FORMAT;
15111512
}
1513+
/* Save libxml2 global, override its vaule, and restore after saving. */
1514+
old_xml_save_no_empty_tags = xmlSaveNoEmptyTags;
1515+
xmlSaveNoEmptyTags = (options & LIBXML_SAVE_NOEMPTYTAG) ? 1 : 0;
15121516
/* Encoding is handled from the encoding property set on the document */
1513-
xmlDocDumpFormatMemory(docp, &mem, &size, format);
1514-
if (options & LIBXML_SAVE_NOEMPTYTAG) {
1515-
xmlSaveNoEmptyTags = saveempty;
1517+
xmlSaveCtxtPtr ctxt = xmlSaveToBuffer(buf, (const char *) docp->encoding, converted_options);
1518+
xmlSaveNoEmptyTags = old_xml_save_no_empty_tags;
1519+
if (UNEXPECTED(!ctxt)) {
1520+
xmlBufferFree(buf);
1521+
php_error_docref(NULL, E_WARNING, "Could not create save context");
1522+
RETURN_FALSE;
15161523
}
1517-
if (!size || !mem) {
1524+
if (UNEXPECTED(xmlSaveDoc(ctxt, docp) < 0)) {
1525+
(void) xmlSaveClose(ctxt);
1526+
xmlBufferFree(buf);
1527+
php_error_docref(NULL, E_WARNING, "Could not save document");
15181528
RETURN_FALSE;
15191529
}
1520-
RETVAL_STRINGL((char *) mem, size);
1521-
xmlFree(mem);
1530+
(void) xmlSaveFlush(ctxt);
1531+
(void) xmlSaveClose(ctxt);
1532+
}
1533+
mem = xmlBufferContent(buf);
1534+
if (!mem) {
1535+
xmlBufferFree(buf);
1536+
RETURN_FALSE;
15221537
}
1538+
size = xmlBufferLength(buf);
1539+
RETVAL_STRINGL((const char *) mem, size);
1540+
xmlBufferFree(buf);
15231541
}
15241542
/* }}} end dom_document_savexml */
15251543

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
DOMDocument::saveXML(): XML_SAVE_NO_DECL
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$doc = new DOMDocument();
8+
$doc->loadXML('<root>é</root>');
9+
10+
echo $doc->saveXML(options: 0);
11+
echo $doc->saveXML(options: LIBXML_NOXMLDECL);
12+
$doc->encoding = "BIG5";
13+
echo $doc->saveXML(options: LIBXML_NOXMLDECL);
14+
15+
// Edge case
16+
$doc = new DOMDocument();
17+
var_dump($doc->saveXML(options: LIBXML_NOXMLDECL));
18+
?>
19+
--EXPECT--
20+
<?xml version="1.0"?>
21+
<root>&#xE9;</root>
22+
<root>&#xE9;</root>
23+
<root>&#233;</root>
24+
string(0) ""

0 commit comments

Comments
 (0)