From 2bf1164e03ed595fa52b1330cae66db6d3d0676e Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Mon, 20 May 2024 16:29:29 +0200 Subject: [PATCH] Throw early when a non-stream-context resource is passed to libxml_set_streams_context() --- NEWS | 2 ++ UPGRADING | 5 +++++ ext/libxml/libxml.c | 8 +++++--- ext/libxml/tests/bug63389.phpt | 9 +++++++-- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 4416ce1008bdd..348edcc13380d 100644 --- a/NEWS +++ b/NEWS @@ -98,6 +98,8 @@ PHP NEWS - LibXML: . Added LIBXML_RECOVER constant. (nielsdos) + . libxml_set_streams_context() now throws immediately on an invalid context + instead of at the use-site. (nielsdos) - MBString: . Added mb_trim, mb_ltrim and mb_rtrim. (Yuya Hamada) diff --git a/UPGRADING b/UPGRADING index 2507bdd4bec03..2f3121dc929d3 100644 --- a/UPGRADING +++ b/UPGRADING @@ -383,6 +383,11 @@ PHP 8.4 UPGRADE NOTES . ResourceBundle::get() now has a tentative return type of: ResourceBundle|array|string|int|null +- LibXML: + . libxml_set_streams_context() now immediately throws a TypeError when a + non-stream-context resource is passed to the function, instead of throwing + later when the stream context is used. + - MBString: . The behavior of mb_strcut is more consistent now on invalid UTF-8 and UTF-16 strings. (For valid UTF-8 and UTF-16 strings, there is no change.) diff --git a/ext/libxml/libxml.c b/ext/libxml/libxml.c index 3822deed914db..ef108174b0385 100644 --- a/ext/libxml/libxml.c +++ b/ext/libxml/libxml.c @@ -1049,10 +1049,12 @@ PHP_FUNCTION(libxml_set_streams_context) Z_PARAM_RESOURCE(arg) ZEND_PARSE_PARAMETERS_END(); - if (!Z_ISUNDEF(LIBXML(stream_context))) { - zval_ptr_dtor(&LIBXML(stream_context)); + if (php_stream_context_from_zval(arg, true) != NULL) { + if (!Z_ISUNDEF(LIBXML(stream_context))) { + zval_ptr_dtor(&LIBXML(stream_context)); + } + ZVAL_COPY(&LIBXML(stream_context), arg); } - ZVAL_COPY(&LIBXML(stream_context), arg); } /* }}} */ diff --git a/ext/libxml/tests/bug63389.phpt b/ext/libxml/tests/bug63389.phpt index 41623d0c3da88..90a209e7be122 100644 --- a/ext/libxml/tests/bug63389.phpt +++ b/ext/libxml/tests/bug63389.phpt @@ -4,15 +4,20 @@ Bug #63389 (Missing context check on libxml_set_streams_context() causes memleak libxml --FILE-- getMessage(), "\n"; } +$fp = fopen("php://input", "r"); +try { + libxml_set_streams_context($fp); +} catch (TypeError $e) { + echo $e->getMessage(), "\n"; +} echo "okey"; ?> --EXPECT-- libxml_set_streams_context(): Argument #1 ($context) must be of type resource, string given +libxml_set_streams_context(): supplied resource is not a valid Stream-Context resource okey