Commit f5299f7d524 for php
commit f5299f7d52491cfef3ebe2dbe603cbeb404a3577
Author: lacatoire <la.catoire@gmail.com>
Date: Fri Oct 16 15:30:45 2026 +0200
ext/sockets: Fix socket_accept() storing its error on the discarded socket
socket_accept() stored the error of a failed accept() on the Socket
allocated for the connection, which is destroyed right away, so
socket_last_error($listener) and socket_clear_error($listener) never saw
it. Store it on the listening socket, like socket_listen() and
socket_connect() do.
Close GH-26031
diff --git a/NEWS b/NEWS
index e4c6af5315d..f804b1faa6e 100644
--- a/NEWS
+++ b/NEWS
@@ -196,6 +196,10 @@ PHP NEWS
. Fixed bug GH-24315 (Harden SoapClient cross-origin redirect handling).
(Ilia Alshanetsky)
+- Sockets:
+ . Fixed socket_accept() storing its error on the discarded socket instead of
+ the listening one. (lacatoire)
+
- SPL:
. Fixed bug GH-23385 (SplDoublyLinkedList::serialize() use-after-free when
__serialize() removes an element). (David Carlier)
diff --git a/UPGRADING b/UPGRADING
index 37b3c87966b..66ab8b394a1 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -144,6 +144,10 @@ PHP 8.7 UPGRADE NOTES
5. Changed Functions
========================================
+- Sockets:
+ . socket_accept() now records a failure on the listening socket, so that
+ socket_last_error($socket) and socket_clear_error($socket) see it.
+
- Standard:
. stream_select() no longer fails once a watched descriptor exceeds
FD_SETSIZE. The descriptor set now grows on demand on both POSIX (bitset)
diff --git a/ext/sockets/sockets.c b/ext/sockets/sockets.c
index 13a2f6ade76..20e06f80dde 100644
--- a/ext/sockets/sockets.c
+++ b/ext/sockets/sockets.c
@@ -280,14 +280,14 @@ static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct
out_sock->bsd_socket = accept4(in_sock->bsd_socket, la, la_len, flags);
if (IS_INVALID_SOCKET(out_sock)) {
- PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
+ PHP_SOCKET_ERROR(in_sock, "unable to accept incoming connection", errno);
return false;
}
#else
out_sock->bsd_socket = accept(in_sock->bsd_socket, la, la_len);
if (IS_INVALID_SOCKET(out_sock)) {
- PHP_SOCKET_ERROR(out_sock, "unable to accept incoming connection", errno);
+ PHP_SOCKET_ERROR(in_sock, "unable to accept incoming connection", errno);
return false;
}
@@ -299,7 +299,7 @@ static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct
int mode;
if ((mode = fcntl(out_sock->bsd_socket, F_GETFD)) < 0) {
- PHP_SOCKET_ERROR(out_sock, "unable to get fcntl mode on the socket", errno);
+ PHP_SOCKET_ERROR(in_sock, "unable to get fcntl mode on the socket", errno);
return false;
}
@@ -307,7 +307,7 @@ static bool php_accept_connect(php_socket *in_sock, php_socket *out_sock, struct
if (mode != cloexec) {
if (fcntl(out_sock->bsd_socket, F_SETFD, cloexec) < 0) {
- PHP_SOCKET_ERROR(out_sock, "unable to set cloexec mode on the socket", errno);
+ PHP_SOCKET_ERROR(in_sock, "unable to set cloexec mode on the socket", errno);
return false;
}
}
diff --git a/ext/sockets/tests/socket_accept_error_on_listener.phpt b/ext/sockets/tests/socket_accept_error_on_listener.phpt
new file mode 100644
index 00000000000..04c6de42e5c
--- /dev/null
+++ b/ext/sockets/tests/socket_accept_error_on_listener.phpt
@@ -0,0 +1,30 @@
+--TEST--
+socket_accept(): a failed accept stores the error on the listening socket
+--EXTENSIONS--
+sockets
+--SKIPIF--
+<?php
+if (strtolower(substr(PHP_OS, 0, 3)) == 'win') die('skip not for Windows');
+?>
+--FILE--
+<?php
+$l = socket_create(AF_INET, SOCK_STREAM, 0);
+socket_bind($l, '127.0.0.1', 0);
+socket_listen($l);
+socket_set_nonblock($l);
+var_dump(socket_accept($l));
+var_dump(socket_last_error($l) === SOCKET_EAGAIN, socket_last_error() === SOCKET_EAGAIN);
+socket_clear_error($l);
+var_dump(socket_last_error($l));
+
+$u = socket_create(AF_INET, SOCK_STREAM, 0);
+var_dump(@socket_accept($u));
+var_dump(socket_last_error($u) === SOCKET_EINVAL);
+?>
+--EXPECT--
+bool(false)
+bool(true)
+bool(true)
+int(0)
+bool(false)
+bool(true)