Skip to content

Commit 9f4d767

Browse files
committed
Improve service docs, fd validation, and defensive guards
- local_datagram_service.hpp: platform-neutral docs with ownership semantics for assign_socket and bind_socket - local_stream_service.hpp: document ownership transfer semantics for assign_socket (impl takes ownership on success, caller retains on failure) - local_datagram.hpp: convert block comment to Doxygen with @see tag - win_local_stream_acceptor_service.hpp: null-guard release_socket() against reset internal_ - epoll_local_datagram_service.hpp: validate fd >= 0 in assign_socket - kqueue_local_stream_service.hpp: validate fd >= 0 in assign_socket - kqueue_local_datagram_service.hpp: validate fd >= 0 in assign_socket - local_stream_acceptor.cpp: add explicit #include <cstring> for std::memcpy
1 parent 27f4448 commit 9f4d767

8 files changed

Lines changed: 49 additions & 25 deletions

include/boost/corosio/detail/local_datagram_service.hpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818

1919
namespace boost::corosio::detail {
2020

21-
/* Abstract local datagram service base class.
21+
/** Abstract local datagram service base class.
2222
23-
Concrete implementations (epoll, select, kqueue) inherit from
24-
this class and provide platform-specific datagram socket operations
25-
for Unix domain sockets. The context constructor installs
26-
whichever backend via make_service, and local_datagram_socket.cpp
27-
retrieves it via use_service<local_datagram_service>().
23+
Concrete implementations (epoll, select, kqueue, IOCP)
24+
inherit from this class and provide platform-specific
25+
datagram socket operations for local (Unix domain) sockets.
26+
27+
Instances are looked up via key_type in the
28+
execution_context. All errors are reported via the returned
29+
std::error_code; these methods do not throw.
2830
*/
2931
class BOOST_COROSIO_DECL local_datagram_service
3032
: public capy::execution_context::service
@@ -34,14 +36,16 @@ class BOOST_COROSIO_DECL local_datagram_service
3436
/// Identifies this service for execution_context lookup.
3537
using key_type = local_datagram_service;
3638

37-
/** Open a Unix datagram socket.
39+
/** Open a local (Unix domain) datagram socket.
3840
39-
Creates a socket and associates it with the platform reactor.
41+
Creates a socket and associates it with the platform
42+
I/O backend (reactor or IOCP).
4043
4144
@param impl The socket implementation to open.
42-
@param family Address family (AF_UNIX).
43-
@param type Socket type (SOCK_DGRAM).
44-
@param protocol Protocol number (0).
45+
Must not already represent an open socket.
46+
@param family Address family for local IPC.
47+
@param type Socket type for datagram sockets.
48+
@param protocol Protocol number (typically 0).
4549
@return Error code on failure, empty on success.
4650
*/
4751
virtual std::error_code open_socket(
@@ -50,12 +54,16 @@ class BOOST_COROSIO_DECL local_datagram_service
5054
int type,
5155
int protocol) = 0;
5256

53-
/** Assign an existing file descriptor to a socket.
57+
/** Assign an existing native socket handle to a socket.
5458
55-
Used by socketpair() to adopt pre-created fds.
59+
Adopts a pre-created socket handle. On success the
60+
impl takes ownership and will close the handle. On
61+
failure the caller retains ownership and must close
62+
it. On platforms that do not support handle adoption,
63+
returns @c operation_not_supported.
5664
5765
@param impl The socket implementation to assign to.
58-
@param fd The file descriptor to adopt.
66+
@param fd The native socket handle to adopt.
5967
@return Error code on failure, empty on success.
6068
*/
6169
virtual std::error_code assign_socket(
@@ -64,8 +72,10 @@ class BOOST_COROSIO_DECL local_datagram_service
6472

6573
/** Bind a datagram socket to a local endpoint.
6674
75+
@pre @p impl was opened via open_socket().
6776
@param impl The socket implementation to bind.
6877
@param ep The local endpoint to bind to.
78+
Copied; need not remain valid after the call.
6979
@return Error code on failure, empty on success.
7080
*/
7181
virtual std::error_code bind_socket(

include/boost/corosio/detail/local_stream_service.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class BOOST_COROSIO_DECL local_stream_service
5353
/** Assign an existing native socket handle to a socket.
5454
5555
Adopts a pre-created socket handle (e.g. from a
56-
platform-specific pair creation API). On platforms
57-
that do not support handle adoption, returns
58-
@c operation_not_supported.
56+
platform-specific pair creation API). On success the
57+
impl takes ownership and will close the handle. On
58+
failure the caller retains ownership and must close
59+
it. On platforms that do not support handle adoption,
60+
returns @c operation_not_supported.
5961
6062
@param impl The socket implementation to assign to.
6163
@param fd The native socket handle to adopt.

include/boost/corosio/local_datagram.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ namespace boost::corosio {
1616

1717
class local_datagram_socket;
1818

19-
/* Encapsulate the Unix datagram protocol for socket creation.
19+
/** Protocol tag for local (Unix domain) datagram sockets.
2020
21-
This class identifies the Unix datagram protocol (AF_UNIX,
22-
SOCK_DGRAM). It is used to parameterize socket open() calls
23-
with a self-documenting type.
21+
Identifies the local datagram protocol for parameterizing
22+
socket open() calls with a self-documenting type.
2423
25-
The family(), type(), and protocol() members are implemented
26-
in the compiled library to avoid exposing platform socket
27-
headers.
24+
The family(), type(), and protocol() members are implemented
25+
in the compiled library to avoid exposing platform socket
26+
headers.
2827
29-
See local_datagram_socket
28+
@see local_datagram_socket
3029
*/
3130
class BOOST_COROSIO_DECL local_datagram
3231
{

include/boost/corosio/native/detail/epoll/epoll_local_datagram_service.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ epoll_local_datagram_service::assign_socket(
267267
local_datagram_socket::implementation& impl,
268268
int fd)
269269
{
270+
if (fd < 0)
271+
return make_err(EBADF);
272+
270273
auto* epoll_impl = static_cast<epoll_local_datagram_socket*>(&impl);
271274
epoll_impl->close_socket();
272275

include/boost/corosio/native/detail/iocp/win_local_stream_acceptor_service.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ win_local_stream_acceptor::cancel() noexcept
404404
inline native_handle_type
405405
win_local_stream_acceptor::release_socket() noexcept
406406
{
407+
if (!internal_)
408+
return static_cast<native_handle_type>(INVALID_SOCKET);
407409
SOCKET s = internal_->socket_;
408410
if (s != INVALID_SOCKET)
409411
{

include/boost/corosio/native/detail/kqueue/kqueue_local_datagram_service.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ kqueue_local_datagram_service::assign_socket(
301301
local_datagram_socket::implementation& impl,
302302
int fd)
303303
{
304+
if (fd < 0)
305+
return make_err(EBADF);
306+
304307
auto* kq_impl = static_cast<kqueue_local_datagram_socket*>(&impl);
305308
kq_impl->close_socket();
306309

include/boost/corosio/native/detail/kqueue/kqueue_local_stream_service.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ kqueue_local_stream_service::assign_socket(
229229
local_stream_socket::implementation& impl,
230230
int fd)
231231
{
232+
if (fd < 0)
233+
return make_err(EBADF);
234+
232235
auto* kq_impl = static_cast<kqueue_local_stream_socket*>(&impl);
233236
kq_impl->close_socket();
234237

src/corosio/src/local_stream_acceptor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <boost/corosio/detail/platform.hpp>
1313
#include <boost/corosio/detail/local_stream_acceptor_service.hpp>
1414

15+
#include <cstring>
16+
1517
#if BOOST_COROSIO_POSIX
1618
#include <unistd.h>
1719
#endif

0 commit comments

Comments
 (0)