From 99ed66be3107c5eae9bb391460bd0d286912d1b4 Mon Sep 17 00:00:00 2001 From: Tee KOBAYASHI Date: Sun, 26 Jun 2022 17:30:02 +0900 Subject: Avoid using union wrt. optlen parameter for getsockopt Reviewed-by: Tomas Mraz Reviewed-by: Paul Dale (Merged from https://github.com/openssl/openssl/pull/18660) (cherry picked from commit 8e949b35d396005d63f3a2c944c36a1c94e41019) --- crypto/bio/bss_dgram.c | 66 ++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/crypto/bio/bss_dgram.c b/crypto/bio/bss_dgram.c index 8e7daa1998..4b363829af 100644 --- a/crypto/bio/bss_dgram.c +++ b/crypto/bio/bss_dgram.c @@ -195,12 +195,6 @@ static void dgram_adjust_rcv_timeout(BIO *b) { # if defined(SO_RCVTIMEO) bio_dgram_data *data = (bio_dgram_data *)b->ptr; - union { - size_t s; - int i; - } sz = { - 0 - }; /* Is a timer active? */ if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) { @@ -210,21 +204,21 @@ static void dgram_adjust_rcv_timeout(BIO *b) # ifdef OPENSSL_SYS_WINDOWS int timeout; - sz.i = sizeof(timeout); + int sz = sizeof(timeout); if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, &sz.i) < 0) { + (void *)&timeout, &sz) < 0) { perror("getsockopt"); } else { data->socket_timeout.tv_sec = timeout / 1000; data->socket_timeout.tv_usec = (timeout % 1000) * 1000; } # else - sz.i = sizeof(data->socket_timeout); + socklen_t sz = sizeof(data->socket_timeout); if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, - &(data->socket_timeout), (void *)&sz) < 0) { + &(data->socket_timeout), &sz) < 0) { perror("getsockopt"); - } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) - OPENSSL_assert(sz.s <= sizeof(data->socket_timeout)); + } else + OPENSSL_assert(sz <= sizeof(data->socket_timeout)); # endif /* Get current time */ @@ -607,19 +601,14 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr) break; case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT: { - union { - size_t s; - int i; - } sz = { - 0 - }; # ifdef OPENSSL_SYS_WINDOWS + int sz = 0; int timeout; struct timeval *tv = (struct timeval *)ptr; - sz.i = sizeof(timeout); + sz = sizeof(timeout); if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, - (void *)&timeout, &sz.i) < 0) { + (void *)&timeout, &sz) < 0) { perror("getsockopt"); ret = -1; } else { @@ -628,16 +617,15 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr) ret = sizeof(*tv); } # else - sz.i = sizeof(struct timeval); + socklen_t sz = sizeof(struct timeval); if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, - ptr, (void *)&sz) < 0) { + ptr, &sz) < 0) { perror("getsockopt"); ret = -1; - } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) { - OPENSSL_assert(sz.s <= sizeof(struct timeval)); - ret = (int)sz.s; - } else - ret = sz.i; + } else { + OPENSSL_assert(sz <= sizeof(struct timeval)); + ret = (int)sz; + } # endif } break; @@ -664,19 +652,14 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr) break; case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT: { - union { - size_t s; - int i; - } sz = { - 0 - }; # ifdef OPENSSL_SYS_WINDOWS + int sz = 0; int timeout; struct timeval *tv = (struct timeval *)ptr; - sz.i = sizeof(timeout); + sz = sizeof(timeout); if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, - (void *)&timeout, &sz.i) < 0) { + (void *)&timeout, &sz) < 0) { perror("getsockopt"); ret = -1; } else { @@ -685,16 +668,15 @@ static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr) ret = sizeof(*tv); } # else - sz.i = sizeof(struct timeval); + socklen_t sz = sizeof(struct timeval); if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, - ptr, (void *)&sz) < 0) { + ptr, &sz) < 0) { perror("getsockopt"); ret = -1; - } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) { - OPENSSL_assert(sz.s <= sizeof(struct timeval)); - ret = (int)sz.s; - } else - ret = sz.i; + } else { + OPENSSL_assert(sz <= sizeof(struct timeval)); + ret = (int)sz; + } # endif } break; -- cgit v1.2.3