Skip to content

Commit 0039af0

Browse files
committed
Propagate bind error for stream_socket_server()
When stream_socket_server() fails during bind(), we're currently only showing "Unknown error" in the error message. Properly propagate this error for better diagnostics. Closes phpGH-21328
1 parent f44609c commit 0039af0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ PHP NEWS
145145
. Allowed filtered streams to be casted as fd for select. (Jakub Zelenka)
146146
. Fixed bug GH-21221 (Prevent closing of innerstream of php://temp stream).
147147
(ilutov)
148+
. Improved stream_socket_server() bind failure error reporting. (ilutov)
148149

149150
- Zip:
150151
. Fixed ZipArchive callback being called after executor has shut down.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
stream_socket_server() bind error
3+
--SKIPIF--
4+
<?php
5+
if (substr(PHP_OS, 0, 3) == 'WIN' ) {
6+
die('skip not for Windows');
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
$server1 = stream_socket_server("tcp://0.0.0.0:0");
13+
$name = stream_socket_get_name($server1, false);
14+
$server2 = stream_socket_server("tcp://$name");
15+
fclose($server1);
16+
17+
?>
18+
--EXPECTF--
19+
Warning: stream_socket_server(): Unable to connect to tcp://0.0.0.0:%d (Address %r(already )?%rin use) in %s on line %d

main/streams/xp_socket.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,13 @@ static inline int php_tcp_sockop_bind(php_stream *stream, php_netstream_data_t *
699699

700700
parse_unix_address(xparam, &unix_addr);
701701

702-
return bind(sock->socket, (const struct sockaddr *)&unix_addr,
702+
int result = bind(sock->socket, (const struct sockaddr *)&unix_addr,
703703
(socklen_t) XtOffsetOf(struct sockaddr_un, sun_path) + xparam->inputs.namelen);
704+
if (result == -1 && xparam->want_errortext) {
705+
char errstr[256];
706+
xparam->outputs.error_text = strpprintf(0, "%s", php_socket_strerror_s(errno, errstr, sizeof(errstr)));
707+
}
708+
return result;
704709
}
705710
#endif
706711

0 commit comments

Comments
 (0)