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
#ifdef MULTIBYTE /* Set env. variable PGCLIENTENCODING if it's not set already */
/* query server encoding */ val = getenv(envname);
env = getenv(envname); if (!val || *val == '\0')
if (!env || *env == '\0')
{ {
rtn = PQexec(conn, "select getdatabaseencoding()"); const char *encoding = NULL;
if (rtn && PQresultStatus(rtn) == PGRES_TUPLES_OK)
/* query server encoding */
res = PQexec(conn, "select getdatabaseencoding()");
if (res && PQresultStatus(res) == PGRES_TUPLES_OK)
encoding = PQgetvalue(res, 0, 0);
if (!encoding) /* this should not happen */
encoding = pg_encoding_to_char(MULTIBYTE);
if (encoding)
{ {
encoding = PQgetvalue(rtn, 0, 0); /* set client encoding via environment variable */
if (encoding) char *envbuf;
{
/* set client encoding */ envbuf = (char *) malloc(strlen(envname) + strlen(encoding) + 2);
sprintf(envbuf, "%s=%s", envname, encoding); sprintf(envbuf, "%s=%s", envname, encoding);
putenv(envbuf);
}
}
PQclear(rtn);
if (!encoding)
{ /* this should not happen */
sprintf(envbuf, "%s=%s", envname, pg_encoding_to_char(MULTIBYTE));
putenv(envbuf); putenv(envbuf);
} }
PQclear(res);
} }
#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