Skip to content

Throw early when a non-stream-context resource is passed to libxml_set_streams_context() #14279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
8 changes: 5 additions & 3 deletions ext/libxml/libxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
/* }}} */

Expand Down
9 changes: 7 additions & 2 deletions ext/libxml/tests/bug63389.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ Bug #63389 (Missing context check on libxml_set_streams_context() causes memleak
libxml
--FILE--
<?php
$fp = fopen("php://input", "r");
libxml_set_streams_context($fp);
try {
libxml_set_streams_context("a");
} catch (TypeError $e) {
echo $e->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
Loading