Commit 3b004b8e authored by Tom Lane's avatar Tom Lane

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...
parent 3d1db6dd
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.104 1999/10/26 04:49:00 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-connect.c,v 1.105 1999/11/05 06:43:45 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -854,50 +854,41 @@ void ...@@ -854,50 +854,41 @@ void
PQsetenv(PGconn *conn) PQsetenv(PGconn *conn)
{ {
struct EnvironmentOptions *eo; struct EnvironmentOptions *eo;
char setQuery[80]; /* mjl: size okay? XXX */ char setQuery[100]; /* note length limits in sprintf's below */
const char *val;
PGresult *res;
#ifdef MULTIBYTE #ifdef MULTIBYTE
char *envname = "PGCLIENTENCODING"; char *envname = "PGCLIENTENCODING";
static char envbuf[64]; /* big enough? */
char *env;
char *encoding = 0;
PGresult *rtn;
#endif /* Set env. variable PGCLIENTENCODING if it's not set already */
val = getenv(envname);
if (!val || *val == '\0')
{
const char *encoding = NULL;
#ifdef MULTIBYTE
/* query server encoding */ /* query server encoding */
env = getenv(envname); res = PQexec(conn, "select getdatabaseencoding()");
if (!env || *env == '\0') if (res && PQresultStatus(res) == PGRES_TUPLES_OK)
{ encoding = PQgetvalue(res, 0, 0);
rtn = PQexec(conn, "select getdatabaseencoding()"); if (!encoding) /* this should not happen */
if (rtn && PQresultStatus(rtn) == PGRES_TUPLES_OK) encoding = pg_encoding_to_char(MULTIBYTE);
{
encoding = PQgetvalue(rtn, 0, 0);
if (encoding) if (encoding)
{ {
/* set client encoding */ /* set client encoding via environment variable */
char *envbuf;
envbuf = (char *) malloc(strlen(envname) + strlen(encoding) + 2);
sprintf(envbuf, "%s=%s", envname, encoding); sprintf(envbuf, "%s=%s", envname, encoding);
putenv(envbuf); putenv(envbuf);
} }
} PQclear(res);
PQclear(rtn);
if (!encoding)
{ /* this should not happen */
sprintf(envbuf, "%s=%s", envname, pg_encoding_to_char(MULTIBYTE));
putenv(envbuf);
}
} }
#endif #endif
for (eo = EnvironmentOptions; eo->envName; eo++) for (eo = EnvironmentOptions; eo->envName; eo++)
{ {
const char *val;
if ((val = getenv(eo->envName))) if ((val = getenv(eo->envName)))
{ {
PGresult *res;
if (strcasecmp(val, "default") == 0) if (strcasecmp(val, "default") == 0)
sprintf(setQuery, "SET %s = %.60s", eo->pgName, val); sprintf(setQuery, "SET %s = %.60s", eo->pgName, val);
else else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment