Skip to content

Commit 79193c7

Browse files
committed
Fix assorted issues in pg_ctl's pgwin32_CommandLine().
Ensure that the invocation command for postgres or pg_ctl runservice double-quotes the executable's pathname; failure to do this leads to trouble when the path contains spaces. Also, ensure that the path ends in ".exe" in both cases and uses backslashes rather than slashes as directory separators. The latter issue is reported to confuse some third-party tools such as Symantec Backup Exec. Also, rewrite the function to avoid buffer overrun issues by using a PQExpBuffer instead of a fixed-size static buffer. Combinations of very long executable pathnames and very long data directory pathnames could have caused trouble before, for example. Back-patch to all active branches, since this code has been like this for a long while. Naoya Anzai and Tom Lane, reviewed by Rajeev Rastogi
1 parent 8b15155 commit 79193c7

File tree

1 file changed

+38
-36
lines changed

1 file changed

+38
-36
lines changed

src/bin/pg_ctl/pg_ctl.c

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#endif
1919

2020
#include "postgres_fe.h"
21+
2122
#include "libpq-fe.h"
23+
#include "pqexpbuffer.h"
2224

2325
#include <fcntl.h>
2426
#include <locale.h>
@@ -1238,16 +1240,13 @@ pgwin32_IsInstalled(SC_HANDLE hSCM)
12381240
static char *
12391241
pgwin32_CommandLine(bool registration)
12401242
{
1241-
static char cmdLine[MAXPGPATH];
1243+
PQExpBuffer cmdLine = createPQExpBuffer();
1244+
char cmdPath[MAXPGPATH];
12421245
int ret;
12431246

1244-
#ifdef __CYGWIN__
1245-
char buf[MAXPGPATH];
1246-
#endif
1247-
12481247
if (registration)
12491248
{
1250-
ret = find_my_exec(argv0, cmdLine);
1249+
ret = find_my_exec(argv0, cmdPath);
12511250
if (ret != 0)
12521251
{
12531252
write_stderr(_("%s: could not find own program executable\n"), progname);
@@ -1257,7 +1256,7 @@ pgwin32_CommandLine(bool registration)
12571256
else
12581257
{
12591258
ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1260-
cmdLine);
1259+
cmdPath);
12611260
if (ret != 0)
12621261
{
12631262
write_stderr(_("%s: could not find postgres program executable\n"), progname);
@@ -1267,54 +1266,57 @@ pgwin32_CommandLine(bool registration)
12671266

12681267
#ifdef __CYGWIN__
12691268
/* need to convert to windows path */
1269+
{
1270+
char buf[MAXPGPATH];
1271+
12701272
#if CYGWIN_VERSION_DLL_MAJOR >= 1007
1271-
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdLine, buf, sizeof(buf));
1273+
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdPath, buf, sizeof(buf));
12721274
#else
1273-
cygwin_conv_to_full_win32_path(cmdLine, buf);
1275+
cygwin_conv_to_full_win32_path(cmdPath, buf);
12741276
#endif
1275-
strcpy(cmdLine, buf);
1277+
strcpy(cmdPath, buf);
1278+
}
12761279
#endif
12771280

1281+
/* if path does not end in .exe, append it */
1282+
if (strlen(cmdPath) < 4 ||
1283+
pg_strcasecmp(cmdPath + strlen(cmdPath) - 4, ".exe") != 0)
1284+
snprintf(cmdPath + strlen(cmdPath), sizeof(cmdPath) - strlen(cmdPath),
1285+
".exe");
1286+
1287+
/* use backslashes in path to avoid problems with some third-party tools */
1288+
make_native_path(cmdPath);
1289+
1290+
/* be sure to double-quote the executable's name in the command */
1291+
appendPQExpBuffer(cmdLine, "\"%s\"", cmdPath);
1292+
1293+
/* append assorted switches to the command line, as needed */
1294+
12781295
if (registration)
1279-
{
1280-
if (pg_strcasecmp(cmdLine + strlen(cmdLine) - 4, ".exe") != 0)
1281-
{
1282-
/* If commandline does not end in .exe, append it */
1283-
strcat(cmdLine, ".exe");
1284-
}
1285-
strcat(cmdLine, " runservice -N \"");
1286-
strcat(cmdLine, register_servicename);
1287-
strcat(cmdLine, "\"");
1288-
}
1296+
appendPQExpBuffer(cmdLine, " runservice -N \"%s\"",
1297+
register_servicename);
12891298

12901299
if (pg_config)
1291-
{
1292-
strcat(cmdLine, " -D \"");
1293-
strcat(cmdLine, pg_config);
1294-
strcat(cmdLine, "\"");
1295-
}
1300+
appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_config);
12961301

12971302
if (registration && do_wait)
1298-
strcat(cmdLine, " -w");
1303+
appendPQExpBuffer(cmdLine, " -w");
12991304

13001305
if (registration && wait_seconds != DEFAULT_WAIT)
1301-
/* concatenate */
1302-
sprintf(cmdLine + strlen(cmdLine), " -t %d", wait_seconds);
1306+
appendPQExpBuffer(cmdLine, " -t %d", wait_seconds);
13031307

13041308
if (registration && silent_mode)
1305-
strcat(cmdLine, " -s");
1309+
appendPQExpBuffer(cmdLine, " -s");
13061310

13071311
if (post_opts)
13081312
{
1309-
strcat(cmdLine, " ");
1310-
if (registration)
1311-
strcat(cmdLine, " -o \"");
1312-
strcat(cmdLine, post_opts);
13131313
if (registration)
1314-
strcat(cmdLine, "\"");
1314+
appendPQExpBuffer(cmdLine, " -o \"%s\"", post_opts);
1315+
else
1316+
appendPQExpBuffer(cmdLine, " %s", post_opts);
13151317
}
13161318

1317-
return cmdLine;
1319+
return cmdLine->data;
13181320
}
13191321

13201322
static void
@@ -1745,7 +1747,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
17451747
*/
17461748
return r;
17471749
}
1748-
#endif
1750+
#endif /* defined(WIN32) || defined(__CYGWIN__) */
17491751

17501752
static void
17511753
do_advice(void)

0 commit comments

Comments
 (0)