Skip to content

Commit d9ff5e0

Browse files
committed
Fix GH-8472: stream_socket_accept result may have incorrect metadata
1 parent a807092 commit d9ff5e0

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ PHP NEWS
2424
. Fixed bug GH-9017 (php_stream_sock_open_from_socket could return NULL).
2525
(Heiko Weber)
2626

27+
- Streams:
28+
. Fixed bug GH-8472 (The resource returned by stream_socket_accept may have
29+
incorrect metadata). (Jakub Zelenka)
30+
2731
04 Aug 2022, PHP 8.0.22
2832

2933
- CLI:

ext/openssl/xp_ssl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,10 @@ static inline int php_openssl_tcp_sockop_accept(php_stream *stream, php_openssl_
22872287
memcpy(clisockdata, sock, sizeof(clisockdata->s));
22882288

22892289
clisockdata->s.socket = clisock;
2290+
#ifdef __linux__
2291+
/* O_NONBLOCK is not inherited on Linux */
2292+
clisockdata->s.is_blocked = 1;
2293+
#endif
22902294

22912295
xparam->outputs.client = php_stream_alloc_rel(stream->ops, clisockdata, NULL, "r+");
22922296
if (xparam->outputs.client) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-8472: The resource returned by stream_socket_accept may have incorrect metadata
3+
--FILE--
4+
<?php
5+
function setNonBlocking($stream)
6+
{
7+
$block = stream_get_meta_data($stream)['blocked'];
8+
if ($block) {
9+
stream_set_blocking($stream, false);
10+
}
11+
}
12+
13+
$server = stream_socket_server("tcp://127.0.0.1:9100");
14+
setNonBlocking($server);
15+
16+
$client = stream_socket_client("tcp://127.0.0.1:9100");
17+
18+
$res = stream_socket_accept($server);
19+
stream_set_timeout($res, 1);
20+
setNonBlocking($res);
21+
22+
fwrite($client, str_repeat('0', 5));
23+
24+
$read = [$res];
25+
$write = [];
26+
$except = [];
27+
28+
if (stream_select($read, $write, $except, 1)) {
29+
var_dump(fread($res, 4));
30+
var_dump(fread($res, 4));
31+
}
32+
?>
33+
--EXPECT--
34+
string(4) "0000"
35+
string(1) "0"

main/streams/xp_socket.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,10 @@ static inline int php_tcp_sockop_accept(php_stream *stream, php_netstream_data_t
840840

841841
memcpy(clisockdata, sock, sizeof(*clisockdata));
842842
clisockdata->socket = clisock;
843+
#ifdef __linux__
844+
/* O_NONBLOCK is not inherited on Linux */
845+
clisockdata->is_blocked = 1;
846+
#endif
843847

844848
xparam->outputs.client = php_stream_alloc_rel(stream->ops, clisockdata, NULL, "r+");
845849
if (xparam->outputs.client) {

0 commit comments

Comments
 (0)