Skip to content

Commit

Permalink
Fix strict-aliasing violation
Browse files Browse the repository at this point in the history
- I always knew about this, but I thought compilers made a special case
  to take care of it
  • Loading branch information
N7Alpha committed May 20, 2024
1 parent 129f27d commit 6e2fe1d
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions Source/UnrealLibretro/Private/sam2.h
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,24 @@ static void SAM2_UNUSED sam2__log_write(int level, const char *file, int line, c
}
#endif

// Resolve hostname with DNS query and prioritize IPv6
#ifdef _WIN32
#define SAM2_SOCKET_ERROR (SOCKET_ERROR)
#define SAM2_SOCKET_INVALID (INVALID_SOCKET)
#define SAM2_CLOSESOCKET closesocket
#define SAM2_SOCKERRNO ((int)WSAGetLastError())
#define SAM2_EINPROGRESS WSAEWOULDBLOCK
#else
#include <unistd.h>
#define SAM2_SOCKET_ERROR (-1)
#define SAM2_SOCKET_INVALID (-1)
#define SAM2_CLOSESOCKET close
#define SAM2_SOCKERRNO errno
#define SAM2_EINPROGRESS EINPROGRESS
#endif

// Resolve hostname with DNS query
static int sam2__resolve_hostname(const char *hostname, char *ip) {
struct addrinfo hints, *res, *p, desired_address;
void *ptr;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
Expand All @@ -947,18 +961,12 @@ static int sam2__resolve_hostname(const char *hostname, char *ip) {
return -1;
}

for(p = res; p != NULL; p = p->ai_next) {
if (p->ai_family == AF_INET) {
ptr = &((struct sockaddr_in *) p->ai_addr)->sin_addr;
} else if (p->ai_family == AF_INET6) {
ptr = &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr;
} else {
continue;
}

for (p = res; p != NULL; p = p->ai_next) {
char ipvx[INET6_ADDRSTRLEN];
if (inet_ntop(p->ai_family, ptr, ipvx, INET6_ADDRSTRLEN) == NULL) {
SAM2_LOG_ERROR("Couldn't convert IP Address to string");
socklen_t addrlen = sizeof(struct sockaddr_storage);

if (getnameinfo(p->ai_addr, addrlen, ipvx, sizeof(ipvx), NULL, 0, NI_NUMERICHOST) != 0) {
SAM2_LOG_ERROR("Couldn't convert IP Address to string: ", SAM2_SOCKERRNO);
continue;
}

Expand All @@ -974,21 +982,6 @@ static int sam2__resolve_hostname(const char *hostname, char *ip) {
return desired_address.ai_family;
}

#ifdef _WIN32
#define SAM2_SOCKET_ERROR (SOCKET_ERROR)
#define SAM2_SOCKET_INVALID (INVALID_SOCKET)
#define SAM2_CLOSESOCKET closesocket
#define SAM2_SOCKERRNO ((int)WSAGetLastError())
#define SAM2_EINPROGRESS WSAEWOULDBLOCK
#else
#include <unistd.h>
#define SAM2_SOCKET_ERROR (-1)
#define SAM2_SOCKET_INVALID (-1)
#define SAM2_CLOSESOCKET close
#define SAM2_SOCKERRNO errno
#define SAM2_EINPROGRESS EINPROGRESS
#endif

SAM2_LINKAGE int sam2_client_connect(sam2_socket_t *sockfd_ptr, const char *host, int port) {
struct sockaddr_storage server_addr = {0};
// Initialize winsock / Increment winsock reference count
Expand Down

0 comments on commit 6e2fe1d

Please sign in to comment.