Skip to content

Commit a9380b5

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 360abc0 commit a9380b5

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>
@@ -1239,16 +1241,13 @@ pgwin32_IsInstalled(SC_HANDLE hSCM)
12391241
static char *
12401242
pgwin32_CommandLine(bool registration)
12411243
{
1242-
static char cmdLine[MAXPGPATH];
1244+
PQExpBuffer cmdLine = createPQExpBuffer();
1245+
char cmdPath[MAXPGPATH];
12431246
int ret;
12441247

1245-
#ifdef __CYGWIN__
1246-
char buf[MAXPGPATH];
1247-
#endif
1248-
12491248
if (registration)
12501249
{
1251-
ret = find_my_exec(argv0, cmdLine);
1250+
ret = find_my_exec(argv0, cmdPath);
12521251
if (ret != 0)
12531252
{
12541253
write_stderr(_("%s: could not find own program executable\n"), progname);
@@ -1258,7 +1257,7 @@ pgwin32_CommandLine(bool registration)
12581257
else
12591258
{
12601259
ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1261-
cmdLine);
1260+
cmdPath);
12621261
if (ret != 0)
12631262
{
12641263
write_stderr(_("%s: could not find postgres program executable\n"), progname);
@@ -1268,54 +1267,57 @@ pgwin32_CommandLine(bool registration)
12681267

12691268
#ifdef __CYGWIN__
12701269
/* need to convert to windows path */
1270+
{
1271+
char buf[MAXPGPATH];
1272+
12711273
#if CYGWIN_VERSION_DLL_MAJOR >= 1007
1272-
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdLine, buf, sizeof(buf));
1274+
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdPath, buf, sizeof(buf));
12731275
#else
1274-
cygwin_conv_to_full_win32_path(cmdLine, buf);
1276+
cygwin_conv_to_full_win32_path(cmdPath, buf);
12751277
#endif
1276-
strcpy(cmdLine, buf);
1278+
strcpy(cmdPath, buf);
1279+
}
12771280
#endif
12781281

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

12911300
if (pg_config)
1292-
{
1293-
strcat(cmdLine, " -D \"");
1294-
strcat(cmdLine, pg_config);
1295-
strcat(cmdLine, "\"");
1296-
}
1301+
appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_config);
12971302

12981303
if (registration && do_wait)
1299-
strcat(cmdLine, " -w");
1304+
appendPQExpBuffer(cmdLine, " -w");
13001305

13011306
if (registration && wait_seconds != DEFAULT_WAIT)
1302-
/* concatenate */
1303-
sprintf(cmdLine + strlen(cmdLine), " -t %d", wait_seconds);
1307+
appendPQExpBuffer(cmdLine, " -t %d", wait_seconds);
13041308

13051309
if (registration && silent_mode)
1306-
strcat(cmdLine, " -s");
1310+
appendPQExpBuffer(cmdLine, " -s");
13071311

13081312
if (post_opts)
13091313
{
1310-
strcat(cmdLine, " ");
1311-
if (registration)
1312-
strcat(cmdLine, " -o \"");
1313-
strcat(cmdLine, post_opts);
13141314
if (registration)
1315-
strcat(cmdLine, "\"");
1315+
appendPQExpBuffer(cmdLine, " -o \"%s\"", post_opts);
1316+
else
1317+
appendPQExpBuffer(cmdLine, " %s", post_opts);
13161318
}
13171319

1318-
return cmdLine;
1320+
return cmdLine->data;
13191321
}
13201322

13211323
static void
@@ -1746,7 +1748,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
17461748
*/
17471749
return r;
17481750
}
1749-
#endif
1751+
#endif /* defined(WIN32) || defined(__CYGWIN__) */
17501752

17511753
static void
17521754
do_advice(void)

0 commit comments

Comments
 (0)