From 8dc799aac7b3c5f7be639c0f15d3f294a7b194a7 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:11:34 +0200 Subject: [PATCH 1/5] Port XML_GetCurrentByteIndex to public APIs This is necessary to avoid a deprecation break in libxml2 2.14.x. --- ext/xml/compat.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ext/xml/compat.c b/ext/xml/compat.c index 7b463ebb511..bc46f001c05 100644 --- a/ext/xml/compat.c +++ b/ext/xml/compat.c @@ -705,8 +705,21 @@ XML_GetCurrentColumnNumber(XML_Parser parser) PHP_XML_API int XML_GetCurrentByteIndex(XML_Parser parser) { - return parser->parser->input->consumed + - (parser->parser->input->cur - parser->parser->input->base); + /* We have to temporarily disable the encoder to satisfy the note from the manual: + * "This function returns byte index according to UTF-8 encoded text disregarding if input is in another encoding." + * Although that should probably be corrected at one point? (TODO) */ + xmlCharEncodingHandlerPtr encoder = NULL; + xmlParserInputPtr input = parser->parser->input; + if (input->buf) { + encoder = input->buf->encoder; + input->buf->encoder = NULL; + } + long result = xmlByteConsumed(parser->parser); + if (encoder) { + input->buf->encoder = encoder; + } + /* TODO: at one point this should return long probably to make sure that files greater than 2 GiB are handled correctly. */ + return (int) result; } PHP_XML_API int From 6490a1827100c0b5da01bf9de915921ea586a281 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:32:09 +0200 Subject: [PATCH 2/5] Stop relying on the sax2 flag directly Setting this directly will become deprecated in libxml2 2.14. --- ext/xml/compat.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/xml/compat.c b/ext/xml/compat.c index bc46f001c05..910063f6def 100644 --- a/ext/xml/compat.c +++ b/ext/xml/compat.c @@ -476,8 +476,9 @@ XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *m parser->parser->replaceEntities = 1; parser->parser->wellFormed = 0; if (sep != NULL) { + /* Note: sax2 flag will be set due to the magic number in `initialized` in php_xml_compat_handlers */ + ZEND_ASSERT(parser->parser->sax->initialized == XML_SAX2_MAGIC); parser->use_namespace = 1; - parser->parser->sax2 = 1; parser->_ns_separator = xmlStrdup(sep); } else { /* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt From 823d0588f767b392ea74850c4586721b7079b588 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:38:07 +0200 Subject: [PATCH 3/5] Stop relying on lastError directly Reading this directly will become deprecated in libxml2 2.14. --- ext/xml/compat.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ext/xml/compat.c b/ext/xml/compat.c index 910063f6def..d8548bf4dc9 100644 --- a/ext/xml/compat.c +++ b/ext/xml/compat.c @@ -564,10 +564,14 @@ XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler en PHP_XML_API int XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final) { - int error; + int error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final); - error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final); - return !error && parser->parser->lastError.level <= XML_ERR_WARNING; + if (!error) { + const xmlError *error_data = xmlCtxtGetLastError(parser->parser); + return !error_data || error_data->level <= XML_ERR_WARNING; + } + + return 0; } PHP_XML_API int From e5e15fd2293e381212786d95790eecbd41630517 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Fri, 5 Jul 2024 20:52:56 +0200 Subject: [PATCH 4/5] Stop setting parse options directly Setting this directly will be deprecated in libxml2 2.14. --- ext/xml/compat.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/xml/compat.c b/ext/xml/compat.c index d8548bf4dc9..242cc4ba7c4 100644 --- a/ext/xml/compat.c +++ b/ext/xml/compat.c @@ -471,9 +471,8 @@ XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *m } php_libxml_sanitize_parse_ctxt_options(parser->parser); - xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX); + xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX | XML_PARSE_NOENT); - parser->parser->replaceEntities = 1; parser->parser->wellFormed = 0; if (sep != NULL) { /* Note: sax2 flag will be set due to the magic number in `initialized` in php_xml_compat_handlers */ From a66afbbe09e91fcbd7d65a9b6d03a0af19aeb778 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 6 Jul 2024 13:46:42 +0200 Subject: [PATCH 5/5] NEWS for compatibility in XML Closes GH-14836. --- NEWS | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS b/NEWS index 74c017b3fbf..883b3c162b1 100644 --- a/NEWS +++ b/NEWS @@ -46,6 +46,9 @@ PHP NEWS - Treewide: . Fix compatibility with libxml2 2.13.2. (nielsdos) +- XML: + . Move away from to-be-deprecated libxml fields. (nielsdos) + 04 Jul 2024, PHP 8.2.21 - Core: