Skip to content

Commit 19af7d4

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 f0e3e05 commit 19af7d4

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>
@@ -1265,16 +1267,13 @@ pgwin32_IsInstalled(SC_HANDLE hSCM)
12651267
static char *
12661268
pgwin32_CommandLine(bool registration)
12671269
{
1268-
static char cmdLine[MAXPGPATH];
1270+
PQExpBuffer cmdLine = createPQExpBuffer();
1271+
char cmdPath[MAXPGPATH];
12691272
int ret;
12701273

1271-
#ifdef __CYGWIN__
1272-
char buf[MAXPGPATH];
1273-
#endif
1274-
12751274
if (registration)
12761275
{
1277-
ret = find_my_exec(argv0, cmdLine);
1276+
ret = find_my_exec(argv0, cmdPath);
12781277
if (ret != 0)
12791278
{
12801279
write_stderr(_("%s: could not find own program executable\n"), progname);
@@ -1284,7 +1283,7 @@ pgwin32_CommandLine(bool registration)
12841283
else
12851284
{
12861285
ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1287-
cmdLine);
1286+
cmdPath);
12881287
if (ret != 0)
12891288
{
12901289
write_stderr(_("%s: could not find postgres program executable\n"), progname);
@@ -1294,54 +1293,57 @@ pgwin32_CommandLine(bool registration)
12941293

12951294
#ifdef __CYGWIN__
12961295
/* need to convert to windows path */
1296+
{
1297+
char buf[MAXPGPATH];
1298+
12971299
#if CYGWIN_VERSION_DLL_MAJOR >= 1007
1298-
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdLine, buf, sizeof(buf));
1300+
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdPath, buf, sizeof(buf));
12991301
#else
1300-
cygwin_conv_to_full_win32_path(cmdLine, buf);
1302+
cygwin_conv_to_full_win32_path(cmdPath, buf);
13011303
#endif
1302-
strcpy(cmdLine, buf);
1304+
strcpy(cmdPath, buf);
1305+
}
13031306
#endif
13041307

1308+
/* if path does not end in .exe, append it */
1309+
if (strlen(cmdPath) < 4 ||
1310+
pg_strcasecmp(cmdPath + strlen(cmdPath) - 4, ".exe") != 0)
1311+
snprintf(cmdPath + strlen(cmdPath), sizeof(cmdPath) - strlen(cmdPath),
1312+
".exe");
1313+
1314+
/* use backslashes in path to avoid problems with some third-party tools */
1315+
make_native_path(cmdPath);
1316+
1317+
/* be sure to double-quote the executable's name in the command */
1318+
appendPQExpBuffer(cmdLine, "\"%s\"", cmdPath);
1319+
1320+
/* append assorted switches to the command line, as needed */
1321+
13051322
if (registration)
1306-
{
1307-
if (pg_strcasecmp(cmdLine + strlen(cmdLine) - 4, ".exe") != 0)
1308-
{
1309-
/* If commandline does not end in .exe, append it */
1310-
strcat(cmdLine, ".exe");
1311-
}
1312-
strcat(cmdLine, " runservice -N \"");
1313-
strcat(cmdLine, register_servicename);
1314-
strcat(cmdLine, "\"");
1315-
}
1323+
appendPQExpBuffer(cmdLine, " runservice -N \"%s\"",
1324+
register_servicename);
13161325

13171326
if (pg_config)
1318-
{
1319-
strcat(cmdLine, " -D \"");
1320-
strcat(cmdLine, pg_config);
1321-
strcat(cmdLine, "\"");
1322-
}
1327+
appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_config);
13231328

13241329
if (registration && do_wait)
1325-
strcat(cmdLine, " -w");
1330+
appendPQExpBuffer(cmdLine, " -w");
13261331

13271332
if (registration && wait_seconds != DEFAULT_WAIT)
1328-
/* concatenate */
1329-
sprintf(cmdLine + strlen(cmdLine), " -t %d", wait_seconds);
1333+
appendPQExpBuffer(cmdLine, " -t %d", wait_seconds);
13301334

13311335
if (registration && silent_mode)
1332-
strcat(cmdLine, " -s");
1336+
appendPQExpBuffer(cmdLine, " -s");
13331337

13341338
if (post_opts)
13351339
{
1336-
strcat(cmdLine, " ");
1337-
if (registration)
1338-
strcat(cmdLine, " -o \"");
1339-
strcat(cmdLine, post_opts);
13401340
if (registration)
1341-
strcat(cmdLine, "\"");
1341+
appendPQExpBuffer(cmdLine, " -o \"%s\"", post_opts);
1342+
else
1343+
appendPQExpBuffer(cmdLine, " %s", post_opts);
13421344
}
13431345

1344-
return cmdLine;
1346+
return cmdLine->data;
13451347
}
13461348

13471349
static void
@@ -1772,7 +1774,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
17721774
*/
17731775
return r;
17741776
}
1775-
#endif
1777+
#endif /* defined(WIN32) || defined(__CYGWIN__) */
17761778

17771779
static void
17781780
do_advice(void)

0 commit comments

Comments
 (0)