Skip to content

Commit 18d554f

Browse files
committed
ext/libxml: Fixed custom external entity loader returning an invalid resource leading to a confusing TypeError message
1 parent 071f707 commit 18d554f

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ PHP NEWS
3232
(David Carlier)
3333
. Fix UConverter::transcode with substitutes as references. (David Carlier)
3434

35+
- libxml:
36+
. Fixed custom external entity loader returning an invalid resource leading
37+
to a confusing TypeError message. (Girgias)
38+
3539
- Mbstring:
3640
. Fixed bug GH-17989 (mb_output_handler crash with unset
3741
http_output_conv_mimetypes). (nielsdos)

ext/libxml/libxml.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -793,13 +793,18 @@ static xmlParserInputPtr _php_libxml_external_entity_loader(const char *URL,
793793
is_string:
794794
resource = Z_STRVAL(retval);
795795
} else if (Z_TYPE(retval) == IS_RESOURCE) {
796-
php_stream *stream;
797-
php_stream_from_zval_no_verify(stream, &retval);
798-
if (stream == NULL) {
799-
php_libxml_ctx_error(context,
800-
"The user entity loader callback '%s' has returned a "
801-
"resource, but it is not a stream",
802-
ZSTR_VAL(LIBXML(entity_loader_callback).function_handler->common.function_name));
796+
php_stream *stream = (php_stream*)zend_fetch_resource2_ex(&retval, NULL, php_file_le_stream(), php_file_le_pstream());
797+
if (UNEXPECTED(stream == NULL)) {
798+
zval callable;
799+
zend_get_callable_zval_from_fcc(&LIBXML(entity_loader_callback), &callable);
800+
zend_string *callable_name = zend_get_callable_name(&callable);
801+
zend_string *func_name = get_active_function_or_method_name();
802+
zend_type_error(
803+
"%s(): The user entity loader callback \"%s\" has returned a resource, but it is not a stream",
804+
ZSTR_VAL(func_name), ZSTR_VAL(callable_name));
805+
zend_string_release(func_name);
806+
zend_string_release(callable_name);
807+
zval_ptr_dtor(&callable);
803808
} else {
804809
/* TODO: allow storing the encoding in the stream context? */
805810
xmlCharEncoding enc = XML_CHAR_ENCODING_NONE;

ext/libxml/tests/libxml_get_external_entity_loader_error_callback_name.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ $file = __DIR__ . '/db.dba';
4040
unlink($file);
4141
?>
4242
--EXPECT--
43-
string(73) "DOMDocument::validate(): supplied resource is not a valid stream resource"
43+
string(122) "DOMDocument::validate(): The user entity loader callback "Handler::handle" has returned a resource, but it is not a stream"

0 commit comments

Comments
 (0)