aboutsummaryrefslogtreecommitdiff
path: root/src/fe_utils/string_utils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2016-08-20 15:05:25 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2016-08-20 15:05:25 -0400
commita00c58314745772f6c6a49b6d02a9572cd600bda (patch)
tree62c989a7460f4088ebcca979f20db9549c126e8c /src/fe_utils/string_utils.c
parent6471045230f5d891ad724c54d406e2214f3c96d9 (diff)
downloadpostgresql-a00c58314745772f6c6a49b6d02a9572cd600bda.tar.gz
postgresql-a00c58314745772f6c6a49b6d02a9572cd600bda.zip
Make initdb's suggested "pg_ctl start" command line more reliable.
The original coding here was not nearly careful enough about quoting special characters, and it didn't get corner cases right for constructing the pg_ctl path either. Use join_path_components() and appendShellString() to do it honestly, so that the string will more likely work if blindly copied-and-pasted. While at it, teach appendShellString() not to quote strings that clearly don't need it, so that the output from initdb doesn't become uglier than it was before in typical cases where quoting is not needed. Ryan Murphy, reviewed by Michael Paquier and myself Discussion: <CAHeEsBeAe1FeBypT3E8R1ZVZU0e8xv3A-7BHg6bEOi=jZny2Uw@mail.gmail.com>
Diffstat (limited to 'src/fe_utils/string_utils.c')
-rw-r--r--src/fe_utils/string_utils.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/fe_utils/string_utils.c b/src/fe_utils/string_utils.c
index 2c566b1ad75..edbc869e453 100644
--- a/src/fe_utils/string_utils.c
+++ b/src/fe_utils/string_utils.c
@@ -418,7 +418,7 @@ appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length,
/*
* Append the given string to the shell command being built in the buffer,
- * with suitable shell-style quoting to create exactly one argument.
+ * with shell-style quoting as needed to create exactly one argument.
*
* Forbid LF or CR characters, which have scant practical use beyond designing
* security breaches. The Windows command shell is unusable as a conduit for
@@ -429,8 +429,22 @@ appendByteaLiteral(PQExpBuffer buf, const unsigned char *str, size_t length,
void
appendShellString(PQExpBuffer buf, const char *str)
{
+#ifdef WIN32
+ int backslash_run_length = 0;
+#endif
const char *p;
+ /*
+ * Don't bother with adding quotes if the string is nonempty and clearly
+ * contains only safe characters.
+ */
+ if (*str != '\0' &&
+ strspn(str, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_./:") == strlen(str))
+ {
+ appendPQExpBufferStr(buf, str);
+ return;
+ }
+
#ifndef WIN32
appendPQExpBufferChar(buf, '\'');
for (p = str; *p; p++)
@@ -450,7 +464,6 @@ appendShellString(PQExpBuffer buf, const char *str)
}
appendPQExpBufferChar(buf, '\'');
#else /* WIN32 */
- int backslash_run_length = 0;
/*
* A Windows system() argument experiences two layers of interpretation.