]> granicus.if.org Git - postgresql/commitdiff
psql: fix startup crash caused by PSQLRC containing a tilde
authorBruce Momjian <bruce@momjian.us>
Thu, 4 Apr 2013 16:56:21 +0000 (12:56 -0400)
committerBruce Momjian <bruce@momjian.us>
Thu, 4 Apr 2013 16:56:25 +0000 (12:56 -0400)
'strdup' the PSQLRC environment variable value before calling a routine
that might free() it.

Backpatch to 9.2, where the bug first appeared.

src/bin/psql/common.c
src/bin/psql/common.h
src/bin/psql/startup.c

index c804148cd575623c64caa85a15345f1eb5a4c032..df8c80a1b26d3d7a17a22fa4935b7ece03d2baba 100644 (file)
@@ -1617,11 +1617,11 @@ session_username(void)
  * substitute '~' with HOME or '~username' with username's home dir
  *
  */
-char *
+void
 expand_tilde(char **filename)
 {
        if (!filename || !(*filename))
-               return NULL;
+               return;
 
        /*
         * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
@@ -1669,5 +1669,5 @@ expand_tilde(char **filename)
        }
 #endif
 
-       return *filename;
+       return;
 }
index 8037cbc0a0d60790c18105e8fc62208725ebb7df..cfe0dadfd667d98df0d54de00b45433bf08ed47e 100644 (file)
@@ -61,6 +61,6 @@ extern bool is_superuser(void);
 extern bool standard_strings(void);
 extern const char *session_username(void);
 
-extern char *expand_tilde(char **filename);
+extern void expand_tilde(char **filename);
 
 #endif   /* COMMON_H */
index 9a6306b8cf2479039c1b3982a9c018eadbff04b0..b30639395ecad69d9271b2851468612b6c853a75 100644 (file)
@@ -591,7 +591,7 @@ process_psqlrc(char *argv0)
        char            rc_file[MAXPGPATH];
        char            my_exec_path[MAXPGPATH];
        char            etc_path[MAXPGPATH];
-       char       *envrc;
+       char       *envrc = getenv("PSQLRC");
 
        find_my_exec(argv0, my_exec_path);
        get_etc_path(my_exec_path, etc_path);
@@ -599,12 +599,13 @@ process_psqlrc(char *argv0)
        snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
        process_psqlrc_file(rc_file);
 
-       envrc = getenv("PSQLRC");
-
        if (envrc != NULL && strlen(envrc) > 0)
        {
-               expand_tilde(&envrc);
-               process_psqlrc_file(envrc);
+               /* might need to free() this */
+               char *envrc_alloc = pg_strdup(envrc);
+
+               expand_tilde(&envrc_alloc);
+               process_psqlrc_file(envrc_alloc);
        }
        else if (get_home_path(home))
        {