]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Remove arbitrary MAXPGPATH limit on command lengths in pg_ctl.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 4 Sep 2021 01:04:45 +0000 (21:04 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 4 Sep 2021 01:04:45 +0000 (21:04 -0400)
Replace fixed-length command buffers with psprintf() calls.  We didn't
have anything as convenient as psprintf() when this code was written,
but now that we do, there's little reason for the limitation to
stand.  Removing it eliminates some corner cases where (for example)
starting the postmaster with a whole lot of options fails.

Most individual file names that pg_ctl deals with are still restricted
to MAXPGPATH, but we've seldom had complaints about that limitation
so long as it only applies to one filename.

Back-patch to all supported branches.

Phil Krylov

Discussion: https://postgr.es/m/567e199c6b97ee19deee600311515b86@krylov.eu

src/bin/pg_ctl/pg_ctl.c

index efc07291adea715b0f39266da723c6cd47f1851d..a8885614d2f1a80c31583a96b36446c0f9952725 100644 (file)
@@ -428,7 +428,7 @@ free_readfile(char **optlines)
 static pgpid_t
 start_postmaster(void)
 {
-       char            cmd[MAXPGPATH];
+       char       *cmd;
 
 #ifndef WIN32
        pgpid_t         pm_pid;
@@ -459,12 +459,12 @@ start_postmaster(void)
         * has the same PID as the current child process.
         */
        if (log_file != NULL)
-               snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
-                                exec_path, pgdata_opt, post_opts,
-                                DEVNULL, log_file);
+               cmd = psprintf("exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
+                                          exec_path, pgdata_opt, post_opts,
+                                          DEVNULL, log_file);
        else
-               snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" 2>&1",
-                                exec_path, pgdata_opt, post_opts, DEVNULL);
+               cmd = psprintf("exec \"%s\" %s%s < \"%s\" 2>&1",
+                                          exec_path, pgdata_opt, post_opts, DEVNULL);
 
        (void) execl("/bin/sh", "/bin/sh", "-c", cmd, (char *) NULL);
 
@@ -485,11 +485,11 @@ start_postmaster(void)
        PROCESS_INFORMATION pi;
 
        if (log_file != NULL)
-               snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
-                                exec_path, pgdata_opt, post_opts, DEVNULL, log_file);
+               cmd = psprintf("CMD /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
+                                          exec_path, pgdata_opt, post_opts, DEVNULL, log_file);
        else
-               snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
-                                exec_path, pgdata_opt, post_opts, DEVNULL);
+               cmd = psprintf("CMD /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
+                                          exec_path, pgdata_opt, post_opts, DEVNULL);
 
        if (!CreateRestrictedProcess(cmd, &pi, false))
        {
@@ -837,7 +837,7 @@ find_other_exec_or_die(const char *argv0, const char *target, const char *versio
 static void
 do_init(void)
 {
-       char            cmd[MAXPGPATH];
+       char       *cmd;
 
        if (exec_path == NULL)
                exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
@@ -849,11 +849,11 @@ do_init(void)
                post_opts = "";
 
        if (!silent_mode)
-               snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
-                                exec_path, pgdata_opt, post_opts);
+               cmd = psprintf("\"%s\" %s%s",
+                                          exec_path, pgdata_opt, post_opts);
        else
-               snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
-                                exec_path, pgdata_opt, post_opts, DEVNULL);
+               cmd = psprintf("\"%s\" %s%s > \"%s\"",
+                                          exec_path, pgdata_opt, post_opts, DEVNULL);
 
        if (system(cmd) != 0)
        {
@@ -2059,9 +2059,9 @@ set_starttype(char *starttypeopt)
 static void
 adjust_data_dir(void)
 {
-       char            cmd[MAXPGPATH],
-                               filename[MAXPGPATH],
-                          *my_exec_path;
+       char            filename[MAXPGPATH];
+       char       *my_exec_path,
+                          *cmd;
        FILE       *fd;
 
        /* do nothing if we're working without knowledge of data dir */
@@ -2091,10 +2091,10 @@ adjust_data_dir(void)
                my_exec_path = pg_strdup(exec_path);
 
        /* it's important for -C to be the first option, see main.c */
-       snprintf(cmd, MAXPGPATH, "\"%s\" -C data_directory %s%s",
-                        my_exec_path,
-                        pgdata_opt ? pgdata_opt : "",
-                        post_opts ? post_opts : "");
+       cmd = psprintf("\"%s\" -C data_directory %s%s",
+                                  my_exec_path,
+                                  pgdata_opt ? pgdata_opt : "",
+                                  post_opts ? post_opts : "");
 
        fd = popen(cmd, "r");
        if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)