Skip to content

Commit

Permalink
socket: fds can never be negative
Browse files Browse the repository at this point in the history
  • Loading branch information
gdamore committed Nov 23, 2024
1 parent 6dfdcac commit 0421d8e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/core/sockfd.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ sfd_listener_set_fd(void *arg, const void *buf, size_t sz, nni_type t)
int fd;
int rv;

if ((rv = nni_copyin_int(&fd, buf, sz, NNI_MININT, NNI_MAXINT, t)) !=
0) {
if ((rv = nni_copyin_int(&fd, buf, sz, 0, NNI_MAXINT, t)) != 0) {
return (rv);
}

Expand Down Expand Up @@ -169,7 +168,7 @@ static int
sfd_listener_get_addr(void *arg, void *buf, size_t *szp, nni_type t)
{
NNI_ARG_UNUSED(arg);
nng_sockaddr sa;
nng_sockaddr sa;
sa.s_family = NNG_AF_UNSPEC;
return (nni_copyout_sockaddr(&sa, buf, szp, t));
}
Expand All @@ -181,7 +180,7 @@ static const nni_option sfd_listener_options[] = {
},
{
.o_name = NNG_OPT_LOCADDR,
.o_get = sfd_listener_get_addr,
.o_get = sfd_listener_get_addr,
},
{
.o_name = NULL,
Expand Down
17 changes: 17 additions & 0 deletions src/sp/transport/socket/sockfd_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,22 @@ test_sfd_fd_option_type(void)
#endif
}

void
test_sfd_fd_invalid_fd(void)
{
#ifdef NNG_HAVE_SOCKETPAIR
nng_socket s;
nng_listener l;

NUTS_OPEN(s);
NUTS_PASS(nng_listener_create(&l, s, "socket://"));
NUTS_FAIL(
nng_listener_set_int(l, NNG_OPT_SOCKET_FD, -100), NNG_EINVAL);
NUTS_CLOSE(s);
#else
NUTS_SKIP("no socketpair");
#endif
}
void
test_sfd_fd_dev_zero(void)
{
Expand Down Expand Up @@ -521,6 +537,7 @@ NUTS_TESTS = {
{ "socket pipe peer id", test_sockfd_pipe_peer },
{ "socket listen full", test_sfd_listen_full },
{ "socket bad fd type", test_sfd_fd_option_type },
{ "socket invalid fd", test_sfd_fd_invalid_fd },
{ "socket dev zero", test_sfd_fd_dev_zero },
{ NULL, NULL },
};

0 comments on commit 0421d8e

Please sign in to comment.