Skip to content

Commit 576fcab

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

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,20 @@ PHP_FUNCTION(stream_socket_server)
241241
} else if (errstr) {
242242
zend_string_release_ex(errstr, 0);
243243
}
244-
RETURN_FALSE;
244+
RETVAL_FALSE;
245+
goto exit;
245246
}
246247

247248
if (errstr) {
248249
zend_string_release_ex(errstr, 0);
249250
}
250251

251252
php_stream_to_zval(stream, return_value);
253+
254+
exit:
255+
if (zcontext) {
256+
zval_ptr_dtor(zcontext);
257+
}
252258
}
253259
/* }}} */
254260

ext/standard/tests/gh10885.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===

0 commit comments

Comments
 (0)