Skip to content

Commit 2953cd6

Browse files
committed
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.
1 parent 3dee636 commit 2953cd6

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/bin/pg_dump/pg_dumpall.c

+31-6
Original file line numberDiff line numberDiff line change
@@ -2038,15 +2038,40 @@ dumpTimestamp(char *msg)
20382038
static void
20392039
doConnStrQuoting(PQExpBuffer buf, const char *str)
20402040
{
2041-
while (*str)
2041+
const char *s;
2042+
bool needquotes;
2043+
2044+
/*
2045+
* If the string consists entirely of plain ASCII characters, no need to
2046+
* quote it. This is quite conservative, but better safe than sorry.
2047+
*/
2048+
needquotes = false;
2049+
for (s = str; *s; s++)
2050+
{
2051+
if (!((*s >= 'a' && *s <= 'z') || (*s >= 'A' && *s <= 'Z') ||
2052+
(*s >= '0' && *s <= '9') || *s == '_' || *s == '.'))
2053+
{
2054+
needquotes = true;
2055+
break;
2056+
}
2057+
}
2058+
2059+
if (needquotes)
20422060
{
2043-
/* ' and \ must be escaped by to \' and \\ */
2044-
if (*str == '\'' || *str == '\\')
2045-
appendPQExpBufferChar(buf, '\\');
2061+
appendPQExpBufferChar(buf, '\'');
2062+
while (*str)
2063+
{
2064+
/* ' and \ must be escaped by to \' and \\ */
2065+
if (*str == '\'' || *str == '\\')
2066+
appendPQExpBufferChar(buf, '\\');
20462067

2047-
appendPQExpBufferChar(buf, *str);
2048-
str++;
2068+
appendPQExpBufferChar(buf, *str);
2069+
str++;
2070+
}
2071+
appendPQExpBufferChar(buf, '\'');
20492072
}
2073+
else
2074+
appendPQExpBufferStr(buf, str);
20502075
}
20512076

20522077
/*

0 commit comments

Comments
 (0)