Commit 2953cd6d authored by Heikki Linnakangas's avatar Heikki Linnakangas

Only quote libpq connection string values that need quoting.

There's no harm in excessive quoting per se, but it makes the strings nicer
to read. The values can get quite unwieldy, when they're first quoted within
within single-quotes when included in the connection string, and then all
the single-quotes are escaped when the connection string is passed as a
shell argument.
parent 3dee636e
...@@ -2038,6 +2038,27 @@ dumpTimestamp(char *msg) ...@@ -2038,6 +2038,27 @@ dumpTimestamp(char *msg)
static void static void
doConnStrQuoting(PQExpBuffer buf, const char *str) doConnStrQuoting(PQExpBuffer buf, const char *str)
{ {
const char *s;
bool needquotes;
/*
* If the string consists entirely of plain ASCII characters, no need to
* quote it. This is quite conservative, but better safe than sorry.
*/
needquotes = false;
for (s = str; *s; s++)
{
if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
(*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
{
needquotes = true;
break;
}
}
if (needquotes)
{
appendPQExpBufferChar(buf, '\'');
while (*str) while (*str)
{ {
/* ' and \ must be escaped by to \' and \\ */ /* ' and \ must be escaped by to \' and \\ */
...@@ -2047,6 +2068,10 @@ doConnStrQuoting(PQExpBuffer buf, const char *str) ...@@ -2047,6 +2068,10 @@ doConnStrQuoting(PQExpBuffer buf, const char *str)
appendPQExpBufferChar(buf, *str); appendPQExpBufferChar(buf, *str);
str++; str++;
} }
appendPQExpBufferChar(buf, '\'');
}
else
appendPQExpBufferStr(buf, str);
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment