Skip to content

Commit 3b302eb

Browse files
committed
Remove arbitrary MAXPGPATH limit on command lengths in pg_ctl.
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
1 parent 9046a05 commit 3b302eb

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/bin/pg_ctl/pg_ctl.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ free_readfile(char **optlines)
448448
static pgpid_t
449449
start_postmaster(void)
450450
{
451-
char cmd[MAXPGPATH];
451+
char *cmd;
452452

453453
#ifndef WIN32
454454
pgpid_t pm_pid;
@@ -493,12 +493,12 @@ start_postmaster(void)
493493
* has the same PID as the current child process.
494494
*/
495495
if (log_file != NULL)
496-
snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
497-
exec_path, pgdata_opt, post_opts,
498-
DEVNULL, log_file);
496+
cmd = psprintf("exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
497+
exec_path, pgdata_opt, post_opts,
498+
DEVNULL, log_file);
499499
else
500-
snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" 2>&1",
501-
exec_path, pgdata_opt, post_opts, DEVNULL);
500+
cmd = psprintf("exec \"%s\" %s%s < \"%s\" 2>&1",
501+
exec_path, pgdata_opt, post_opts, DEVNULL);
502502

503503
(void) execl("/bin/sh", "/bin/sh", "-c", cmd, (char *) NULL);
504504

@@ -553,12 +553,12 @@ start_postmaster(void)
553553
else
554554
close(fd);
555555

556-
snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
557-
exec_path, pgdata_opt, post_opts, DEVNULL, log_file);
556+
cmd = psprintf("CMD /C \"\"%s\" %s%s < \"%s\" >> \"%s\" 2>&1\"",
557+
exec_path, pgdata_opt, post_opts, DEVNULL, log_file);
558558
}
559559
else
560-
snprintf(cmd, MAXPGPATH, "CMD /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
561-
exec_path, pgdata_opt, post_opts, DEVNULL);
560+
cmd = psprintf("CMD /C \"\"%s\" %s%s < \"%s\" 2>&1\"",
561+
exec_path, pgdata_opt, post_opts, DEVNULL);
562562

563563
if (!CreateRestrictedProcess(cmd, &pi, false))
564564
{
@@ -829,7 +829,7 @@ find_other_exec_or_die(const char *argv0, const char *target, const char *versio
829829
static void
830830
do_init(void)
831831
{
832-
char cmd[MAXPGPATH];
832+
char *cmd;
833833

834834
if (exec_path == NULL)
835835
exec_path = find_other_exec_or_die(argv0, "initdb", "initdb (PostgreSQL) " PG_VERSION "\n");
@@ -841,11 +841,11 @@ do_init(void)
841841
post_opts = "";
842842

843843
if (!silent_mode)
844-
snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
845-
exec_path, pgdata_opt, post_opts);
844+
cmd = psprintf("\"%s\" %s%s",
845+
exec_path, pgdata_opt, post_opts);
846846
else
847-
snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
848-
exec_path, pgdata_opt, post_opts, DEVNULL);
847+
cmd = psprintf("\"%s\" %s%s > \"%s\"",
848+
exec_path, pgdata_opt, post_opts, DEVNULL);
849849

850850
if (system(cmd) != 0)
851851
{
@@ -2207,9 +2207,9 @@ set_starttype(char *starttypeopt)
22072207
static void
22082208
adjust_data_dir(void)
22092209
{
2210-
char cmd[MAXPGPATH],
2211-
filename[MAXPGPATH],
2212-
*my_exec_path;
2210+
char filename[MAXPGPATH];
2211+
char *my_exec_path,
2212+
*cmd;
22132213
FILE *fd;
22142214

22152215
/* do nothing if we're working without knowledge of data dir */
@@ -2239,10 +2239,10 @@ adjust_data_dir(void)
22392239
my_exec_path = pg_strdup(exec_path);
22402240

22412241
/* it's important for -C to be the first option, see main.c */
2242-
snprintf(cmd, MAXPGPATH, "\"%s\" -C data_directory %s%s",
2243-
my_exec_path,
2244-
pgdata_opt ? pgdata_opt : "",
2245-
post_opts ? post_opts : "");
2242+
cmd = psprintf("\"%s\" -C data_directory %s%s",
2243+
my_exec_path,
2244+
pgdata_opt ? pgdata_opt : "",
2245+
post_opts ? post_opts : "");
22462246

22472247
fd = popen(cmd, "r");
22482248
if (fd == NULL || fgets(filename, sizeof(filename), fd) == NULL)

0 commit comments

Comments
 (0)