Skip to content

Commit 449e575

Browse files
committed
Only free memory if environment value is replaced.
1 parent 658f229 commit 449e575

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/bin/psql/psql.c

Lines changed: 8 additions & 3 deletions
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.63 1997/05/23 00:20:20 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.64 1997/05/23 01:53:42 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -837,6 +837,7 @@ do_connect(const char *new_dbname,
837837
else {
838838
PGconn *olddb = settings->db;
839839
static char *userenv = NULL;
840+
char *old_userenv = NULL;
840841

841842
printf("closing connection to database: %s\n", dbname);
842843
if (new_user != NULL) {
@@ -845,10 +846,14 @@ do_connect(const char *new_dbname,
845846
so we have to do it via PGUSER
846847
*/
847848
if (userenv != NULL)
848-
free(userenv);
849+
old_userenv = userenv;
849850
userenv = malloc(strlen("PGUSER=") + strlen(new_user) + 1);
850851
sprintf(userenv,"PGUSER=%s",new_user);
851-
putenv(userenv); /*Solaris putenv() continues to use memory in env*/
852+
/* putenv() may continue to use memory as part of environment */
853+
putenv(userenv);
854+
/* can delete old memory if we malloc'ed it */
855+
if (old_userenv != NULL)
856+
free(old_userenv);
852857
}
853858
settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
854859
NULL, NULL, new_dbname);

0 commit comments

Comments
 (0)