about ssize_t nread when error in uv_udp_recv_cb

34 views
Skip to first unread message

Iñaki Baz Castillo

unread,
Aug 2, 2019, 10:01:45 AM8/2/19
to li...@googlegroups.com
> void (*uv_udp_recv_cb)(uv_udp_t* handle, ssize_t nread, const uv_buf_t* buf, const struct sockaddr* addr, unsigned flags)
>
> nread: Number of bytes that have been received. 0 if there is no more data to read. You may discard or repurpose the read buffer. Note that 0 may also mean that an empty datagram was received (in this case addr is not NULL). < 0 if a transmission error was detected.


So, if nread is < 0 it must be considered an error. However, ssize_t
can just be -1 or 0 or a positive value. So, if read operation failed,
nread will always be -1. Is that true?

And, if so, does it make sense to do this?

printf("UV UDP read error: %s", uv_strerror(nread));

Thanks.


--
Iñaki Baz Castillo
<i...@aliax.net>

Ben Noordhuis

unread,
Aug 3, 2019, 6:05:56 AM8/3/19
to li...@googlegroups.com
When nread < 0, it's one of the UV_E* status codes. Those are always < 0.

-1 corresponds to UV_EPERM on most platforms.

If your assumption is that because the type is ssize_t it can only be
>= -1, that's mistaken. Hope that helps!

Iñaki Baz Castillo

unread,
Aug 3, 2019, 6:08:37 AM8/3/19
to li...@googlegroups.com
On Sat, 3 Aug 2019 at 12:05, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
> -1 corresponds to UV_EPERM on most platforms.
>
> If your assumption is that because the type is ssize_t it can only be
> >= -1, that's mistaken. Hope that helps!

What is mistaken? All references I read about ssize_t say that it's
supposed to be >= -1:

For instance, https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html:

> The type ssize_t shall be capable of storing values at least in the range [-1, {SSIZE_MAX}].

Ben Noordhuis

unread,
Aug 4, 2019, 2:50:16 PM8/4/19
to li...@googlegroups.com
Note the "at least." :-)

In practice it's a signed type that's capable of representing at least
-INT_MAX-1 to INT_MAX.
Reply all
Reply to author
Forward
0 new messages