Skip to content

Commit aa158a7

Browse files
committed
Fix memory leak from Tom Lane.
1 parent 3ac9688 commit aa158a7

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.82 1998/09/18 16:46:05 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.83 1998/09/20 04:51:10 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1316,9 +1316,8 @@ conninfo_parse(const char *conninfo, char *errorMessage)
13161316
*/
13171317
if (!strcmp(option->keyword, "user"))
13181318
{
1319-
tmp = fe_getauthname(errortmp);
1320-
if (tmp)
1321-
option->val = strdup(tmp);
1319+
option->val = fe_getauthname(errortmp);
1320+
continue;
13221321
}
13231322

13241323
/* ----------
@@ -1330,6 +1329,7 @@ conninfo_parse(const char *conninfo, char *errorMessage)
13301329
tmp = conninfo_getval("user");
13311330
if (tmp)
13321331
option->val = strdup(tmp);
1332+
continue;
13331333
}
13341334
}
13351335

src/interfaces/libpq/fe-misc.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
*
2626
* IDENTIFICATION
27-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.21 1998/09/03 02:10:50 momjian Exp $
27+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v 1.22 1998/09/20 04:51:12 momjian Exp $
2828
*
2929
*-------------------------------------------------------------------------
3030
*/
@@ -366,6 +366,11 @@ pqReadData(PGconn *conn)
366366
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
367367
if (errno == EWOULDBLOCK)
368368
return 0;
369+
#endif
370+
/* We might get ECONNRESET here if using TCP and backend died */
371+
#ifdef ECONNRESET
372+
if (errno == ECONNRESET)
373+
goto definitelyFailed;
369374
#endif
370375
sprintf(conn->errorMessage,
371376
"pqReadData() -- read() failed: errno=%d\n%s\n",
@@ -409,6 +414,11 @@ pqReadData(PGconn *conn)
409414
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
410415
if (errno == EWOULDBLOCK)
411416
return 0;
417+
#endif
418+
/* We might get ECONNRESET here if using TCP and backend died */
419+
#ifdef ECONNRESET
420+
if (errno == ECONNRESET)
421+
goto definitelyFailed;
412422
#endif
413423
sprintf(conn->errorMessage,
414424
"pqReadData() -- read() failed: errno=%d\n%s\n",
@@ -425,6 +435,7 @@ pqReadData(PGconn *conn)
425435
* OK, we are getting a zero read even though select() says ready.
426436
* This means the connection has been closed. Cope.
427437
*/
438+
definitelyFailed:
428439
sprintf(conn->errorMessage,
429440
"pqReadData() -- backend closed the channel unexpectedly.\n"
430441
"\tThis probably means the backend terminated abnormally"
@@ -460,7 +471,6 @@ pqFlush(PGconn *conn)
460471
/* Prevent being SIGPIPEd if backend has closed the connection. */
461472
#ifndef WIN32
462473
pqsigfunc oldsighandler = pqsignal(SIGPIPE, SIG_IGN);
463-
464474
#endif
465475

466476
int sent = send(conn->sock, ptr, len, 0);
@@ -471,7 +481,11 @@ pqFlush(PGconn *conn)
471481

472482
if (sent < 0)
473483
{
474-
/* Anything except EAGAIN or EWOULDBLOCK is trouble */
484+
/*
485+
* Anything except EAGAIN or EWOULDBLOCK is trouble.
486+
* If it's EPIPE or ECONNRESET, assume we've lost the
487+
* backend connection permanently.
488+
*/
475489
switch (errno)
476490
{
477491
#ifdef EAGAIN
@@ -482,10 +496,27 @@ pqFlush(PGconn *conn)
482496
case EWOULDBLOCK:
483497
break;
484498
#endif
499+
case EPIPE:
500+
#ifdef ECONNRESET
501+
case ECONNRESET:
502+
#endif
503+
sprintf(conn->errorMessage,
504+
"pqFlush() -- backend closed the channel unexpectedly.\n"
505+
"\tThis probably means the backend terminated abnormally"
506+
" before or while processing the request.\n");
507+
conn->status = CONNECTION_BAD; /* No more connection */
508+
#ifdef WIN32
509+
closesocket(conn->sock);
510+
#else
511+
close(conn->sock);
512+
#endif
513+
conn->sock = -1;
514+
return EOF;
485515
default:
486516
sprintf(conn->errorMessage,
487-
"pqFlush() -- couldn't send data: errno=%d\n%s\n",
517+
"pqFlush() -- couldn't send data: errno=%d\n%s\n",
488518
errno, strerror(errno));
519+
/* We don't assume it's a fatal error... */
489520
return EOF;
490521
}
491522
}

0 commit comments

Comments
 (0)