From 2494c088f0aacb50305b1c05a81ea64a28b21caf Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Mon, 9 Dec 2024 21:14:01 +0100 Subject: [PATCH] unix,win: handle nbufs=0 in uv_udp_try_send (#4641) --- src/unix/udp.c | 3 ++- src/win/udp.c | 3 ++- test/test-udp-try-send.c | 4 ++++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/unix/udp.c b/src/unix/udp.c index f6640fc7..c3e30314 100644 --- a/src/unix/udp.c +++ b/src/unix/udp.c @@ -793,7 +793,8 @@ int uv__udp_try_send(uv_udp_t* handle, struct msghdr h; ssize_t size; - assert(nbufs > 0); + if (nbufs < 1) + return UV_EINVAL; /* already sending a message */ if (handle->send_queue_count != 0) diff --git a/src/win/udp.c b/src/win/udp.c index 5c8f6e1d..557b4e60 100644 --- a/src/win/udp.c +++ b/src/win/udp.c @@ -1101,7 +1101,8 @@ int uv__udp_try_send(uv_udp_t* handle, struct sockaddr_storage converted; int err; - assert(nbufs > 0); + if (nbufs < 1) + return UV_EINVAL; if (addr != NULL) { err = uv__convert_to_localhost_if_unspecified(addr, &converted); diff --git a/test/test-udp-try-send.c b/test/test-udp-try-send.c index 0c76fb1c..04246a82 100644 --- a/test/test-udp-try-send.c +++ b/test/test-udp-try-send.c @@ -101,6 +101,10 @@ TEST_IMPL(udp_try_send) { ASSERT_OK(r); buf = uv_buf_init(buffer, sizeof(buffer)); + + r = uv_udp_try_send(&client, &buf, 0, (const struct sockaddr*) &addr); + ASSERT_EQ(r, UV_EINVAL); + r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &addr); ASSERT_EQ(r, UV_EMSGSIZE);