Skip to content

Commit 2fc80b9

Browse files
committed
Add positive defense against trying to connect when the connection
option state hasn't been fully set up. This is possible via PQreset() and might occur in other code paths too, so a state flag seems the most robust solution. Per report from Arturs Zoldners.
1 parent 1cf13e6 commit 2fc80b9

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.325 2006/01/11 08:43:13 neilc Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.326 2006/02/13 22:33:57 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -505,6 +505,13 @@ connectOptions2(PGconn *conn)
505505
else
506506
conn->sslmode = strdup(DefaultSSLMode);
507507

508+
/*
509+
* Only if we get this far is it appropriate to try to connect.
510+
* (We need a state flag, rather than just the boolean result of
511+
* this function, in case someone tries to PQreset() the PGconn.)
512+
*/
513+
conn->options_valid = true;
514+
508515
return true;
509516
}
510517

@@ -729,6 +736,9 @@ connectDBStart(PGconn *conn)
729736
if (!conn)
730737
return 0;
731738

739+
if (!conn->options_valid)
740+
goto connect_errReturn;
741+
732742
/* Ensure our buffers are empty */
733743
conn->inStart = conn->inCursor = conn->inEnd = 0;
734744
conn->outCount = 0;
@@ -1814,6 +1824,8 @@ makeEmptyPGconn(void)
18141824
conn->status = CONNECTION_BAD;
18151825
conn->asyncStatus = PGASYNC_IDLE;
18161826
conn->xactStatus = PQTRANS_IDLE;
1827+
conn->options_valid = false;
1828+
conn->nonblocking = false;
18171829
conn->setenv_state = SETENV_STATE_IDLE;
18181830
conn->client_encoding = PG_SQL_ASCII;
18191831
conn->verbosity = PQERRORS_DEFAULT;
@@ -1838,7 +1850,6 @@ makeEmptyPGconn(void)
18381850
conn->inBuffer = (char *) malloc(conn->inBufSize);
18391851
conn->outBufSize = 16 * 1024;
18401852
conn->outBuffer = (char *) malloc(conn->outBufSize);
1841-
conn->nonblocking = FALSE;
18421853
initPQExpBuffer(&conn->errorMessage);
18431854
initPQExpBuffer(&conn->workBuffer);
18441855

src/interfaces/libpq/libpq-int.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.109 2005/11/22 18:17:33 momjian Exp $
15+
* $PostgreSQL: pgsql/src/interfaces/libpq/libpq-int.h,v 1.110 2006/02/13 22:33:57 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -276,9 +276,9 @@ struct pg_conn
276276
/* Status indicators */
277277
ConnStatusType status;
278278
PGAsyncStatusType asyncStatus;
279-
PGTransactionStatusType xactStatus;
280-
/* note: xactStatus never changes to ACTIVE */
279+
PGTransactionStatusType xactStatus; /* never changes to ACTIVE */
281280
PGQueryClass queryclass;
281+
bool options_valid; /* true if OK to attempt connection */
282282
bool nonblocking; /* whether this connection is using nonblock
283283
* sending semantics */
284284
char copy_is_binary; /* 1 = copy binary, 0 = copy text */

0 commit comments

Comments
 (0)