Skip to content

Commit 90e0ce7

Browse files
authored
Throw early when a non-stream-context resource is passed to libxml_set_streams_context() (#14279)
1 parent 412a395 commit 90e0ce7

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ PHP NEWS
9898

9999
- LibXML:
100100
. Added LIBXML_RECOVER constant. (nielsdos)
101+
. libxml_set_streams_context() now throws immediately on an invalid context
102+
instead of at the use-site. (nielsdos)
101103

102104
- MBString:
103105
. Added mb_trim, mb_ltrim and mb_rtrim. (Yuya Hamada)

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,11 @@ PHP 8.4 UPGRADE NOTES
383383
. ResourceBundle::get() now has a tentative return type of:
384384
ResourceBundle|array|string|int|null
385385

386+
- LibXML:
387+
. libxml_set_streams_context() now immediately throws a TypeError when a
388+
non-stream-context resource is passed to the function, instead of throwing
389+
later when the stream context is used.
390+
386391
- MBString:
387392
. The behavior of mb_strcut is more consistent now on invalid UTF-8 and UTF-16
388393
strings. (For valid UTF-8 and UTF-16 strings, there is no change.)

ext/libxml/libxml.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,10 +1049,12 @@ PHP_FUNCTION(libxml_set_streams_context)
10491049
Z_PARAM_RESOURCE(arg)
10501050
ZEND_PARSE_PARAMETERS_END();
10511051

1052-
if (!Z_ISUNDEF(LIBXML(stream_context))) {
1053-
zval_ptr_dtor(&LIBXML(stream_context));
1052+
if (php_stream_context_from_zval(arg, true) != NULL) {
1053+
if (!Z_ISUNDEF(LIBXML(stream_context))) {
1054+
zval_ptr_dtor(&LIBXML(stream_context));
1055+
}
1056+
ZVAL_COPY(&LIBXML(stream_context), arg);
10541057
}
1055-
ZVAL_COPY(&LIBXML(stream_context), arg);
10561058
}
10571059
/* }}} */
10581060

ext/libxml/tests/bug63389.phpt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@ Bug #63389 (Missing context check on libxml_set_streams_context() causes memleak
44
libxml
55
--FILE--
66
<?php
7-
$fp = fopen("php://input", "r");
8-
libxml_set_streams_context($fp);
97
try {
108
libxml_set_streams_context("a");
119
} catch (TypeError $e) {
1210
echo $e->getMessage(), "\n";
1311
}
12+
$fp = fopen("php://input", "r");
13+
try {
14+
libxml_set_streams_context($fp);
15+
} catch (TypeError $e) {
16+
echo $e->getMessage(), "\n";
17+
}
1418
echo "okey";
1519
?>
1620
--EXPECT--
1721
libxml_set_streams_context(): Argument #1 ($context) must be of type resource, string given
22+
libxml_set_streams_context(): supplied resource is not a valid Stream-Context resource
1823
okey

0 commit comments

Comments
 (0)