Skip to content

Commit 76b11e8

Browse files
committed
Give a better error message on invalid hostaddr option.
If you accidentally pass a host name in the hostaddr option, e.g. hostaddr=localhost, you get an error like: psql: could not translate host name "localhost" to address: Name or service not known That's a bit confusing, because it implies that we tried to look up "localhost" in DNS, but it failed. To make it more clear that we tried to parse "localhost" as a numeric network address, change the message to: psql: could not parse network address "localhost": Name or service not known Discussion: https://www.postgresql.org/message-id/10badbc6-4d5a-a769-623a-f7ada43e14dd@iki.fi
1 parent 67d370e commit 76b11e8

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,6 @@ connectDBStart(PGconn *conn)
16421642
for (i = 0; i < conn->nconnhost; ++i)
16431643
{
16441644
pg_conn_host *ch = &conn->connhost[i];
1645-
char *node = ch->host;
16461645
struct addrinfo hint;
16471646
int thisport;
16481647

@@ -1668,17 +1667,28 @@ connectDBStart(PGconn *conn)
16681667
}
16691668
snprintf(portstr, sizeof(portstr), "%d", thisport);
16701669

1671-
/* Set up for name resolution. */
1670+
/* Use pg_getaddrinfo_all() to resolve the address */
16721671
switch (ch->type)
16731672
{
16741673
case CHT_HOST_NAME:
1674+
ret = pg_getaddrinfo_all(ch->host, portstr, &hint, &ch->addrlist);
1675+
if (ret || !ch->addrlist)
1676+
appendPQExpBuffer(&conn->errorMessage,
1677+
libpq_gettext("could not translate host name \"%s\" to address: %s\n"),
1678+
ch->host, gai_strerror(ret));
16751679
break;
1680+
16761681
case CHT_HOST_ADDRESS:
16771682
hint.ai_flags = AI_NUMERICHOST;
1683+
ret = pg_getaddrinfo_all(ch->host, portstr, &hint, &ch->addrlist);
1684+
if (ret || !ch->addrlist)
1685+
appendPQExpBuffer(&conn->errorMessage,
1686+
libpq_gettext("could not parse network address \"%s\": %s\n"),
1687+
ch->host, gai_strerror(ret));
16781688
break;
1689+
16791690
case CHT_UNIX_SOCKET:
16801691
#ifdef HAVE_UNIX_SOCKETS
1681-
node = NULL;
16821692
hint.ai_family = AF_UNIX;
16831693
UNIXSOCK_PATH(portstr, thisport, ch->host);
16841694
if (strlen(portstr) >= UNIXSOCK_PATH_BUFLEN)
@@ -1690,24 +1700,25 @@ connectDBStart(PGconn *conn)
16901700
conn->options_valid = false;
16911701
goto connect_errReturn;
16921702
}
1703+
1704+
/*
1705+
* NULL hostname tells pg_getaddrinfo_all to parse the service
1706+
* name as a Unix-domain socket path.
1707+
*/
1708+
ret = pg_getaddrinfo_all(NULL, portstr, &hint, &ch->addrlist);
1709+
if (ret || !ch->addrlist)
1710+
appendPQExpBuffer(&conn->errorMessage,
1711+
libpq_gettext("could not translate Unix-domain socket path \"%s\" to address: %s\n"),
1712+
portstr, gai_strerror(ret));
1713+
break;
16931714
#else
16941715
Assert(false);
1716+
conn->options_valid = false;
1717+
goto connect_errReturn;
16951718
#endif
1696-
break;
16971719
}
1698-
1699-
/* Use pg_getaddrinfo_all() to resolve the address */
1700-
ret = pg_getaddrinfo_all(node, portstr, &hint, &ch->addrlist);
17011720
if (ret || !ch->addrlist)
17021721
{
1703-
if (node)
1704-
appendPQExpBuffer(&conn->errorMessage,
1705-
libpq_gettext("could not translate host name \"%s\" to address: %s\n"),
1706-
node, gai_strerror(ret));
1707-
else
1708-
appendPQExpBuffer(&conn->errorMessage,
1709-
libpq_gettext("could not translate Unix-domain socket path \"%s\" to address: %s\n"),
1710-
portstr, gai_strerror(ret));
17111722
if (ch->addrlist)
17121723
{
17131724
pg_freeaddrinfo_all(hint.ai_family, ch->addrlist);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy