This is the mail archive of the cygwin-patches@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: connect patch


On Wed, Feb 06, 2002 at 01:07:28PM -0500, Jason Tishler wrote:
> The attached patch fixes a SEGV when getsockname () is called.  This
> problem can be tickled by the PostgreSQL 7.2 version of psql:
> 
>     http://archives.postgresql.org/pgsql-cygwin/2002-02/msg00012.php
> 
> Note that I essentially plagiarized the following commit:
> 
>     http://cygwin.com/ml/cygwin-cvs/2002-q1/msg00028.html
> 
> Was this the right thing to do?

The patch isn't correct since it now calls fdsock() twice which allocates
a new fhandler even if the line before already had created one.

Better:

  fhandler_socket* res_fh = fdsock (fd, name, soc)->set_addr_family (af);
  if (af == AF_LOCAL)
    res_fh->set_sun_path (name);

However, I don't understand the need for that patch.  Does postgresql
call getsockname() before calling bind()? I didn't know that that makes
sense.  Sure, it shouldn't SEGV but the returned name doesn't make
sense on non-Cygwin systems either.

A quick test on Linux returns:

[~]$ ./uds /tmp/mysocket
Before bind(): name = M`@(R`@b, returned len = 2
After bind() : name = /tmp/mysocket, returned len = 16

So, IMO, the correct way is to clean up cygwin_getsockname()
so that it always returns "something" instead of SEGVing.

Could you please test the below patch if that works with postgresql?

Thanks,
Corinna


	* net.cc (cygwin_getsockname): Fix handling of NULL sun_path.


Index: net.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/net.cc,v
retrieving revision 1.99
diff -u -p -r1.99 net.cc
--- net.cc	2002/01/29 13:39:41	1.99
+++ net.cc	2002/02/07 13:53:11
@@ -1375,12 +1375,17 @@ cygwin_getsockname (int fd, struct socka
 	  struct sockaddr_un *sun = (struct sockaddr_un *) addr;
 	  memset (sun, 0, *namelen);
 	  sun->sun_family = AF_LOCAL;
-	  /* According to SUSv2 "If the actual length of the address is greater
-	     than the length of the supplied sockaddr structure, the stored
-	     address will be truncated."  We play it save here so that the
-	     path always has a trailing 0 even if it's truncated. */
-	  strncpy (sun->sun_path, sock->get_sun_path (),
-		   *namelen - sizeof *sun + sizeof sun->sun_path - 1);
+
+	  if (!sock->get_sun_path ())
+	    sun->sun_path[0] = '\0';
+	  else
+	    /* According to SUSv2 "If the actual length of the address is
+	       greater than the length of the supplied sockaddr structure, the
+	       stored address will be truncated."  We play it save here so
+	       that the path always has a trailing 0 even if it's truncated. */
+	    strncpy (sun->sun_path, sock->get_sun_path (),
+		     *namelen - sizeof *sun + sizeof sun->sun_path - 1);
+
 	  *namelen = sizeof *sun - sizeof sun->sun_path
 		     + strlen (sun->sun_path) + 1;
 	  res = 0;

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                                mailto:cygwin@cygwin.com
Red Hat, Inc.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]