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

@ -6817,8 +6817,7 @@ run_inferior_command (char *cmd, int len)
#else /* !IN_PROCESS_AGENT */
#include <sys/socket.h>
#include <sys/un.h>
#include "gdb_socket.h"
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
@ -6837,7 +6836,7 @@ static int
init_named_socket (const char *name)
{
int result, fd;
struct sockaddr_un addr;
union gdb_sockaddr_u addr;
result = fd = socket (PF_UNIX, SOCK_STREAM, 0);
if (result == -1)
@ -6846,10 +6845,10 @@ init_named_socket (const char *name)
return -1;
}
addr.sun_family = AF_UNIX;
addr.sa_un.sun_family = AF_UNIX;
strncpy (addr.sun_path, name, UNIX_PATH_MAX);
addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
strncpy (addr.sa_un.sun_path, name, UNIX_PATH_MAX);
addr.sa_un.sun_path[UNIX_PATH_MAX - 1] = '\0';
result = access (name, F_OK);
if (result == 0)
@ -6865,7 +6864,7 @@ init_named_socket (const char *name)
warning ("socket %s already exists; overwriting", name);
}
result = bind (fd, (struct sockaddr *) &addr, sizeof (addr));
result = bind (fd, &addr.sa, sizeof (addr.sa_un));
if (result == -1)
{
warning ("bind failed: %s", strerror (errno));
@ -7164,17 +7163,17 @@ gdb_agent_helper_thread (void *arg)
while (1)
{
socklen_t tmp;
struct sockaddr_un sockaddr;
union gdb_sockaddr_u sockaddr;
int fd;
char buf[1];
int ret;
int stop_loop = 0;
tmp = sizeof (sockaddr);
tmp = sizeof (sockaddr.sa_un);
do
{
fd = accept (listen_fd, &sockaddr, &tmp);
fd = accept (listen_fd, &sockaddr.sa, &tmp);
}
/* It seems an ERESTARTSYS can escape out of accept. */
while (fd == -512 || (fd == -1 && errno == EINTR));