Skip to content

Commit

Permalink
Avoid using union wrt. optlen parameter for getsockopt
Browse files Browse the repository at this point in the history
Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #18660)

(cherry picked from commit 8e949b3)
  • Loading branch information
xtkoba authored and paulidale committed Jun 29, 2022
1 parent 47741c5 commit 99ed66b
Showing 1 changed file with 24 additions and 42 deletions.
66 changes: 24 additions & 42 deletions crypto/bio/bss_dgram.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 */
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 99ed66b

Please sign in to comment.