From 366c75fc9183e46fe151aefb40f2d55a17815cb7 Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Sat, 7 Mar 2015 17:30:46 +0000 Subject: Fix struct sockaddr/sockaddr_in/sockaddr_un strict aliasing violations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 * 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 * gdbreplay.c: No longer include , , or here. Instead include "gdb_socket.h". (remote_open): Use union gdb_sockaddr_u. * remote-utils.c: No longer include , or here. Instead include "gdb_socket.h". (handle_accept_event, remote_prepare): Use union gdb_sockaddr_u. * tracepoint.c: Include "gdb_socket.h" instead of or . (init_named_socket, gdb_agent_helper_thread): Use union gdb_sockaddr_u. --- gdb/common/gdb_socket.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 gdb/common/gdb_socket.h (limited to 'gdb/common/gdb_socket.h') diff --git a/gdb/common/gdb_socket.h b/gdb/common/gdb_socket.h new file mode 100644 index 0000000..a670f74 --- /dev/null +++ b/gdb/common/gdb_socket.h @@ -0,0 +1,43 @@ +/* Copyright (C) 2015 Free Software Foundation, Inc. + + This file is part of GDB. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#ifndef GDB_SOCKET_H +#define GDB_SOCKET_H + +#if USE_WIN32API +#include +#else +#include +#include +#if HAVE_SYS_UN_H +#include +#endif +#endif + +/* Use this union instead of casts between struct sockaddr <-> struct + sockaddr_foo to avoid strict aliasing violations. */ + +union gdb_sockaddr_u +{ + struct sockaddr sa; + struct sockaddr_in sa_in; +#if HAVE_SYS_UN_H + struct sockaddr_un sa_un; +#endif +}; + +#endif /* GDB_SOCKET_H */ -- cgit v1.1