Skip to content

Commit 8c72b20

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 bf0d21e commit 8c72b20

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>
@@ -1251,16 +1253,13 @@ pgwin32_IsInstalled(SC_HANDLE hSCM)
12511253
static char *
12521254
pgwin32_CommandLine(bool registration)
12531255
{
1254-
static char cmdLine[MAXPGPATH];
1256+
PQExpBuffer cmdLine = createPQExpBuffer();
1257+
char cmdPath[MAXPGPATH];
12551258
int ret;
12561259

1257-
#ifdef __CYGWIN__
1258-
char buf[MAXPGPATH];
1259-
#endif
1260-
12611260
if (registration)
12621261
{
1263-
ret = find_my_exec(argv0, cmdLine);
1262+
ret = find_my_exec(argv0, cmdPath);
12641263
if (ret != 0)
12651264
{
12661265
write_stderr(_("%s: could not find own program executable\n"), progname);
@@ -1270,7 +1269,7 @@ pgwin32_CommandLine(bool registration)
12701269
else
12711270
{
12721271
ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1273-
cmdLine);
1272+
cmdPath);
12741273
if (ret != 0)
12751274
{
12761275
write_stderr(_("%s: could not find postgres program executable\n"), progname);
@@ -1280,54 +1279,57 @@ pgwin32_CommandLine(bool registration)
12801279

12811280
#ifdef __CYGWIN__
12821281
/* need to convert to windows path */
1282+
{
1283+
char buf[MAXPGPATH];
1284+
12831285
#if CYGWIN_VERSION_DLL_MAJOR >= 1007
1284-
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdLine, buf, sizeof(buf));
1286+
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdPath, buf, sizeof(buf));
12851287
#else
1286-
cygwin_conv_to_full_win32_path(cmdLine, buf);
1288+
cygwin_conv_to_full_win32_path(cmdPath, buf);
12871289
#endif
1288-
strcpy(cmdLine, buf);
1290+
strcpy(cmdPath, buf);
1291+
}
12891292
#endif
12901293

1294+
/* if path does not end in .exe, append it */
1295+
if (strlen(cmdPath) < 4 ||
1296+
pg_strcasecmp(cmdPath + strlen(cmdPath) - 4, ".exe") != 0)
1297+
snprintf(cmdPath + strlen(cmdPath), sizeof(cmdPath) - strlen(cmdPath),
1298+
".exe");
1299+
1300+
/* use backslashes in path to avoid problems with some third-party tools */
1301+
make_native_path(cmdPath);
1302+
1303+
/* be sure to double-quote the executable's name in the command */
1304+
appendPQExpBuffer(cmdLine, "\"%s\"", cmdPath);
1305+
1306+
/* append assorted switches to the command line, as needed */
1307+
12911308
if (registration)
1292-
{
1293-
if (pg_strcasecmp(cmdLine + strlen(cmdLine) - 4, ".exe"))
1294-
{
1295-
/* If commandline does not end in .exe, append it */
1296-
strcat(cmdLine, ".exe");
1297-
}
1298-
strcat(cmdLine, " runservice -N \"");
1299-
strcat(cmdLine, register_servicename);
1300-
strcat(cmdLine, "\"");
1301-
}
1309+
appendPQExpBuffer(cmdLine, " runservice -N \"%s\"",
1310+
register_servicename);
13021311

13031312
if (pg_data)
1304-
{
1305-
strcat(cmdLine, " -D \"");
1306-
strcat(cmdLine, pg_data);
1307-
strcat(cmdLine, "\"");
1308-
}
1313+
appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_data);
13091314

13101315
if (registration && do_wait)
1311-
strcat(cmdLine, " -w");
1316+
appendPQExpBuffer(cmdLine, " -w");
13121317

13131318
if (registration && wait_seconds != DEFAULT_WAIT)
1314-
/* concatenate */
1315-
sprintf(cmdLine + strlen(cmdLine), " -t %d", wait_seconds);
1319+
appendPQExpBuffer(cmdLine, " -t %d", wait_seconds);
13161320

13171321
if (registration && silent_mode)
1318-
strcat(cmdLine, " -s");
1322+
appendPQExpBuffer(cmdLine, " -s");
13191323

13201324
if (post_opts)
13211325
{
1322-
strcat(cmdLine, " ");
1323-
if (registration)
1324-
strcat(cmdLine, " -o \"");
1325-
strcat(cmdLine, post_opts);
13261326
if (registration)
1327-
strcat(cmdLine, "\"");
1327+
appendPQExpBuffer(cmdLine, " -o \"%s\"", post_opts);
1328+
else
1329+
appendPQExpBuffer(cmdLine, " %s", post_opts);
13281330
}
13291331

1330-
return cmdLine;
1332+
return cmdLine->data;
13311333
}
13321334

13331335
static void
@@ -1765,7 +1767,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
17651767
*/
17661768
return r;
17671769
}
1768-
#endif
1770+
#endif /* defined(WIN32) || defined(__CYGWIN__) */
17691771

17701772
static void
17711773
do_advice(void)

0 commit comments

Comments
 (0)