From ab7c730fa6df3ed1f25b303416e255d0d022e6df Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Sat, 10 Feb 2024 13:10:04 +0000 Subject: [PATCH] coap_io.c: Cleanup timeout checking code in coap_io_process_with_fds() Should fix CID: 1583564 --- src/coap_io.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/coap_io.c b/src/coap_io.c index 0bccd4a0fe..7c54eedb1f 100644 --- a/src/coap_io.c +++ b/src/coap_io.c @@ -1561,7 +1561,8 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, timeout = coap_io_prepare_io(ctx, ctx->sockets, (sizeof(ctx->sockets) / sizeof(ctx->sockets[0])), &ctx->num_sockets, before); - if (timeout == 0 || timeout_ms < timeout) + if (timeout_ms != COAP_IO_NO_WAIT && timeout_ms != COAP_IO_WAIT && + timeout_ms < timeout) timeout = timeout_ms; if (ereadfds) { @@ -1604,6 +1605,7 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, tv.tv_sec = 0; timeout = 1; } else if (timeout > 0) { + /* Ignore timeout_ms == COAP_IO_WAIT as there is something coming up */ tv.tv_usec = (timeout % 1000) * 1000; tv.tv_sec = (long)(timeout / 1000); } @@ -1672,7 +1674,8 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, timeout = coap_io_prepare_epoll(ctx, before); - if (timeout == 0 || timeout_ms < timeout) + if (timeout_ms != COAP_IO_NO_WAIT && timeout_ms != COAP_IO_WAIT && + timeout_ms < timeout) timeout = timeout_ms; do { @@ -1681,12 +1684,19 @@ coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms, /* Potentially adjust based on what the caller wants */ if (timeout_ms == COAP_IO_NO_WAIT) { + /* Need to return immediately from epoll_wait() */ etimeout = 0; - } else if (timeout == COAP_IO_WAIT) { - /* coap_io_prepare_epoll() returned 0 and timeout_ms COAP_IO_WAIT (0) */ + } else if (timeout == 0 && timeout_ms == COAP_IO_WAIT) { + /* + * Nothing found in coap_io_prepare_epoll() and COAP_IO_WAIT set, + * so wait forever in epoll_wait(). + */ etimeout = -1; } else if (etimeout < 0) { - /* epoll_wait cannot wait longer than this as int timeout parameter */ + /* + * If timeout > INT_MAX, epoll_wait() cannot wait longer than this as + * it has int timeout parameter + */ etimeout = INT_MAX; }