Explicitly cast INVALID_SOCKET to (int) to address warnings on Windows.

Even though SOCKET is effectively declared as (void *) on Windows, it's
not actually a pointer, but an index within per-process table of
kernel objects. The table size is actually limited and its upper limit
is far below upper limit for signed 32-bit integer. This is what makes
cast in question possible.

Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Tim Hudson <tjh@openssl.org>
This commit is contained in:
Andy Polyakov 2015-09-30 10:15:03 +02:00
parent f93ad22f6a
commit b13fdc4860
5 changed files with 29 additions and 27 deletions

View File

@ -266,7 +266,7 @@ static int init_client_ip(int *sock, const unsigned char ip[4], int port,
else /* ( type == SOCK_DGRAM) */ else /* ( type == SOCK_DGRAM) */
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET) { if (s == (int)INVALID_SOCKET) {
perror("socket"); perror("socket");
return (0); return (0);
} }
@ -303,7 +303,7 @@ int init_client_unix(int *sock, const char *server)
return (0); return (0);
s = socket(AF_UNIX, SOCK_STREAM, 0); s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s == INVALID_SOCKET) { if (s == (int)INVALID_SOCKET) {
perror("socket"); perror("socket");
return (0); return (0);
} }
@ -428,7 +428,7 @@ static int init_server_long(int *sock, int port, char *ip, int type)
else /* type == SOCK_DGRAM */ else /* type == SOCK_DGRAM */
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == INVALID_SOCKET) if (s == (int)INVALID_SOCKET)
goto err; goto err;
# if defined SOL_SOCKET && defined SO_REUSEADDR # if defined SOL_SOCKET && defined SO_REUSEADDR
{ {
@ -472,7 +472,7 @@ static int init_server_unix(int *sock, const char *path)
return (0); return (0);
s = socket(AF_UNIX, SOCK_STREAM, 0); s = socket(AF_UNIX, SOCK_STREAM, 0);
if (s == INVALID_SOCKET) if (s == (int)INVALID_SOCKET)
goto err; goto err;
memset(&server, 0, sizeof(server)); memset(&server, 0, sizeof(server));
@ -527,7 +527,7 @@ static int do_accept(int acc_sock, int *sock, char **host)
* can either go for (int *) or (void *). * can either go for (int *) or (void *).
*/ */
ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len); ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len);
if (ret == INVALID_SOCKET) { if (ret == (int)INVALID_SOCKET) {
# if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)) # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
int i; int i;
i = WSAGetLastError(); i = WSAGetLastError();
@ -589,7 +589,7 @@ static int do_accept_unix(int acc_sock, int *sock)
redoit: redoit:
ret = accept(acc_sock, NULL, NULL); ret = accept(acc_sock, NULL, NULL);
if (ret == INVALID_SOCKET) { if (ret == (int)INVALID_SOCKET) {
if (errno == EINTR) { if (errno == EINTR) {
/* /*
* check_timeout(); * check_timeout();

View File

@ -391,7 +391,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
struct sockaddr_in6 sa_in6; struct sockaddr_in6 sa_in6;
# endif # endif
} server, client; } server, client;
int s = INVALID_SOCKET, cs, addrlen; int s = (int)INVALID_SOCKET, cs, addrlen;
unsigned char ip[4]; unsigned char ip[4];
unsigned short port; unsigned short port;
char *str = NULL, *e; char *str = NULL, *e;
@ -400,10 +400,10 @@ int BIO_get_accept_socket(char *host, int bind_mode)
int err_num; int err_num;
if (BIO_sock_init() != 1) if (BIO_sock_init() != 1)
return (INVALID_SOCKET); return ((int)INVALID_SOCKET);
if ((str = BUF_strdup(host)) == NULL) if ((str = BUF_strdup(host)) == NULL)
return (INVALID_SOCKET); return ((int)INVALID_SOCKET);
h = p = NULL; h = p = NULL;
h = str; h = str;
@ -503,7 +503,7 @@ int BIO_get_accept_socket(char *host, int bind_mode)
again: again:
s = socket(server.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL); s = socket(server.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL);
if (s == INVALID_SOCKET) { if (s == (int)INVALID_SOCKET) {
SYSerr(SYS_F_SOCKET, get_last_socket_error()); SYSerr(SYS_F_SOCKET, get_last_socket_error());
ERR_add_error_data(3, "port='", host, "'"); ERR_add_error_data(3, "port='", host, "'");
BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET); BIOerr(BIO_F_BIO_GET_ACCEPT_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET);
@ -545,11 +545,11 @@ int BIO_get_accept_socket(char *host, int bind_mode)
goto err; goto err;
} }
cs = socket(client.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL); cs = socket(client.sa.sa_family, SOCK_STREAM, SOCKET_PROTOCOL);
if (cs != INVALID_SOCKET) { if (cs != (int)INVALID_SOCKET) {
int ii; int ii;
ii = connect(cs, &client.sa, addrlen); ii = connect(cs, &client.sa, addrlen);
closesocket(cs); closesocket(cs);
if (ii == INVALID_SOCKET) { if (ii == (int)INVALID_SOCKET) {
bind_mode = BIO_BIND_REUSEADDR; bind_mode = BIO_BIND_REUSEADDR;
closesocket(s); closesocket(s);
goto again; goto again;
@ -573,16 +573,16 @@ int BIO_get_accept_socket(char *host, int bind_mode)
ret = 1; ret = 1;
err: err:
OPENSSL_free(str); OPENSSL_free(str);
if ((ret == 0) && (s != INVALID_SOCKET)) { if ((ret == 0) && (s != (int)INVALID_SOCKET)) {
closesocket(s); closesocket(s);
s = INVALID_SOCKET; s = (int)INVALID_SOCKET;
} }
return (s); return (s);
} }
int BIO_accept(int sock, char **addr) int BIO_accept(int sock, char **addr)
{ {
int ret = INVALID_SOCKET; int ret = (int)INVALID_SOCKET;
unsigned long l; unsigned long l;
unsigned short port; unsigned short port;
char *p; char *p;
@ -631,7 +631,7 @@ int BIO_accept(int sock, char **addr)
sa.len.i = (int)sa.len.s; sa.len.i = (int)sa.len.s;
/* use sa.len.i from this point */ /* use sa.len.i from this point */
} }
if (ret == INVALID_SOCKET) { if (ret == (int)INVALID_SOCKET) {
if (BIO_sock_should_retry(ret)) if (BIO_sock_should_retry(ret))
return -2; return -2;
SYSerr(SYS_F_ACCEPT, get_last_socket_error()); SYSerr(SYS_F_ACCEPT, get_last_socket_error());

View File

@ -123,7 +123,7 @@ static int acpt_new(BIO *bi)
BIO_ACCEPT *ba; BIO_ACCEPT *ba;
bi->init = 0; bi->init = 0;
bi->num = INVALID_SOCKET; bi->num = (int)INVALID_SOCKET;
bi->flags = 0; bi->flags = 0;
if ((ba = BIO_ACCEPT_new()) == NULL) if ((ba = BIO_ACCEPT_new()) == NULL)
return (0); return (0);
@ -139,7 +139,7 @@ static BIO_ACCEPT *BIO_ACCEPT_new(void)
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
return (NULL); return (NULL);
ret->accept_sock = INVALID_SOCKET; ret->accept_sock = (int)INVALID_SOCKET;
ret->bind_mode = BIO_BIND_NORMAL; ret->bind_mode = BIO_BIND_NORMAL;
return (ret); return (ret);
} }
@ -160,11 +160,11 @@ static void acpt_close_socket(BIO *bio)
BIO_ACCEPT *c; BIO_ACCEPT *c;
c = (BIO_ACCEPT *)bio->ptr; c = (BIO_ACCEPT *)bio->ptr;
if (c->accept_sock != INVALID_SOCKET) { if (c->accept_sock != (int)INVALID_SOCKET) {
shutdown(c->accept_sock, 2); shutdown(c->accept_sock, 2);
closesocket(c->accept_sock); closesocket(c->accept_sock);
c->accept_sock = INVALID_SOCKET; c->accept_sock = (int)INVALID_SOCKET;
bio->num = INVALID_SOCKET; bio->num = (int)INVALID_SOCKET;
} }
} }
@ -200,7 +200,7 @@ static int acpt_state(BIO *b, BIO_ACCEPT *c)
return (-1); return (-1);
} }
s = BIO_get_accept_socket(c->param_addr, c->bind_mode); s = BIO_get_accept_socket(c->param_addr, c->bind_mode);
if (s == INVALID_SOCKET) if (s == (int)INVALID_SOCKET)
return (-1); return (-1);
if (c->accept_nbio) { if (c->accept_nbio) {

View File

@ -189,7 +189,7 @@ static int conn_state(BIO *b, BIO_CONNECT *c)
c->state = BIO_CONN_S_CREATE_SOCKET; c->state = BIO_CONN_S_CREATE_SOCKET;
ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ret = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ret == INVALID_SOCKET) { if (ret == (int)INVALID_SOCKET) {
SYSerr(SYS_F_SOCKET, get_last_socket_error()); SYSerr(SYS_F_SOCKET, get_last_socket_error());
ERR_add_error_data(4, "host=", c->param_hostname, ERR_add_error_data(4, "host=", c->param_hostname,
":", c->param_port); ":", c->param_port);
@ -313,7 +313,7 @@ BIO_METHOD *BIO_s_connect(void)
static int conn_new(BIO *bi) static int conn_new(BIO *bi)
{ {
bi->init = 0; bi->init = 0;
bi->num = INVALID_SOCKET; bi->num = (int)INVALID_SOCKET;
bi->flags = 0; bi->flags = 0;
if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
return (0); return (0);
@ -326,12 +326,12 @@ static void conn_close_socket(BIO *bio)
BIO_CONNECT *c; BIO_CONNECT *c;
c = (BIO_CONNECT *)bio->ptr; c = (BIO_CONNECT *)bio->ptr;
if (bio->num != INVALID_SOCKET) { if (bio->num != (int)INVALID_SOCKET) {
/* Only do a shutdown if things were established */ /* Only do a shutdown if things were established */
if (c->state == BIO_CONN_S_OK) if (c->state == BIO_CONN_S_OK)
shutdown(bio->num, 2); shutdown(bio->num, 2);
closesocket(bio->num); closesocket(bio->num);
bio->num = INVALID_SOCKET; bio->num = (int)INVALID_SOCKET;
} }
} }

4
e_os.h
View File

@ -477,7 +477,9 @@ struct servent *PASCAL getservbyname(const char *, const char *);
/* /*
* Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because * Even though sizeof(SOCKET) is 8, it's safe to cast it to int, because
* the value constitutes an index in per-process table of limited size * the value constitutes an index in per-process table of limited size
* and not a real pointer. * and not a real pointer. And we also depend on fact that all processors
* Windows run on happen to be two's-complement, which allows to
* interchange INVALID_SOCKET and -1.
*/ */
# define socket(d,t,p) ((int)socket(d,t,p)) # define socket(d,t,p) ((int)socket(d,t,p))
# define accept(s,f,l) ((int)accept(s,f,l)) # define accept(s,f,l) ((int)accept(s,f,l))