diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2013-02-25 19:53:04 +0200 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2013-02-25 19:53:04 +0200 |
commit | 2953cd6d17210935098c803c52c6df5b12a725b9 (patch) | |
tree | a9b1ebbfe0bf1db2365de90f92f52eaf8998c610 | |
parent | 3dee636e0404885d07885d41c0d70e50c784f324 (diff) | |
download | postgresql-2953cd6d17210935098c803c52c6df5b12a725b9.tar.gz postgresql-2953cd6d17210935098c803c52c6df5b12a725b9.zip |
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.
-rw-r--r-- | src/bin/pg_dump/pg_dumpall.c | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index ac3f57e5fe1..5488021bcae 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -2038,15 +2038,40 @@ dumpTimestamp(char *msg) static void doConnStrQuoting(PQExpBuffer buf, const char *str) { - while (*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) { - /* ' and \ must be escaped by to \' and \\ */ - if (*str == '\'' || *str == '\\') - appendPQExpBufferChar(buf, '\\'); + appendPQExpBufferChar(buf, '\''); + while (*str) + { + /* ' and \ must be escaped by to \' and \\ */ + if (*str == '\'' || *str == '\\') + appendPQExpBufferChar(buf, '\\'); - appendPQExpBufferChar(buf, *str); - str++; + appendPQExpBufferChar(buf, *str); + str++; + } + appendPQExpBufferChar(buf, '\''); } + else + appendPQExpBufferStr(buf, str); } /* |