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

Commit 8d1f52e

Browse files
committed
From: Igor <igor@sba.miami.edu>
Subject: [PATCHES] More psql and libpq patches Well..these would be the last patches until the release (I hope)... I ran the regression tests while watching psql under purify, and it did not leak even one byte. In this patch: * Plugged a major leak when PSQL reads files for input (either through \i options or through -f option) * Fixed the one remaining leak in PSQL in not clearing PGresult *results everywhere it is supposed to. (Thanks Tymm) * Fixed A small leak in PSQL not clearing all the PGsettings correctly. * A not-so-obvious (but small) leak in Libpq when PQsetdb fails for any reason. * Added \n to some Libpq error messages to make them easier to digest.. * Finally, added /* PURIFY */ comment to some of the code indicating the reason for why it was added/changed...for future developers.
1 parent 442306f commit 8d1f52e

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/bin/psql/psql.c

+17-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.70 1997/06/03 06:17:34 vadim Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.71 1997/06/06 01:41:24 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -184,7 +184,8 @@ slashUsage(PsqlSettings * ps)
184184
static PGresult *
185185
PSQLexec(PsqlSettings * ps, char *query)
186186
{
187-
PGresult *res = PQexec(ps->db, query);
187+
PGresult *res;
188+
res = PQexec(ps->db, query);
188189
if (!res)
189190
fputs(PQerrorMessage(ps->db), stderr);
190191
else {
@@ -259,7 +260,6 @@ tableList(PsqlSettings * ps, bool deep_tablelist, char table_index_both)
259260
strcat(listbuf, " ORDER BY relname ");
260261
if (!(res = PSQLexec(ps, listbuf)))
261262
return -1;
262-
263263
/* first, print out the attribute names */
264264
nColumns = PQntuples(res);
265265
if (nColumns > 0) {
@@ -278,7 +278,7 @@ tableList(PsqlSettings * ps, bool deep_tablelist, char table_index_both)
278278
strcpy(table[i], PQgetvalue(res, i, 1));
279279
}
280280

281-
PQclear(res);
281+
PQclear(res); /* PURIFY */
282282
for (i = 0; i < nColumns; i++) {
283283
tableDesc(ps, table[i]);
284284
}
@@ -309,6 +309,7 @@ tableList(PsqlSettings * ps, bool deep_tablelist, char table_index_both)
309309
return (0);
310310

311311
} else {
312+
PQclear(res); /* PURIFY */
312313
switch (table_index_both) {
313314
case 't': fprintf(stderr, "Couldn't find any tables!\n");
314315
break;
@@ -507,7 +508,10 @@ gets_fromFile(char *prompt, FILE * source)
507508

508509
/* read up to MAX_QUERY_BUFFER characters */
509510
if (fgets(line, MAX_QUERY_BUFFER, source) == NULL)
511+
{
512+
free(line);
510513
return NULL;
514+
}
511515

512516
line[MAX_QUERY_BUFFER - 1] = '\0';
513517
len = strlen(line);
@@ -579,17 +583,14 @@ SendQuery(bool * success_p, PsqlSettings * settings, const char *query,
579583
&(settings->opt));
580584
fflush(settings->queryFout);
581585
}
582-
PQclear(results);
583586
break;
584587
case PGRES_EMPTY_QUERY:
585588
*success_p = true;
586-
PQclear(results);
587589
break;
588590
case PGRES_COMMAND_OK:
589591
*success_p = true;
590592
if (!settings->quiet)
591593
fprintf(stdout, "%s\n", PQcmdStatus(results));
592-
PQclear(results);
593594
break;
594595
case PGRES_COPY_OUT:
595596
*success_p = true;
@@ -601,15 +602,13 @@ SendQuery(bool * success_p, PsqlSettings * settings, const char *query,
601602

602603
handleCopyOut(results, settings->quiet, stdout);
603604
}
604-
PQclear(results);
605605
break;
606606
case PGRES_COPY_IN:
607607
*success_p = true;
608608
if (copy_in)
609609
handleCopyIn(results, false, copystream);
610610
else
611611
handleCopyIn(results, !settings->quiet, stdin);
612-
PQclear(results);
613612
break;
614613
case PGRES_NONFATAL_ERROR:
615614
case PGRES_FATAL_ERROR:
@@ -634,6 +633,7 @@ SendQuery(bool * success_p, PsqlSettings * settings, const char *query,
634633
notify->relname, notify->be_pid);
635634
free(notify);
636635
}
636+
if(results) PQclear(results);
637637
}
638638
}
639639

