]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Avoid using union wrt. optlen parameter for getsockopt
authorTee KOBAYASHI <xtkoba@gmail.com>
Sun, 26 Jun 2022 08:30:02 +0000 (17:30 +0900)
committerPauli <pauli@openssl.org>
Wed, 29 Jun 2022 02:11:17 +0000 (12:11 +1000)
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/18660)

crypto/bio/bss_dgram.c

index 8e7daa1998638fea1a21e5fd457cea5a7af22a50..4b363829af1d19c4a2386a21f85da0096a63e905 100644 (file)
@@ -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;