Skip to content

Commit e150b0b

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: Fix phpGH-16318: Recursive array segfaults soap encoding
2 parents 7a1f4b6 + f108c16 commit e150b0b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

ext/soap/php_encoding.c

+18
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,13 @@ static void add_xml_array_elements(xmlNodePtr xmlParam,
21492149
xmlNodePtr xparam;
21502150

21512151
if (data && Z_TYPE_P(data) == IS_ARRAY) {
2152+
if (UNEXPECTED(Z_IS_RECURSIVE_P(data))) {
2153+
zend_value_error("Recursive array cannot be encoded");
2154+
return;
2155+
}
2156+
2157+
GC_TRY_PROTECT_RECURSION(Z_ARRVAL_P(data));
2158+
21522159
ZEND_HASH_FOREACH_VAL_IND(Z_ARRVAL_P(data), zdata) {
21532160
if (j >= dims[0]) {
21542161
break;
@@ -2197,6 +2204,8 @@ static void add_xml_array_elements(xmlNodePtr xmlParam,
21972204
j++;
21982205
}
21992206
}
2207+
2208+
GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(data));
22002209
} else {
22012210
for (j=0; j<dims[0]; j++) {
22022211
if (dimension == 1) {
@@ -2714,6 +2723,13 @@ static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodeP
27142723
FIND_ZVAL_NULL(data, xmlParam, style);
27152724

27162725
if (Z_TYPE_P(data) == IS_ARRAY) {
2726+
if (UNEXPECTED(Z_IS_RECURSIVE_P(data))) {
2727+
zend_value_error("Recursive array cannot be encoded");
2728+
return NULL;
2729+
}
2730+
2731+
GC_TRY_PROTECT_RECURSION(Z_ARRVAL_P(data));
2732+
27172733
ZEND_HASH_FOREACH_KEY_VAL_IND(Z_ARRVAL_P(data), int_val, key_val, temp_data) {
27182734
item = xmlNewNode(NULL, BAD_CAST("item"));
27192735
xmlAddChild(xmlParam, item);
@@ -2741,6 +2757,8 @@ static xmlNodePtr to_xml_map(encodeTypePtr type, zval *data, int style, xmlNodeP
27412757
xparam = master_to_xml(get_conversion(Z_TYPE_P(temp_data)), temp_data, style, item);
27422758
xmlNodeSetName(xparam, BAD_CAST("value"));
27432759
} ZEND_HASH_FOREACH_END();
2760+
2761+
GC_TRY_UNPROTECT_RECURSION(Z_ARRVAL_P(data));
27442762
}
27452763
if (style == SOAP_ENCODED) {
27462764
set_ns_and_type(xmlParam, type);

ext/soap/tests/gh16318.phpt

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--TEST--
2+
GH-16318 (Recursive array segfaults soap encoding)
3+
--EXTENSIONS--
4+
soap
5+
--FILE--
6+
<?php
7+
8+
// SOAP-ENC array
9+
$tmp =& $test1;
10+
$test1[] = $tmp;
11+
12+
// map array
13+
$test2 = [];
14+
$test2["a"] = "a";
15+
$test2[] =& $test2;
16+
17+
class TestSoapClient extends SoapClient {
18+
public function __doRequest(string $request, string $location, string $action, int $version, bool $oneWay = false): ?string
19+
{
20+
die($request);
21+
}
22+
}
23+
$client = new TestSoapClient(NULL,array("location"=>"test://","uri"=>"http://soapinterop.org/","trace"=>1,"exceptions"=>0));
24+
25+
foreach ([$test1, $test2] as $test) {
26+
try {
27+
$client->__soapCall("echoStructArray", array($test), array("soapaction"=>"http://soapinterop.org/","uri"=>"http://soapinterop.org/"));
28+
} catch (ValueError $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
}
32+
33+
?>
34+
--EXPECT--
35+
Recursive array cannot be encoded
36+
Recursive array cannot be encoded

0 commit comments

Comments
 (0)