From dc3928fc75550422306f9630d34430634767d87a Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Thu, 1 Mar 2018 18:14:23 +0100 Subject: [PATCH] Cygwin: convert sun_name_t into class Add constructors and new/delete operators to make sure sun_name_t objects are allocated on the cygheap. Signed-off-by: Corinna Vinschen --- winsup/cygwin/fhandler.h | 19 ++++++++++++++++++- winsup/cygwin/fhandler_socket_unix.cc | 16 ++++------------ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 1d4e681c4..cfd93d2c8 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -817,8 +817,9 @@ class fhandler_socket_local: public fhandler_socket_wsock } }; -struct sun_name_t +class sun_name_t { + public: __socklen_t un_len; union { @@ -826,6 +827,22 @@ struct sun_name_t /* Allows 108 bytes sun_path plus trailing NUL */ char _nul[sizeof (struct sockaddr_un) + 1]; }; + sun_name_t () + { + un_len = 0; + un.sun_family = 0; + _nul[sizeof (struct sockaddr_un)] = '\0'; + } + sun_name_t (const struct sockaddr *name, __socklen_t namelen) + { + un_len = namelen < (__socklen_t) sizeof un ? namelen : sizeof un; + memcpy (&un, name, un_len); + _nul[sizeof (struct sockaddr_un)] = '\0'; + } + + void *operator new (size_t) __attribute__ ((nothrow)) + { return cmalloc_abort (HEAP_FHANDLER, sizeof (sun_name_t)); } + void operator delete (void *p) {cfree (p);} }; class fhandler_socket_unix : public fhandler_socket diff --git a/winsup/cygwin/fhandler_socket_unix.cc b/winsup/cygwin/fhandler_socket_unix.cc index 1d8c4c4cd..506befc51 100644 --- a/winsup/cygwin/fhandler_socket_unix.cc +++ b/winsup/cygwin/fhandler_socket_unix.cc @@ -158,9 +158,9 @@ fhandler_socket_unix::fhandler_socket_unix () : fhandler_socket_unix::~fhandler_socket_unix () { if (sun_path) - cfree (sun_path); + delete sun_path; if (peer_sun_path) - cfree (peer_sun_path); + delete peer_sun_path; } void @@ -168,11 +168,7 @@ fhandler_socket_unix::set_sun_path (struct sockaddr_un *un, socklen_t unlen) { if (!un) sun_path = NULL; - sun_path = (struct sun_name_t *) cmalloc_abort (HEAP_FHANDLER, - sizeof *sun_path); - sun_path->un_len = unlen; - memcpy (&sun_path->un, un, sizeof (*un)); - sun_path->_nul[sizeof (struct sockaddr_un)] = '\0'; + sun_path = new sun_name_t ((const struct sockaddr *) un, unlen); } void @@ -181,11 +177,7 @@ fhandler_socket_unix::set_peer_sun_path (struct sockaddr_un *un, { if (!un) peer_sun_path = NULL; - peer_sun_path = (struct sun_name_t *) cmalloc_abort (HEAP_FHANDLER, - sizeof *peer_sun_path); - peer_sun_path->un_len = unlen; - memcpy (&peer_sun_path->un, un, sizeof (*un)); - peer_sun_path->_nul[sizeof (struct sockaddr_un)] = '\0'; + peer_sun_path = new sun_name_t ((const struct sockaddr *) un, unlen); } void