From 882cb6fdfb85d432332ac4f3309e3a948e7319f1 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Mon, 27 Apr 2020 23:52:39 +0100 Subject: [PATCH 1/2] Problem: static analysis shows dead store in testutil Solution: remove it and rework the ifdefs to be safe against uninitialised use --- tests/testutil.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/testutil.cpp b/tests/testutil.cpp index c602b0ca..d518ef1a 100644 --- a/tests/testutil.cpp +++ b/tests/testutil.cpp @@ -374,7 +374,7 @@ fd_t connect_socket (const char *endpoint_, const int af_, const int protocol_) { struct sockaddr_storage addr; // OSX is very opinionated and wants the size to match the AF family type - socklen_t addr_len = sizeof (addr); + socklen_t addr_len; const fd_t s_pre = socket (af_, SOCK_STREAM, protocol_); TEST_ASSERT_NOT_EQUAL (-1, s_pre); @@ -406,15 +406,16 @@ fd_t connect_socket (const char *endpoint_, const int af_, const int protocol_) memcpy (&addr, in->ai_addr, in->ai_addrlen); addr_len = (socklen_t) in->ai_addrlen; freeaddrinfo (in); - } + } else { #if defined(ZMQ_HAVE_IPC) - else { struct sockaddr_un *un_addr = (struct sockaddr_un *) &addr; addr_len = sizeof (struct sockaddr_un); un_addr->sun_family = AF_UNIX; strcpy (un_addr->sun_path, endpoint_); - } +#else + return retired_fd; #endif + } TEST_ASSERT_SUCCESS_RAW_ERRNO ( connect (s_pre, (struct sockaddr *) &addr, addr_len)); @@ -430,7 +431,7 @@ fd_t bind_socket_resolve_port (const char *address_, { struct sockaddr_storage addr; // OSX is very opinionated and wants the size to match the AF family type - socklen_t addr_len = sizeof (addr); + socklen_t addr_len; const fd_t s_pre = socket (af_, SOCK_STREAM, protocol_); TEST_ASSERT_NOT_EQUAL (-1, s_pre); @@ -460,9 +461,8 @@ fd_t bind_socket_resolve_port (const char *address_, memcpy (&addr, in->ai_addr, in->ai_addrlen); addr_len = (socklen_t) in->ai_addrlen; freeaddrinfo (in); - } + } else { #if defined(ZMQ_HAVE_IPC) - else { struct sockaddr_un *un_addr = (struct sockaddr_un *) &addr; addr_len = sizeof (struct sockaddr_un); un_addr->sun_family = AF_UNIX; @@ -490,8 +490,10 @@ fd_t bind_socket_resolve_port (const char *address_, // TODO check return value of unlink unlink (buffer); - } +#else + return retired_fd; #endif + } TEST_ASSERT_SUCCESS_RAW_ERRNO ( bind (s_pre, (struct sockaddr *) &addr, addr_len)); From a909e7296d5b2a15fcf80c5d021a06f3bc09d1a4 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Tue, 28 Apr 2020 00:06:04 +0100 Subject: [PATCH 2/2] Problem: testutil build broken with gcc 4.4 on CentOS 6 Solution: do not alias sockaddr_un, but take a copy tests/testutil.cpp: In function 'fd_t bind_socket_resolve_port(const char*, const char*, char*, int, int)': tests/testutil.cpp:468: error: dereferencing pointer 'un_addr' does break strict-aliasing rules --- tests/testutil.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/testutil.cpp b/tests/testutil.cpp index d518ef1a..4becce81 100644 --- a/tests/testutil.cpp +++ b/tests/testutil.cpp @@ -408,10 +408,10 @@ fd_t connect_socket (const char *endpoint_, const int af_, const int protocol_) freeaddrinfo (in); } else { #if defined(ZMQ_HAVE_IPC) - struct sockaddr_un *un_addr = (struct sockaddr_un *) &addr; + // Cannot cast addr as gcc 4.4 will fail with strict aliasing errors + (*(struct sockaddr_un *) &addr).sun_family = AF_UNIX; + strcpy ((*(struct sockaddr_un *) &addr).sun_path, endpoint_); addr_len = sizeof (struct sockaddr_un); - un_addr->sun_family = AF_UNIX; - strcpy (un_addr->sun_path, endpoint_); #else return retired_fd; #endif @@ -463,9 +463,9 @@ fd_t bind_socket_resolve_port (const char *address_, freeaddrinfo (in); } else { #if defined(ZMQ_HAVE_IPC) - struct sockaddr_un *un_addr = (struct sockaddr_un *) &addr; + // Cannot cast addr as gcc 4.4 will fail with strict aliasing errors + (*(struct sockaddr_un *) &addr).sun_family = AF_UNIX; addr_len = sizeof (struct sockaddr_un); - un_addr->sun_family = AF_UNIX; #if defined ZMQ_HAVE_WINDOWS char buffer[MAX_PATH] = ""; @@ -484,7 +484,7 @@ fd_t bind_socket_resolve_port (const char *address_, close (fd); #endif #endif - strcpy (un_addr->sun_path, buffer); + strcpy ((*(struct sockaddr_un *) &addr).sun_path, buffer); memcpy (my_endpoint_, "ipc://", 7); strcat (my_endpoint_, buffer); @@ -506,8 +506,8 @@ fd_t bind_socket_resolve_port (const char *address_, sprintf (my_endpoint_, "%s://%s:%u", protocol_ == IPPROTO_TCP ? "tcp" : "udp", address_, af_ == AF_INET - ? ntohs (((struct sockaddr_in *) &addr)->sin_port) - : ntohs (((struct sockaddr_in6 *) &addr)->sin6_port)); + ? ntohs ((*(struct sockaddr_in *) &addr).sin_port) + : ntohs ((*(struct sockaddr_in6 *) &addr).sin6_port)); } return s_pre;