Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5037ed8

Browse files
committed
Fix up closePGconn() so that PQreset() will work on GSSAPI/SSPI connections;
the patch for those features put its cleanup code into freePGconn() which is really the wrong place. Remove redundant code from freePGconn() and add comments in hopes of preventing similar mistakes in future. Noticed while trying (futilely) to reproduce bug #3902.
1 parent fad2b99 commit 5037ed8

File tree

1 file changed

+50
-73
lines changed

1 file changed

+50
-73
lines changed

src/interfaces/libpq/fe-connect.c

+50-73
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.355 2008/01/01 19:46:00 momjian Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.356 2008/01/29 02:06:30 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1963,25 +1963,16 @@ makeEmptyPGconn(void)
19631963

19641964
/*
19651965
* freePGconn
1966-
* - free the PGconn data structure
1966+
* - free an idle (closed) PGconn data structure
19671967
*
1968-
* When changing/adding to this function, see also closePGconn!
1968+
* NOTE: this should not overlap any functionality with closePGconn().
1969+
* Clearing/resetting of transient state belongs there; what we do here is
1970+
* release data that is to be held for the life of the PGconn structure.
1971+
* If a value ought to be cleared/freed during PQreset(), do it there not here.
19691972
*/
19701973
static void
19711974
freePGconn(PGconn *conn)
19721975
{
1973-
PGnotify *notify;
1974-
pgParameterStatus *pstatus;
1975-
1976-
if (!conn)
1977-
return;
1978-
1979-
pqClearAsyncResult(conn); /* deallocate result and curTuple */
1980-
if (conn->sock >= 0)
1981-
{
1982-
pqsecure_close(conn);
1983-
closesocket(conn->sock);
1984-
}
19851976
if (conn->pghost)
19861977
free(conn->pghost);
19871978
if (conn->pghostaddr)
@@ -2011,65 +2002,13 @@ freePGconn(PGconn *conn)
20112002
/* Note that conn->Pfdebug is not ours to close or free */
20122003
if (conn->last_query)
20132004
free(conn->last_query);
2014-
pg_freeaddrinfo_all(conn->addrlist_family, conn->addrlist);
2015-
notify = conn->notifyHead;
2016-
while (notify != NULL)
2017-
{
2018-
PGnotify *prev = notify;
2019-
2020-
notify = notify->next;
2021-
free(prev);
2022-
}
2023-
#ifdef ENABLE_GSS
2024-
{
2025-
OM_uint32 min_s;
2026-
2027-
if (conn->gctx)
2028-
gss_delete_sec_context(&min_s, &conn->gctx, GSS_C_NO_BUFFER);
2029-
if (conn->gtarg_nam)
2030-
gss_release_name(&min_s, &conn->gtarg_nam);
2031-
if (conn->ginbuf.length)
2032-
gss_release_buffer(&min_s, &conn->ginbuf);
2033-
if (conn->goutbuf.length)
2034-
gss_release_buffer(&min_s, &conn->goutbuf);
2035-
}
2036-
#endif
2037-
#ifdef ENABLE_SSPI
2038-
{
2039-
if (conn->ginbuf.length)
2040-
free(conn->ginbuf.value);
2041-
2042-
if (conn->sspitarget)
2043-
free(conn->sspitarget);
2044-
2045-
if (conn->sspicred)
2046-
{
2047-
FreeCredentialsHandle(conn->sspicred);
2048-
free(conn->sspicred);
2049-
}
2050-
if (conn->sspictx)
2051-
{
2052-
DeleteSecurityContext(conn->sspictx);
2053-
free(conn->sspictx);
2054-
}
2055-
}
2056-
#endif
2057-
pstatus = conn->pstatus;
2058-
while (pstatus != NULL)
2059-
{
2060-
pgParameterStatus *prev = pstatus;
2061-
2062-
pstatus = pstatus->next;
2063-
free(prev);
2064-
}
2065-
if (conn->lobjfuncs)
2066-
free(conn->lobjfuncs);
20672005
if (conn->inBuffer)
20682006
free(conn->inBuffer);
20692007
if (conn->outBuffer)
20702008
free(conn->outBuffer);
20712009
termPQExpBuffer(&conn->errorMessage);
20722010
termPQExpBuffer(&conn->workBuffer);
2011+
20732012
free(conn);
20742013

20752014
#ifdef WIN32
@@ -2081,7 +2020,9 @@ freePGconn(PGconn *conn)
20812020
* closePGconn
20822021
* - properly close a connection to the backend
20832022
*
2084-
* Release all transient state, but NOT the connection parameters.
2023+
* This should reset or release all transient state, but NOT the connection
2024+
* parameters. On exit, the PGconn should be in condition to start a fresh
2025+
* connection with the same parameters (see PQreset()).
20852026
*/
20862027
static void
20872028
closePGconn(PGconn *conn)
@@ -2105,9 +2046,10 @@ closePGconn(PGconn *conn)
21052046
}
21062047

21072048
/*
2108-
* must reset the blocking status so a possible reconnect will work don't
2109-
* call PQsetnonblocking() because it will fail if it's unable to flush
2110-
* the connection.
2049+
* Must reset the blocking status so a possible reconnect will work.
2050+
*
2051+
* Don't call PQsetnonblocking() because it will fail if it's unable to
2052+
* flush the connection.
21112053
*/
21122054
conn->nonblocking = FALSE;
21132055

@@ -2135,7 +2077,7 @@ closePGconn(PGconn *conn)
21352077
notify = notify->next;
21362078
free(prev);
21372079
}
2138-
conn->notifyHead = NULL;
2080+
conn->notifyHead = conn->notifyTail = NULL;
21392081
pstatus = conn->pstatus;
21402082
while (pstatus != NULL)
21412083
{
@@ -2150,6 +2092,41 @@ closePGconn(PGconn *conn)
21502092
conn->lobjfuncs = NULL;
21512093
conn->inStart = conn->inCursor = conn->inEnd = 0;
21522094
conn->outCount = 0;
2095+
#ifdef ENABLE_GSS
2096+
{
2097+
OM_uint32 min_s;
2098+
2099+
if (conn->gctx)
2100+
gss_delete_sec_context(&min_s, &conn->gctx, GSS_C_NO_BUFFER);
2101+
if (conn->gtarg_nam)
2102+
gss_release_name(&min_s, &conn->gtarg_nam);
2103+
if (conn->ginbuf.length)
2104+
gss_release_buffer(&min_s, &conn->ginbuf);
2105+
if (conn->goutbuf.length)
2106+
gss_release_buffer(&min_s, &conn->goutbuf);
2107+
}
2108+
#endif
2109+
#ifdef ENABLE_SSPI
2110+
if (conn->ginbuf.length)
2111+
free(conn->ginbuf.value);
2112+
conn->ginbuf.length = 0;
2113+
conn->ginbuf.value = NULL;
2114+
if (conn->sspitarget)
2115+
free(conn->sspitarget);
2116+
conn->sspitarget = NULL;
2117+
if (conn->sspicred)
2118+
{
2119+
FreeCredentialsHandle(conn->sspicred);
2120+
free(conn->sspicred);
2121+
conn->sspicred = NULL;
2122+
}
2123+
if (conn->sspictx)
2124+
{
2125+
DeleteSecurityContext(conn->sspictx);
2126+
free(conn->sspictx);
2127+
conn->sspictx = NULL;
2128+
}
2129+
#endif
21532130
}
21542131

21552132
/*

0 commit comments

Comments
 (0)