Skip to content

Commit 8bbfa16

Browse files
committed
Don't assume PQdb() will return a valid result from a failed connection.
1 parent bbd1e1c commit 8bbfa16

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.164 2004/09/26 22:51:49 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.165 2004/10/01 17:34:17 tgl Exp $
33
-->
44

55
<chapter id="libpq">
@@ -3979,8 +3979,8 @@ main(int argc, char **argv)
39793979
/* Check to see that the backend connection was successfully made */
39803980
if (PQstatus(conn) != CONNECTION_OK)
39813981
{
3982-
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
3983-
fprintf(stderr, "%s", PQerrorMessage(conn));
3982+
fprintf(stderr, "Connection to database failed: %s",
3983+
PQerrorMessage(conn));
39843984
exit_nicely(conn);
39853985
}
39863986

@@ -4125,8 +4125,8 @@ main(int argc, char **argv)
41254125
/* Check to see that the backend connection was successfully made */
41264126
if (PQstatus(conn) != CONNECTION_OK)
41274127
{
4128-
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
4129-
fprintf(stderr, "%s", PQerrorMessage(conn));
4128+
fprintf(stderr, "Connection to database failed: %s",
4129+
PQerrorMessage(conn));
41304130
exit_nicely(conn);
41314131
}
41324132

@@ -4267,8 +4267,8 @@ main(int argc, char **argv)
42674267
/* Check to see that the backend connection was successfully made */
42684268
if (PQstatus(conn) != CONNECTION_OK)
42694269
{
4270-
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
4271-
fprintf(stderr, "%s", PQerrorMessage(conn));
4270+
fprintf(stderr, "Connection to database failed: %s",
4271+
PQerrorMessage(conn));
42724272
exit_nicely(conn);
42734273
}
42744274

src/test/examples/testlibpq.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ main(int argc, char **argv)
4141
/* Check to see that the backend connection was successfully made */
4242
if (PQstatus(conn) != CONNECTION_OK)
4343
{
44-
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
45-
fprintf(stderr, "%s", PQerrorMessage(conn));
44+
fprintf(stderr, "Connection to database failed: %s",
45+
PQerrorMessage(conn));
4646
exit_nicely(conn);
4747
}
4848

src/test/examples/testlibpq2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ main(int argc, char **argv)
6161
/* Check to see that the backend connection was successfully made */
6262
if (PQstatus(conn) != CONNECTION_OK)
6363
{
64-
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
65-
fprintf(stderr, "%s", PQerrorMessage(conn));
64+
fprintf(stderr, "Connection to database failed: %s",
65+
PQerrorMessage(conn));
6666
exit_nicely(conn);
6767
}
6868

src/test/examples/testlibpq3.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ main(int argc, char **argv)
6666
/* Check to see that the backend connection was successfully made */
6767
if (PQstatus(conn) != CONNECTION_OK)
6868
{
69-
fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn));
70-
fprintf(stderr, "%s", PQerrorMessage(conn));
69+
fprintf(stderr, "Connection to database failed: %s",
70+
PQerrorMessage(conn));
7171
exit_nicely(conn);
7272
}
7373

src/test/examples/testlibpq4.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ static void
2222
check_conn(PGconn *conn, const char *dbName)
2323
{
2424
/* check to see that the backend connection was successfully made */
25-
if (PQstatus(conn) == CONNECTION_BAD)
25+
if (PQstatus(conn) != CONNECTION_OK)
2626
{
27-
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
28-
fprintf(stderr, "%s", PQerrorMessage(conn));
27+
fprintf(stderr, "Connection to database \"%s\" failed: %s",
28+
dbName, PQerrorMessage(conn));
2929
exit(1);
3030
}
3131
}

src/test/examples/testlo.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/test/examples/testlo.c,v 1.23 2004/09/22 05:12:45 neilc Exp $
11+
* $PostgreSQL: pgsql/src/test/examples/testlo.c,v 1.24 2004/10/01 17:34:19 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -225,10 +225,10 @@ main(int argc, char **argv)
225225
conn = PQsetdb(NULL, NULL, NULL, NULL, database);
226226

227227
/* check to see that the backend connection was successfully made */
228-
if (PQstatus(conn) == CONNECTION_BAD)
228+
if (PQstatus(conn) != CONNECTION_OK)
229229
{
230-
fprintf(stderr, "Connection to database '%s' failed.\n", database);
231-
fprintf(stderr, "%s", PQerrorMessage(conn));
230+
fprintf(stderr, "Connection to database failed: %s",
231+
PQerrorMessage(conn));
232232
exit_nicely(conn);
233233
}
234234

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