Preserve putenv() memory in psql \connect user fix.

This commit is contained in:
Bruce Momjian 1997-05-22 23:57:27 +00:00
parent 65ff0feacf
commit 148f7203fa
1 changed files with 5 additions and 4 deletions

View File

@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.61 1997/05/22 18:55:35 momjian Exp $ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.62 1997/05/22 23:57:27 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -836,7 +836,7 @@ do_connect(const char *new_dbname,
fprintf(stderr, "\\connect must be followed by a database name\n"); fprintf(stderr, "\\connect must be followed by a database name\n");
else { else {
PGconn *olddb = settings->db; PGconn *olddb = settings->db;
char *userenv; static char *userenv = NULL;
printf("closing connection to database: %s\n", dbname); printf("closing connection to database: %s\n", dbname);
if (new_user != NULL) { if (new_user != NULL) {
@ -844,10 +844,11 @@ do_connect(const char *new_dbname,
PQsetdb() does not allow us to specify the user, PQsetdb() does not allow us to specify the user,
so we have to do it via PGUSER so we have to do it via PGUSER
*/ */
if (userenv != NULL)
free(userenv);
userenv = malloc(strlen("PGUSER=") + strlen(new_user) + 1); userenv = malloc(strlen("PGUSER=") + strlen(new_user) + 1);
sprintf(userenv,"PGUSER=%s",new_user); sprintf(userenv,"PGUSER=%s",new_user);
putenv(userenv); putenv(userenv); /* putenv() continues to use memory in env. */
free(userenv);
} }
settings->db = PQsetdb(PQhost(olddb), PQport(olddb), settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
NULL, NULL, new_dbname); NULL, NULL, new_dbname);