Skip to content

Commit b955973

Browse files
committed
Only register error handling when observable
Closes GH-13702.
1 parent 9fd74cf commit b955973

File tree

5 files changed

+40
-10
lines changed

5 files changed

+40
-10
lines changed

UPGRADING.INTERNALS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ PHP 8.4 INTERNALS UPGRADE NOTES
185185
- Removed the "properties" HashTable field from php_libxml_node_object.
186186
- Added a way to attached private data to a php_libxml_ref_obj.
187187
- Added a way to fix a class type onto php_libxml_ref_obj.
188+
- Added php_libxml_uses_internal_errors().
188189

189190
e. ext/date
190191
- Added the php_format_date_ex() API to format instances of php_date_obj.

ext/dom/html_document.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,16 @@ PHP_METHOD(DOM_HTMLDocument, createEmpty)
765765
RETURN_THROWS();
766766
}
767767

768+
/* Only bother to register error handling when the error reports can become observable. */
769+
static bool dom_should_register_error_handlers(zend_long options)
770+
{
771+
if (options & XML_PARSE_NOERROR) {
772+
return false;
773+
}
774+
775+
return php_libxml_uses_internal_errors() || ((EG(error_reporting) | EG(user_error_handler_error_reporting)) & E_WARNING);
776+
}
777+
768778
PHP_METHOD(DOM_HTMLDocument, createFromString)
769779
{
770780
const char *source, *override_encoding = NULL;
@@ -793,7 +803,7 @@ PHP_METHOD(DOM_HTMLDocument, createFromString)
793803
dom_reset_line_column_cache(&application_data.cache_tokenizer);
794804
lexbor_libxml2_bridge_parse_context ctx;
795805
lexbor_libxml2_bridge_parse_context_init(&ctx);
796-
if (!(options & XML_PARSE_NOERROR)) {
806+
if (dom_should_register_error_handlers(options)) {
797807
lexbor_libxml2_bridge_parse_set_error_callbacks(
798808
&ctx,
799809
dom_lexbor_libxml2_bridge_tokenizer_error_reporter,
@@ -952,7 +962,7 @@ PHP_METHOD(DOM_HTMLDocument, createFromFile)
952962
dom_reset_line_column_cache(&application_data.cache_tokenizer);
953963
lexbor_libxml2_bridge_parse_context ctx;
954964
lexbor_libxml2_bridge_parse_context_init(&ctx);
955-
if (!(options & XML_PARSE_NOERROR)) {
965+
if (dom_should_register_error_handlers(options)) {
956966
lexbor_libxml2_bridge_parse_set_error_callbacks(
957967
&ctx,
958968
dom_lexbor_libxml2_bridge_tokenizer_error_reporter,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Parse HTML document with user error handler and error_reporting(0)
3+
--EXTENSIONS--
4+
dom
5+
--INI--
6+
error_reporting=0
7+
--FILE--
8+
<?php
9+
10+
set_error_handler(function ($errno, $errstr, $errfile, $errline) {
11+
var_dump($errno, $errstr);
12+
}, E_WARNING);
13+
14+
DOM\HTMLDocument::createFromString('<html></html>');
15+
16+
?>
17+
--EXPECT--
18+
int(2)
19+
string(113) "DOM\HTMLDocument::createFromString(): tree error unexpected-token-in-initial-mode in Entity, line: 1, column: 2-5"

ext/libxml/libxml.c

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,23 +1055,22 @@ PHP_FUNCTION(libxml_set_streams_context)
10551055
}
10561056
/* }}} */
10571057

1058+
PHP_LIBXML_API bool php_libxml_uses_internal_errors(void)
1059+
{
1060+
return xmlStructuredError == php_libxml_structured_error_handler;
1061+
}
1062+
10581063
/* {{{ Disable libxml errors and allow user to fetch error information as needed */
10591064
PHP_FUNCTION(libxml_use_internal_errors)
10601065
{
1061-
xmlStructuredErrorFunc current_handler;
1062-
bool use_errors, use_errors_is_null = 1, retval;
1066+
bool use_errors, use_errors_is_null = true;
10631067

10641068
ZEND_PARSE_PARAMETERS_START(0, 1)
10651069
Z_PARAM_OPTIONAL
10661070
Z_PARAM_BOOL_OR_NULL(use_errors, use_errors_is_null)
10671071
ZEND_PARSE_PARAMETERS_END();
10681072

1069-
current_handler = xmlStructuredError;
1070-
if (current_handler && current_handler == php_libxml_structured_error_handler) {
1071-
retval = 1;
1072-
} else {
1073-
retval = 0;
1074-
}
1073+
bool retval = php_libxml_uses_internal_errors();
10751074

10761075
if (use_errors_is_null) {
10771076
RETURN_BOOL(retval);

ext/libxml/php_libxml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ PHP_LIBXML_API void php_libxml_issue_error(int level, const char *msg);
163163
PHP_LIBXML_API bool php_libxml_disable_entity_loader(bool disable);
164164
PHP_LIBXML_API void php_libxml_set_old_ns(xmlDocPtr doc, xmlNsPtr ns);
165165
PHP_LIBXML_API php_stream_context *php_libxml_get_stream_context(void);
166+
PHP_LIBXML_API bool php_libxml_uses_internal_errors(void);
166167

167168
PHP_LIBXML_API zend_string *php_libxml_sniff_charset_from_string(const char *start, const char *end);
168169
PHP_LIBXML_API zend_string *php_libxml_sniff_charset_from_stream(const php_stream *s);

0 commit comments

Comments
 (0)