Skip to content

Commit 3b004b8

Browse files
committed
environment variable set by MULTIBYTE startup code should be
stored in malloc'd space, not in a static variable. Otherwise environment variable list is corrupted if libpq is dynamically unlinked...
1 parent 3d1db6d commit 3b004b8

File tree

1 file changed

+22
-31
lines changed

1 file changed

+22
-31
lines changed

src/interfaces/libpq/fe-connect.c

Lines changed: 22 additions & 31 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.104 1999/10/26 04:49:00 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.105 1999/11/05 06:43:45 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -854,50 +854,41 @@ void
854854
PQsetenv(PGconn *conn)
855855
{
856856
struct EnvironmentOptions *eo;
857-
char setQuery[80]; /* mjl: size okay? XXX */
858-
857+
char setQuery[100]; /* note length limits in sprintf's below */
858+
const char *val;
859+
PGresult *res;
859860
#ifdef MULTIBYTE
860861
char *envname = "PGCLIENTENCODING";
861-
static char envbuf[64]; /* big enough? */
862-
char *env;
863-
char *encoding = 0;
864-
PGresult *rtn;
865-
866-
#endif
867862

868-
#ifdef MULTIBYTE
869-
/* query server encoding */
870-
env = getenv(envname);
871-
if (!env || *env == '\0')
863+
/* Set env. variable PGCLIENTENCODING if it's not set already */
864+
val = getenv(envname);
865+
if (!val || *val == '\0')
872866
{
873-
rtn = PQexec(conn, "select getdatabaseencoding()");
874-
if (rtn && PQresultStatus(rtn) == PGRES_TUPLES_OK)
867+
const char *encoding = NULL;
868+
869+
/* query server encoding */
870+
res = PQexec(conn, "select getdatabaseencoding()");
871+
if (res && PQresultStatus(res) == PGRES_TUPLES_OK)
872+
encoding = PQgetvalue(res, 0, 0);
873+
if (!encoding) /* this should not happen */
874+
encoding = pg_encoding_to_char(MULTIBYTE);
875+
if (encoding)
875876
{
876-
encoding = PQgetvalue(rtn, 0, 0);
877-
if (encoding)
878-
{
879-
/* set client encoding */
880-
sprintf(envbuf, "%s=%s", envname, encoding);
881-
putenv(envbuf);
882-
}
883-
}
884-
PQclear(rtn);
885-
if (!encoding)
886-
{ /* this should not happen */
887-
sprintf(envbuf, "%s=%s", envname, pg_encoding_to_char(MULTIBYTE));
877+
/* set client encoding via environment variable */
878+
char *envbuf;
879+
880+
envbuf = (char *) malloc(strlen(envname) + strlen(encoding) + 2);
881+
sprintf(envbuf, "%s=%s", envname, encoding);
888882
putenv(envbuf);
889883
}
884+
PQclear(res);
890885
}
891886
#endif
892887

893888
for (eo = EnvironmentOptions; eo->envName; eo++)
894889
{
895-
const char *val;
896-
897890
if ((val = getenv(eo->envName)))
898891
{
899-
PGresult *res;
900-
901892
if (strcasecmp(val, "default") == 0)
902893
sprintf(setQuery, "SET %s = %.60s", eo->pgName, val);
903894
else

0 commit comments

Comments
 (0)