Skip to content

Commit a1bcaf0

Browse files
committed
Merge branch 'PHP-8.2' into PHP-8.3
* PHP-8.2: NEWS for compatibility in XML Stop setting parse options directly Stop relying on lastError directly Stop relying on the sax2 flag directly Port XML_GetCurrentByteIndex to public APIs
2 parents 7c970f0 + a66afbb commit a1bcaf0

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ PHP NEWS
4444
- Treewide:
4545
. Fix compatibility with libxml2 2.13.2. (nielsdos)
4646

47+
- XML:
48+
. Move away from to-be-deprecated libxml fields. (nielsdos)
49+
4750
20 Jun 2024, PHP 8.3.9
4851

4952
- Core:

ext/xml/compat.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -471,13 +471,13 @@ XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *m
471471
}
472472

473473
php_libxml_sanitize_parse_ctxt_options(parser->parser);
474-
xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX);
474+
xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX | XML_PARSE_NOENT);
475475

476-
parser->parser->replaceEntities = 1;
477476
parser->parser->wellFormed = 0;
478477
if (sep != NULL) {
478+
/* Note: sax2 flag will be set due to the magic number in `initialized` in php_xml_compat_handlers */
479+
ZEND_ASSERT(parser->parser->sax->initialized == XML_SAX2_MAGIC);
479480
parser->use_namespace = 1;
480-
parser->parser->sax2 = 1;
481481
parser->_ns_separator = xmlStrdup(sep);
482482
} else {
483483
/* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt
@@ -563,10 +563,14 @@ XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler en
563563
PHP_XML_API int
564564
XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
565565
{
566-
int error;
566+
int error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);
567567

568-
error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);
569-
return !error && parser->parser->lastError.level <= XML_ERR_WARNING;
568+
if (!error) {
569+
const xmlError *error_data = xmlCtxtGetLastError(parser->parser);
570+
return !error_data || error_data->level <= XML_ERR_WARNING;
571+
}
572+
573+
return 0;
570574
}
571575

572576
PHP_XML_API int
@@ -705,8 +709,21 @@ XML_GetCurrentColumnNumber(XML_Parser parser)
705709
PHP_XML_API int
706710
XML_GetCurrentByteIndex(XML_Parser parser)
707711
{
708-
return parser->parser->input->consumed +
709-
(parser->parser->input->cur - parser->parser->input->base);
712+
/* We have to temporarily disable the encoder to satisfy the note from the manual:
713+
* "This function returns byte index according to UTF-8 encoded text disregarding if input is in another encoding."
714+
* Although that should probably be corrected at one point? (TODO) */
715+
xmlCharEncodingHandlerPtr encoder = NULL;
716+
xmlParserInputPtr input = parser->parser->input;
717+
if (input->buf) {
718+
encoder = input->buf->encoder;
719+
input->buf->encoder = NULL;
720+
}
721+
long result = xmlByteConsumed(parser->parser);
722+
if (encoder) {
723+
input->buf->encoder = encoder;
724+
}
725+
/* TODO: at one point this should return long probably to make sure that files greater than 2 GiB are handled correctly. */
726+
return (int) result;
710727
}
711728

712729
PHP_XML_API int

0 commit comments

Comments
 (0)