Skip to content

Commit 4ab4e5c

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 ba63799 commit 4ab4e5c

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>
@@ -1106,16 +1108,13 @@ pgwin32_IsInstalled(SC_HANDLE hSCM)
11061108
static char *
11071109
pgwin32_CommandLine(bool registration)
11081110
{
1109-
static char cmdLine[MAXPGPATH];
1111+
PQExpBuffer cmdLine = createPQExpBuffer();
1112+
char cmdPath[MAXPGPATH];
11101113
int ret;
11111114

1112-
#ifdef __CYGWIN__
1113-
char buf[MAXPGPATH];
1114-
#endif
1115-
11161115
if (registration)
11171116
{
1118-
ret = find_my_exec(argv0, cmdLine);
1117+
ret = find_my_exec(argv0, cmdPath);
11191118
if (ret != 0)
11201119
{
11211120
write_stderr(_("%s: could not find own program executable\n"), progname);
@@ -1125,7 +1124,7 @@ pgwin32_CommandLine(bool registration)
11251124
else
11261125
{
11271126
ret = find_other_exec(argv0, "postgres", PG_BACKEND_VERSIONSTR,
1128-
cmdLine);
1127+
cmdPath);
11291128
if (ret != 0)
11301129
{
11311130
write_stderr(_("%s: could not find postgres program executable\n"), progname);
@@ -1135,54 +1134,57 @@ pgwin32_CommandLine(bool registration)
11351134

11361135
#ifdef __CYGWIN__
11371136
/* need to convert to windows path */
1137+
{
1138+
char buf[MAXPGPATH];
1139+
11381140
#if CYGWIN_VERSION_DLL_MAJOR >= 1007
1139-
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdLine, buf, sizeof(buf));
1141+
cygwin_conv_path(CCP_POSIX_TO_WIN_A, cmdPath, buf, sizeof(buf));
11401142
#else
1141-
cygwin_conv_to_full_win32_path(cmdLine, buf);
1143+
cygwin_conv_to_full_win32_path(cmdPath, buf);
11421144
#endif
1143-
strcpy(cmdLine, buf);
1145+
strcpy(cmdPath, buf);
1146+
}
11441147
#endif
11451148

1149+
/* if path does not end in .exe, append it */
1150+
if (strlen(cmdPath) < 4 ||
1151+
pg_strcasecmp(cmdPath + strlen(cmdPath) - 4, ".exe") != 0)
1152+
snprintf(cmdPath + strlen(cmdPath), sizeof(cmdPath) - strlen(cmdPath),
1153+
".exe");
1154+
1155+
/* use backslashes in path to avoid problems with some third-party tools */
1156+
make_native_path(cmdPath);
1157+
1158+
/* be sure to double-quote the executable's name in the command */
1159+
appendPQExpBuffer(cmdLine, "\"%s\"", cmdPath);
1160+
1161+
/* append assorted switches to the command line, as needed */
1162+
11461163
if (registration)
1147-
{
1148-
if (pg_strcasecmp(cmdLine + strlen(cmdLine) - 4, ".exe"))
1149-
{
1150-
/* If commandline does not end in .exe, append it */
1151-
strcat(cmdLine, ".exe");
1152-
}
1153-
strcat(cmdLine, " runservice -N \"");
1154-
strcat(cmdLine, register_servicename);
1155-
strcat(cmdLine, "\"");
1156-
}
1164+
appendPQExpBuffer(cmdLine, " runservice -N \"%s\"",
1165+
register_servicename);
11571166

11581167
if (pg_data)
1159-
{
1160-
strcat(cmdLine, " -D \"");
1161-
strcat(cmdLine, pg_data);
1162-
strcat(cmdLine, "\"");
1163-
}
1168+
appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_data);
11641169

11651170
if (registration && do_wait)
1166-
strcat(cmdLine, " -w");
1171+
appendPQExpBuffer(cmdLine, " -w");
11671172

11681173
if (registration && wait_seconds != DEFAULT_WAIT)
1169-
/* concatenate */
1170-
sprintf(cmdLine + strlen(cmdLine), " -t %d", wait_seconds);
1174+
appendPQExpBuffer(cmdLine, " -t %d", wait_seconds);
11711175

11721176
if (registration && silent_mode)
1173-
strcat(cmdLine, " -s");
1177+
appendPQExpBuffer(cmdLine, " -s");
11741178

11751179
if (post_opts)
11761180
{
1177-
strcat(cmdLine, " ");
1178-
if (registration)
1179-
strcat(cmdLine, " -o \"");
1180-
strcat(cmdLine, post_opts);
11811181
if (registration)
1182-
strcat(cmdLine, "\"");
1182+
appendPQExpBuffer(cmdLine, " -o \"%s\"", post_opts);
1183+
else
1184+
appendPQExpBuffer(cmdLine, " %s", post_opts);
11831185
}
11841186

1185-
return cmdLine;
1187+
return cmdLine->data;
11861188
}
11871189

11881190
static void
@@ -1618,7 +1620,7 @@ CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_ser
16181620
*/
16191621
return r;
16201622
}
1621-
#endif
1623+
#endif /* defined(WIN32) || defined(__CYGWIN__) */
16221624

16231625
static void
16241626
do_advice(void)

0 commit comments

Comments
 (0)