Commit 48a2cd37 authored by Bruce Momjian's avatar Bruce Momjian

psql: fix startup crash caused by PSQLRC containing a tilde

'strdup' the PSQLRC environment variable value before calling a routine
that might free() it.

Backpatch to 9.2, where the bug first appeared.
parent bf2b0a14
...@@ -1645,11 +1645,11 @@ session_username(void) ...@@ -1645,11 +1645,11 @@ session_username(void)
* substitute '~' with HOME or '~username' with username's home dir * substitute '~' with HOME or '~username' with username's home dir
* *
*/ */
char * void
expand_tilde(char **filename) expand_tilde(char **filename)
{ {
if (!filename || !(*filename)) if (!filename || !(*filename))
return NULL; return;
/* /*
* WIN32 doesn't use tilde expansion for file names. Also, it uses tilde * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
...@@ -1697,5 +1697,5 @@ expand_tilde(char **filename) ...@@ -1697,5 +1697,5 @@ expand_tilde(char **filename)
} }
#endif #endif
return *filename; return;
} }
...@@ -44,6 +44,6 @@ extern bool is_superuser(void); ...@@ -44,6 +44,6 @@ extern bool is_superuser(void);
extern bool standard_strings(void); extern bool standard_strings(void);
extern const char *session_username(void); extern const char *session_username(void);
extern char *expand_tilde(char **filename); extern void expand_tilde(char **filename);
#endif /* COMMON_H */ #endif /* COMMON_H */
...@@ -610,7 +610,7 @@ process_psqlrc(char *argv0) ...@@ -610,7 +610,7 @@ process_psqlrc(char *argv0)
char rc_file[MAXPGPATH]; char rc_file[MAXPGPATH];
char my_exec_path[MAXPGPATH]; char my_exec_path[MAXPGPATH];
char etc_path[MAXPGPATH]; char etc_path[MAXPGPATH];
char *envrc; char *envrc = getenv("PSQLRC");
find_my_exec(argv0, my_exec_path); find_my_exec(argv0, my_exec_path);
get_etc_path(my_exec_path, etc_path); get_etc_path(my_exec_path, etc_path);
...@@ -618,12 +618,13 @@ process_psqlrc(char *argv0) ...@@ -618,12 +618,13 @@ process_psqlrc(char *argv0)
snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC); snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
process_psqlrc_file(rc_file); process_psqlrc_file(rc_file);
envrc = getenv("PSQLRC");
if (envrc != NULL && strlen(envrc) > 0) if (envrc != NULL && strlen(envrc) > 0)
{ {
expand_tilde(&envrc); /* might need to free() this */
process_psqlrc_file(envrc); char *envrc_alloc = pstrdup(envrc);
expand_tilde(&envrc_alloc);
process_psqlrc_file(envrc_alloc);
} }
else if (get_home_path(home)) else if (get_home_path(home))
{ {
......
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