Skip to content

Commit f5f6aa7

Browse files
committed
Fix GH-10885: Leaking stream_socket_server context
1 parent 0d4d471 commit f5f6aa7

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

Zend/zend_execute_API.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,18 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown)
270270
zval *zv;
271271

272272
EG(flags) |= EG_FLAGS_IN_RESOURCE_SHUTDOWN;
273+
#if ZEND_DEBUG
274+
char *tmp = getenv("AUTO_CLOSE_RESOURCE_LIST");
275+
if (tmp && !ZEND_ATOL(tmp)) {
276+
goto skip_auto_closing_resource_list;
277+
}
278+
#endif
273279
zend_try {
274280
zend_close_rsrc_list(&EG(regular_list));
275281
} zend_end_try();
282+
#if ZEND_DEBUG
283+
skip_auto_closing_resource_list:
284+
#endif
276285

277286
/* No PHP callback functions should be called after this point. */
278287
EG(active) = 0;

Zend/zend_list.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ void zend_close_rsrc_list(HashTable *ht)
230230

231231
void zend_destroy_rsrc_list(HashTable *ht)
232232
{
233+
#if ZEND_DEBUG
234+
char *tmp = getenv("AUTO_CLOSE_RESOURCE_LIST");
235+
if (tmp && !ZEND_ATOL(tmp)) {
236+
if (!(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
237+
pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
238+
}
239+
return;
240+
}
241+
#endif
242+
233243
zend_hash_graceful_reverse_destroy(ht);
234244
}
235245

ext/standard/streamsfuncs.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ PHP_FUNCTION(stream_socket_server)
213213

214214
context = php_stream_context_from_zval(zcontext, flags & PHP_FILE_NO_DEFAULT_CONTEXT);
215215

216-
if (context) {
217-
GC_ADDREF(context->res);
218-
}
219-
220216
if (zerrno) {
221217
ZEND_TRY_ASSIGN_REF_LONG(zerrno, 0);
222218
}

ext/standard/tests/gh10885_1.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-10885: stream_socket_server context leaks
3+
--ENV--
4+
AUTO_CLOSE_RESOURCE_LIST=0
5+
--FILE--
6+
<?php
7+
$context = stream_context_create();
8+
$server = @\stream_socket_server(
9+
'tcp://127.0.0.1:0',
10+
$errno,
11+
$errstr,
12+
\STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN,
13+
$context,
14+
);
15+
fclose($server);
16+
?>
17+
===DONE===
18+
--EXPECT--
19+
===DONE===

ext/standard/tests/gh10885_2.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-10885: stream_socket_server context leaks
3+
--ENV--
4+
AUTO_CLOSE_RESOURCE_LIST=0
5+
--FILE--
6+
<?php
7+
$context = stream_context_create();
8+
$result = @\stream_socket_server(
9+
'tcp://nonexistent:0',
10+
$errno,
11+
$errstr,
12+
\STREAM_SERVER_BIND | \STREAM_SERVER_LISTEN,
13+
$context,
14+
);
15+
var_dump($result);
16+
?>
17+
--EXPECT--
18+
bool(false)

0 commit comments

Comments
 (0)