In thinking about it, I realized I did not handle the memory freeing
properly in the patch earlier today. I can only free the old memory
AFTER I call putenv().
Solaris is clearly correct in its warning.
Though Stevens seems to say it is OK, the Unix Programming FAQ says:
The string passed to putenv must *not* be freed or made invalid, since a
pointer to it is kept by `putenv()'. This means that it must either be a
static buffer or allocated off the heap. The string can be freed if the
environment variable is redefined or deleted via another call to
`putenv()'.
Here is the new patch I just applied. It postpones the free'ing until
after the putenv().
- ---------------------------------------------------------------------------
*** ./bin/psql/psql.c.orig Thu May 22 21:36:12 1997
- --- ./bin/psql/psql.c Thu May 22 21:50:15 1997
***************
*** 837,842 ****
- --- 837,843 ----
else {
PGconn *olddb = settings->db;
static char *userenv = NULL;
+ char *old_userenv = NULL;
printf("closing connection to database: %s\n", dbname);
if (new_user != NULL) {
***************
*** 845,854 ****
so we have to do it via PGUSER
*/
if (userenv != NULL)
! free(userenv);
userenv = malloc(strlen("PGUSER=") + strlen(new_user) + 1);
sprintf(userenv,"PGUSER=%s",new_user);
! putenv(userenv); /*Solaris putenv() continues to use memory in env*/
}
settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
NULL, NULL, new_dbname);
- --- 846,859 ----
so we have to do it via PGUSER
*/
if (userenv != NULL)
! old_userenv = userenv;
userenv = malloc(strlen("PGUSER=") + strlen(new_user) + 1);
sprintf(userenv,"PGUSER=%s",new_user);
! /* putenv() may continue to use memory as part of environment */
! putenv(userenv);
! /* can delete old memory if we malloc'ed it */
! if (old_userenv != NULL)
! free(old_userenv);
}
settings->db = PQsetdb(PQhost(olddb), PQport(olddb),
NULL, NULL, new_dbname);
- --
Bruce Momjian
maillist@candle.pha.pa.us
------------------------------
End of hackers-digest V1 #361
*****************************