1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-02 11:44:50 +03:00

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...
This commit is contained in:
Tom Lane 1999-11-05 06:43:45 +00:00
parent 3d1db6ddc6
commit 3b004b8e2e

View File

@ -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
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);
#ifdef MULTIBYTE if (!val || *val == '\0')
/* query server encoding */
env = getenv(envname);
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