Re: Crash on attempt to connect to nonstarted server

From: Bruce Momjian <bruce(at)momjian(dot)us>
To: Magnus Hagander <magnus(at)hagander(dot)net>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Crash on attempt to connect to nonstarted server
Date: 2010-12-18 16:26:39
Message-ID: 201012181626.oBIGQdE16865@momjian.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

bruce wrote:
> Magnus Hagander wrote:
> > I get a crash on win32 when connecting to a server that's not started.
> > In fe-connect.c, we have:
> >
> > display_host_addr = (conn->pghostaddr == NULL) &&
> > (strcmp(conn->pghost, host_addr) != 0);
> >
> > In my case, conn->pghost is NULL at this point, as is
> > conn->pghostaddr. Thus, it crashes in strcmp().
>
> I have researched this with Magnus, and was able to reproduce the
> failure. It happens only on Win32 because that is missing unix-domain
> sockets so "" maps to localhost, which is an IP address. I have applied
> the attached patch. The new output is:
>
> $ psql test
> psql: could not connect to server: Connection refused
> Is the server running on host "???" and accepting
> TCP/IP connections on port 5432?
>
> Note the "???". This happens because the mapping of "" to localhost
> happens below the libpq library variable level.

I made a mistake in the fix from yesterday. I added code to test for
NULL, but in fact what I should have done was to allow a NULL hostname
to trigger the printing of the IP address because it will never match an
IP number.

The attached applied, patch fixes this, and uses the same logic as in
connectDBStart() to print 'localhost' for connection failures. I also
added code to use DefaultHost consistently in that file. With the patch
the new Win32 output for a down server is:

$ pql test
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

$ psql -h "" test
psql: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?

I am amazed we were printing "???" all these years on this very popular
platform.

--
Bruce Momjian <bruce(at)momjian(dot)us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

Attachment Content-Type Size
/rtmp/diff text/x-diff 3.1 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Tomas Vondra 2010-12-18 16:59:36 Re: proposal : cross-column stats
Previous Message Florian Pflug 2010-12-18 15:59:11 Re: proposal : cross-column stats