From ed64f06909bfbdf2f36ac7f279715cbf3dc0b75e Mon Sep 17 00:00:00 2001 From: Jon Shallow Date: Mon, 25 Sep 2023 15:35:14 +0000 Subject: [PATCH] posix_sockets.c: Fix 16 bit integer compilation errors --- sys/posix/sockets/posix_sockets.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sys/posix/sockets/posix_sockets.c b/sys/posix/sockets/posix_sockets.c index 3933f70a30c1..9110da9d5c8f 100644 --- a/sys/posix/sockets/posix_sockets.c +++ b/sys/posix/sockets/posix_sockets.c @@ -1121,7 +1121,7 @@ int setsockopt(int socket, int level, int option_name, const void *option_value, #ifdef POSIX_SETSOCKOPT socket_t *s; struct timeval *tv; - const uint32_t max_timeout_secs = UINT32_MAX / (1000 * 1000); + const uint32_t max_timeout_secs = UINT32_MAX / US_PER_SEC; if (level != SOL_SOCKET || option_name != SO_RCVTIMEO) { @@ -1144,10 +1144,18 @@ int setsockopt(int socket, int level, int option_name, const void *option_value, tv = (struct timeval *) option_value; +#if MODULE_AVR8_COMMON + /* tv_sec is uint32_t, so never negative */ + if (tv->tv_usec < 0) { + errno = EINVAL; + return -1; + } +#else /* ! MODULE_AVR8_COMMON */ if (tv->tv_sec < 0 || tv->tv_usec < 0) { errno = EINVAL; return -1; } +#endif /* ! MODULE_AVR8_COMMON */ if ((uint32_t)tv->tv_sec > max_timeout_secs || ((uint32_t)tv->tv_sec == max_timeout_secs && (uint32_t)tv->tv_usec > UINT32_MAX - max_timeout_secs * 1000 * 1000)) {