@@ -1434,7 +1434,6 @@ MainLoop(PsqlSettings * settings, FILE * source)
14341434
}
14351435

14361436
query_start = line;
1437-
14381437
if (line == NULL) { /* No more input. Time to quit */
14391438
if (!settings->quiet)
14401439
printf("EOF\n"); /* Goes on prompt line */
@@ -1544,23 +1543,25 @@ MainLoop(PsqlSettings * settings, FILE * source)
15441543
fprintf(stderr, "query buffer max length of %d exceeded\n",
15451544
MAX_QUERY_BUFFER);
15461545
fprintf(stderr, "query line ignored\n");
1546+
free (line);
15471547
} else {
15481548
if (query_start[0] != '\0') {
1549+
15491550
querySent = false;
15501551
if (query[0] != '\0') {
15511552
strcat(query, "\n");
15521553
strcat(query, query_start);
15531554
} else
15541555
strcpy(query, query_start);
15551556
}
1557+
free(line); /* PURIFY */
15561558
}
15571559

15581560
if (slashCmdStatus == 0) {
15591561
SendQuery(&success, settings, query, false, false, 0);
15601562
successResult &= success;
15611563
querySent = true;
15621564
}
1563-
free(line); /* free storage malloc'd by GetNextLine */
15641565
}
15651566
} /* while */
15661567
return successResult;
@@ -1702,6 +1703,7 @@ main(int argc, char **argv)
17021703
if (PQstatus(settings.db) == CONNECTION_BAD) {
17031704
fprintf(stderr, "Connection to database '%s' failed.\n", dbname);
17041705
fprintf(stderr, "%s", PQerrorMessage(settings.db));
1706+
PQfinish(settings.db);
17051707
exit(1);
17061708
}
17071709
if (listDatabases) {
@@ -1731,6 +1733,8 @@ main(int argc, char **argv)
17311733
sprintf(line, "\\i %s", qfilename);
17321734
}
17331735
HandleSlashCmds(&settings, line, "");
1736+
if (!singleSlashCmd) free (line); /* PURIFY */
1737+
17341738
} else {
17351739
if (singleQuery) {
17361740
bool success; /* The query succeeded at the backend */
@@ -1741,6 +1745,8 @@ main(int argc, char **argv)
17411745
}
17421746

17431747
PQfinish(settings.db);
1748+
free(settings.opt.fieldSep); /* PURIFY */
1749+
if(settings.prompt) free(settings.prompt); /* PURIFY */
17441750

17451751
return !successResult;
17461752
}

src/interfaces/libpq/fe-connect.c

+18-11
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.36 1997/06/01 15:38:52 scrappy Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.37 1997/06/06 01:42:02 scrappy Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -569,6 +569,12 @@ connectDB(PGconn *conn)
569569
return CONNECTION_OK;
570570

571571
connect_errReturn:
572+
573+
/* Igor/6/3/97 - We need to free it here...otherwise the function
574+
returns without setting conn->port to port. Because of that
575+
any way of referencing this variable will be lost and it's allocated
576+
memory will not be freed. */
577+
free(port); /* PURIFY */
572578
return CONNECTION_BAD;
573579

