Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations

Building gdbserver in C++ mode shows:

  gdb/gdbserver/tracepoint.c: In function ‘void* gdb_agent_helper_thread(void*)’:
  gdb/gdbserver/tracepoint.c:7190:47: error: cannot convert ‘sockaddr_un*’ to ‘sockaddr*’ for argument ‘2’ to ‘int accept(int, sockaddr*, socklen_t*)’
	  fd = accept (listen_fd, &sockaddr, &tmp);

A few places in the tree already have an explicit cast to struct
sockaddr *, but that's a strict aliasing violation.  Instead of
propagating invalid code, fix this by using a union instead.

gdb/ChangeLog:
2015-03-07  Pedro Alves  <palves@redhat.com>

	* common/gdb_socket.h: New file.
	* ser-tcp.c: Include gdb_socket.h.  Don't include netinet/in.h nor
	sys/socket.h.
	(net_open): Use union gdb_sockaddr_u.

gdb/gdbserver/ChangeLog:
2015-03-07  Pedro Alves  <palves@redhat.com>

	* gdbreplay.c: No longer include <netinet/in.h>, <sys/socket.h>,
	or <winsock2.h> here.  Instead include "gdb_socket.h".
	(remote_open): Use union gdb_sockaddr_u.
	* remote-utils.c: No longer include <netinet/in.h>, <sys/socket.h>
	or <winsock2.h> here.  Instead include "gdb_socket.h".
	(handle_accept_event, remote_prepare): Use union gdb_sockaddr_u.
	* tracepoint.c: Include "gdb_socket.h" instead of <sys/socket.h>
	or <sys/un.h>.
	(init_named_socket, gdb_agent_helper_thread): Use union
	gdb_sockaddr_u.
This commit is contained in:
Pedro Alves
2015-03-07 17:30:46 +00:00
parent 72df25b28d
commit 366c75fc91
7 changed files with 97 additions and 52 deletions

View File

@ -37,6 +37,8 @@
#include <sys/time.h>
#include "gdb_socket.h"
#ifdef USE_WIN32API
#include <winsock2.h>
#ifndef ETIMEDOUT
@ -45,10 +47,8 @@
#define close(fd) closesocket (fd)
#define ioctl ioctlsocket
#else
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#endif
@ -159,7 +159,7 @@ net_open (struct serial *scb, const char *name)
int n, port, tmp;
int use_udp;
struct hostent *hostent;
struct sockaddr_in sockaddr;
union gdb_sockaddr_u sockaddr;
#ifdef USE_WIN32API
u_long ioarg;
#else
@ -199,9 +199,9 @@ net_open (struct serial *scb, const char *name)
return -1;
}
sockaddr.sin_family = PF_INET;
sockaddr.sin_port = htons (port);
memcpy (&sockaddr.sin_addr.s_addr, hostent->h_addr,
sockaddr.sa_in.sin_family = PF_INET;
sockaddr.sa_in.sin_port = htons (port);
memcpy (&sockaddr.sa_in.sin_addr.s_addr, hostent->h_addr,
sizeof (struct in_addr));
retry:
@ -220,7 +220,7 @@ net_open (struct serial *scb, const char *name)
/* Use Non-blocking connect. connect() will return 0 if connected
already. */
n = connect (scb->fd, (struct sockaddr *) &sockaddr, sizeof (sockaddr));
n = connect (scb->fd, &sockaddr.sa, sizeof (sockaddr.sa_in));
if (n < 0)
{