@@ -1656,7 +1656,7 @@ dumpDatabases(PGconn *conn)
1656
1656
static int
1657
1657
runPgDump (const char * dbname )
1658
1658
{
1659
- PQExpBuffer connstr = createPQExpBuffer ();
1659
+ PQExpBuffer connstrbuf = createPQExpBuffer ();
1660
1660
PQExpBuffer cmd = createPQExpBuffer ();
1661
1661
int ret ;
1662
1662
@@ -1678,11 +1678,10 @@ runPgDump(const char *dbname)
1678
1678
* database name as is, but if it contains any = characters, it would
1679
1679
* incorrectly treat it as a connection string.
1680
1680
*/
1681
- appendPQExpBuffer (connstr , "dbname='" );
1682
- doConnStrQuoting (connstr , dbname );
1683
- appendPQExpBuffer (connstr , "'" );
1681
+ appendPQExpBufferStr (connstrbuf , "dbname=" );
1682
+ doConnStrQuoting (connstrbuf , dbname );
1684
1683
1685
- doShellQuoting (cmd , connstr -> data );
1684
+ doShellQuoting (cmd , connstrbuf -> data );
1686
1685
1687
1686
appendPQExpBuffer (cmd , "%s" , SYSTEMQUOTE );
1688
1687
@@ -1695,7 +1694,7 @@ runPgDump(const char *dbname)
1695
1694
ret = system (cmd -> data );
1696
1695
1697
1696
destroyPQExpBuffer (cmd );
1698
- destroyPQExpBuffer (connstr );
1697
+ destroyPQExpBuffer (connstrbuf );
1699
1698
1700
1699
return ret ;
1701
1700
}
@@ -1943,15 +1942,40 @@ dumpTimestamp(char *msg)
1943
1942
static void
1944
1943
doConnStrQuoting (PQExpBuffer buf , const char * str )
1945
1944
{
1946
- while (* str )
1945
+ const char * s ;
1946
+ bool needquotes ;
1947
+
1948
+ /*
1949
+ * If the string consists entirely of plain ASCII characters, no need to
1950
+ * quote it. This is quite conservative, but better safe than sorry.
1951
+ */
1952
+ needquotes = false;
1953
+ for (s = str ; * s ; s ++ )
1954
+ {
1955
+ if (!((* s >= 'a' && * s <= 'z' ) || (* s >= 'A' && * s <= 'Z' ) ||
1956
+ (* s >= '0' && * s <= '9' ) || * s == '_' || * s == '.' ))
1957
+ {
1958
+ needquotes = true;
1959
+ break ;
1960
+ }
1961
+ }
1962
+
1963
+ if (needquotes )
1947
1964
{
1948
- /* ' and \ must be escaped by to \' and \\ */
1949
- if (* str == '\'' || * str == '\\' )
1950
- appendPQExpBufferChar (buf , '\\' );
1965
+ appendPQExpBufferChar (buf , '\'' );
1966
+ while (* str )
1967
+ {
1968
+ /* ' and \ must be escaped by to \' and \\ */
1969
+ if (* str == '\'' || * str == '\\' )
1970
+ appendPQExpBufferChar (buf , '\\' );
1951
1971
1952
- appendPQExpBufferChar (buf , * str );
1953
- str ++ ;
1972
+ appendPQExpBufferChar (buf , * str );
1973
+ str ++ ;
1974
+ }
1975
+ appendPQExpBufferChar (buf , '\'' );
1954
1976
}
1977
+ else
1978
+ appendPQExpBufferStr (buf , str );
1955
1979
}
1956
1980
1957
1981
/*
0 commit comments