Skip to content

Commit b438448

Browse files
committed
Merge branch 'PHP-8.3'
* PHP-8.3: 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 8c8322f + a1bcaf0 commit b438448

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

ext/xml/compat.c

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

453453
php_libxml_sanitize_parse_ctxt_options(parser->parser);
454-
xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX);
454+
xmlCtxtUseOptions(parser->parser, XML_PARSE_OLDSAX | XML_PARSE_NOENT);
455455

456-
parser->parser->replaceEntities = 1;
457456
parser->parser->wellFormed = 0;
458457
if (sep != NULL) {
458+
/* Note: sax2 flag will be set due to the magic number in `initialized` in php_xml_compat_handlers */
459+
ZEND_ASSERT(parser->parser->sax->initialized == XML_SAX2_MAGIC);
459460
parser->use_namespace = 1;
460-
parser->parser->sax2 = 1;
461461
parser->_ns_separator = xmlStrdup(sep);
462462
} else {
463463
/* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt
@@ -543,10 +543,14 @@ XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler en
543543
PHP_XML_API int
544544
XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
545545
{
546-
int error;
546+
int error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);
547547

548-
error = xmlParseChunk(parser->parser, (char *) data, data_len, is_final);
549-
return !error && parser->parser->lastError.level <= XML_ERR_WARNING;
548+
if (!error) {
549+
const xmlError *error_data = xmlCtxtGetLastError(parser->parser);
550+
return !error_data || error_data->level <= XML_ERR_WARNING;
551+
}
552+
553+
return 0;
550554
}
551555

552556
PHP_XML_API int
@@ -685,8 +689,21 @@ XML_GetCurrentColumnNumber(XML_Parser parser)
685689
PHP_XML_API int
686690
XML_GetCurrentByteIndex(XML_Parser parser)
687691
{
688-
return parser->parser->input->consumed +
689-
(parser->parser->input->cur - parser->parser->input->base);
692+
/* We have to temporarily disable the encoder to satisfy the note from the manual:
693+
* "This function returns byte index according to UTF-8 encoded text disregarding if input is in another encoding."
694+
* Although that should probably be corrected at one point? (TODO) */
695+
xmlCharEncodingHandlerPtr encoder = NULL;
696+
xmlParserInputPtr input = parser->parser->input;
697+
if (input->buf) {
698+
encoder = input->buf->encoder;
699+
input->buf->encoder = NULL;
700+
}
701+
long result = xmlByteConsumed(parser->parser);
702+
if (encoder) {
703+
input->buf->encoder = encoder;
704+
}
705+
/* TODO: at one point this should return long probably to make sure that files greater than 2 GiB are handled correctly. */
706+
return (int) result;
690707
}
691708

692709
PHP_XML_API int

0 commit comments

Comments
 (0)