Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 87ad491

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 db2760a commit 87ad491

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/bin/pg_ctl/pg_ctl.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ free_readfile(char **optlines)
442442
static pgpid_t
443443
start_postmaster(void)
444444
{
445-
char cmd[MAXPGPATH];
445+
char *cmd;
446446

447447
#ifndef WIN32
448448
pgpid_t pm_pid;
@@ -487,12 +487,12 @@ start_postmaster(void)
487487
* has the same PID as the current child process.
488488
*/
489489
if (log_file != NULL)
490-
snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
491-
exec_path, pgdata_opt, post_opts,
492-
DEVNULL, log_file);
490+
cmd = psprintf("exec \"%s\" %s%s < \"%s\" >> \"%s\" 2>&1",
491+
exec_path, pgdata_opt, post_opts,
492+
DEVNULL, log_file);
493493
else
494-
snprintf(cmd, MAXPGPATH, "exec \"%s\" %s%s < \"%s\" 2>&1",
495-
exec_path, pgdata_opt, post_opts, DEVNULL);
494+
cmd = psprintf("exec \"%s\" %s%s < \"%s\" 2>&1",
495+
exec_path, pgdata_opt, post_opts, DEVNULL);
496496

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

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

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

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

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

842842
if (!silent_mode)
843-
snprintf(cmd, MAXPGPATH, "\"%s\" %s%s",
844-
exec_path, pgdata_opt, post_opts);
843+
cmd = psprintf("\"%s\" %s%s",
844+
exec_path, pgdata_opt, post_opts);
845845
else
846-
snprintf(cmd, MAXPGPATH, "\"%s\" %s%s > \"%s\"",
847-
exec_path, pgdata_opt, post_opts, DEVNULL);
846+
cmd = psprintf("\"%s\" %s%s > \"%s\"",
847+
exec_path, pgdata_opt, post_opts, DEVNULL);
848848

849849
if (system(cmd) != 0)
850850
{
@@ -2175,9 +2175,9 @@ set_starttype(char *starttypeopt)
21752175
static void
21762176
adjust_data_dir(void)
21772177
{
2178-
char cmd[MAXPGPATH],
2179-
filename[MAXPGPATH],
2180-
*my_exec_path;
2178+
char filename[MAXPGPATH];
2179+
char *my_exec_path,
2180+
*cmd;
21812181
FILE *fd;
21822182

21832183
/* do nothing if we're working without knowledge of data dir */
@@ -2207,10 +2207,10 @@ adjust_data_dir(void)
22072207
my_exec_path = pg_strdup(exec_path);
22082208

22092209
/* it's important for -C to be the first option, see main.c */
2210-
snprintf(cmd, MAXPGPATH, "\"%s\" -C data_directory %s%s",
2211-
my_exec_path,
2212-
pgdata_opt ? pgdata_opt : "",
2213-
post_opts ? post_opts : "");
2210+
cmd = psprintf("\"%s\" -C data_directory %s%s",
2211+
my_exec_path,
2212+
pgdata_opt ? pgdata_opt : "",
2213+
post_opts ? post_opts : "");
22142214

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

0 commit comments

Comments
 (0)