574580
}
@@ -581,6 +587,7 @@ connectDB(PGconn *conn)
581587
static void
582588
freePGconn(PGconn *conn)
583589
{
590+
if (!conn) return;
584591
if (conn->pghost) free(conn->pghost);
585592
if (conn->pgtty) free(conn->pgtty);
586593
if (conn->pgoptions) free(conn->pgoptions);
@@ -639,7 +646,7 @@ void
639646
PQfinish(PGconn *conn)
640647
{
641648
if (!conn) {
642-
fprintf(stderr,"PQfinish() -- pointer to PGconn is null");
649+
fprintf(stderr,"PQfinish() -- pointer to PGconn is null\n");
643650
} else {
644651
if (conn->status == CONNECTION_OK)
645652
closePGconn(conn);
@@ -655,7 +662,7 @@ void
655662
PQreset(PGconn *conn)
656663
{
657664
if (!conn) {
658-
fprintf(stderr,"PQreset() -- pointer to PGconn is null");
665+
fprintf(stderr,"PQreset() -- pointer to PGconn is null\n");
659666
} else {
660667
closePGconn(conn);
661668
conn->status = connectDB(conn);
@@ -957,7 +964,7 @@ char*
957964
PQdb(PGconn* conn)
958965
{
959966
if (!conn) {
960-
fprintf(stderr,"PQdb() -- pointer to PGconn is null");
967+
fprintf(stderr,"PQdb() -- pointer to PGconn is null\n");
961968
return (char *)NULL;
962969
}
963970
return conn->dbName;
@@ -967,7 +974,7 @@ char*
967974
PQuser(PGconn* conn)
968975
{
969976
if (!conn) {
970-
fprintf(stderr,"PQuser() -- pointer to PGconn is null");
977+
fprintf(stderr,"PQuser() -- pointer to PGconn is null\n");
971978
return (char *)NULL;
972979
}
973980
return conn->pguser;
@@ -977,7 +984,7 @@ char*
977984
PQhost(PGconn* conn)
978985
{
979986
if (!conn) {
980-
fprintf(stderr,"PQhost() -- pointer to PGconn is null");
987+
fprintf(stderr,"PQhost() -- pointer to PGconn is null\n");
981988
return (char *)NULL;
982989
}
983990

@@ -988,7 +995,7 @@ char*
988995
PQoptions(PGconn* conn)
989996
{
990997
if (!conn) {
991-
fprintf(stderr,"PQoptions() -- pointer to PGconn is null");
998+
fprintf(stderr,"PQoptions() -- pointer to PGconn is null\n");
992999
return (char *)NULL;
9931000
}
9941001
return conn->pgoptions;
@@ -998,7 +1005,7 @@ char*
9981005
PQtty(PGconn* conn)
9991006
{
10001007
if (!conn) {
1001-
fprintf(stderr,"PQtty() -- pointer to PGconn is null");
1008+
fprintf(stderr,"PQtty() -- pointer to PGconn is null\n");
10021009
return (char *)NULL;
10031010
}
10041011
return conn->pgtty;
@@ -1008,7 +1015,7 @@ char*
10081015
PQport(PGconn* conn)
10091016
{
10101017
if (!conn) {
1011-
fprintf(stderr,"PQport() -- pointer to PGconn is null");
1018+
fprintf(stderr,"PQport() -- pointer to PGconn is null\n");
10121019
return (char *)NULL;
10131020
}
10141021
return conn->pgport;
@@ -1018,7 +1025,7 @@ ConnStatusType
10181025
PQstatus(PGconn* conn)
10191026
{
10201027
if (!conn) {
1021-
fprintf(stderr,"PQstatus() -- pointer to PGconn is null");
1028+
fprintf(stderr,"PQstatus() -- pointer to PGconn is null\n");
10221029
return CONNECTION_BAD;
10231030
}
10241031
return conn->status;
@@ -1028,7 +1035,7 @@ char*
10281035
PQerrorMessage(PGconn* conn)
10291036
{
10301037
if (!conn) {
1031-
fprintf(stderr,"PQerrorMessage() -- pointer to PGconn is null");
1038+
fprintf(stderr,"PQerrorMessage() -- pointer to PGconn is null\n");
10321039
return (char *)NULL;
10331040
}
10341041
return conn->errorMessage;

0 commit comments

Comments
 (0)