Escape processing patch:

o  turns off escape_string_warning in pg_dumpall.c
        o  optionally use E'' for \password (undocumented option?)
        o  honor standard_conforming-strings for \copy (but not
           support literal E'' strings)
        o  optionally use E'' for \d commands
        o  turn off escape_string_warning for createdb, createuser,
           droplang
This commit is contained in:
Bruce Momjian 2006-05-31 11:02:42 +00:00
parent 751d985805
commit eaca1175e9
8 changed files with 54 additions and 32 deletions

View File

@ -6,7 +6,7 @@
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* *
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.77 2006/05/28 21:13:54 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.78 2006/05/31 11:02:42 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -338,6 +338,8 @@ main(int argc, char *argv[])
printf("SET client_encoding = '%s';\n", printf("SET client_encoding = '%s';\n",
pg_encoding_to_char(encoding)); pg_encoding_to_char(encoding));
printf("SET standard_conforming_strings = %s;\n", std_strings); printf("SET standard_conforming_strings = %s;\n", std_strings);
if (strcmp(std_strings, "off") == 0)
printf("SET escape_string_warning = 'off';\n");
printf("\n"); printf("\n");
/* Dump roles (users) */ /* Dump roles (users) */

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.166 2006/04/02 20:08:22 neilc Exp $ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.167 2006/05/31 11:02:42 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "command.h" #include "command.h"
@ -681,8 +681,9 @@ exec_command(const char *cmd,
PGresult *res; PGresult *res;
initPQExpBuffer(&buf); initPQExpBuffer(&buf);
printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD '%s';", printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD %c'%s';",
fmtId(user), encrypted_password); fmtId(user), NEED_E_STR(encrypted_password),
encrypted_password);
res = PSQLexec(buf.data, false); res = PSQLexec(buf.data, false);
termPQExpBuffer(&buf); termPQExpBuffer(&buf);
if (!res) if (!res)

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.47 2006/03/06 19:49:20 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.48 2006/05/31 11:02:42 momjian Exp $
*/ */
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
@ -22,6 +22,12 @@
#define atooid(x) ((Oid) strtoul((x), NULL, 10)) #define atooid(x) ((Oid) strtoul((x), NULL, 10))
/*
* We use this to prefix strings with E'' that we know are already safe,
* so we don't get an escape_string_warning.
*/
#define NEED_E_STR(str) ((strchr(str, '\\') && !standard_strings()) ? ESCAPE_STRING_SYNTAX : ' ')
/* /*
* Safer versions of some standard C library functions. If an * Safer versions of some standard C library functions. If an
* out-of-memory condition occurs, these functions will bail out * out-of-memory condition occurs, these functions will bail out

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.61 2006/05/26 19:51:29 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.62 2006/05/31 11:02:42 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "copy.h" #include "copy.h"
@ -216,7 +216,7 @@ parse_slash_copy(const char *args)
goto error; goto error;
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', true, pset.encoding); standard_strings() ? 0 : '\\', true, pset.encoding);
if (!token) if (!token)
goto error; goto error;
@ -255,7 +255,7 @@ parse_slash_copy(const char *args)
if (token && pg_strcasecmp(token, "delimiters") == 0) if (token && pg_strcasecmp(token, "delimiters") == 0)
{ {
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (!token) if (!token)
goto error; goto error;
result->delim = pg_strdup(token); result->delim = pg_strdup(token);
@ -290,10 +290,10 @@ parse_slash_copy(const char *args)
else if (pg_strcasecmp(token, "delimiter") == 0) else if (pg_strcasecmp(token, "delimiter") == 0)
{ {
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token && pg_strcasecmp(token, "as") == 0) if (token && pg_strcasecmp(token, "as") == 0)
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token) if (token)
result->delim = pg_strdup(token); result->delim = pg_strdup(token);
else else
@ -302,10 +302,10 @@ parse_slash_copy(const char *args)
else if (pg_strcasecmp(token, "null") == 0) else if (pg_strcasecmp(token, "null") == 0)
{ {
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token && pg_strcasecmp(token, "as") == 0) if (token && pg_strcasecmp(token, "as") == 0)
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token) if (token)
result->null = pg_strdup(token); result->null = pg_strdup(token);
else else
@ -314,10 +314,10 @@ parse_slash_copy(const char *args)
else if (pg_strcasecmp(token, "quote") == 0) else if (pg_strcasecmp(token, "quote") == 0)
{ {
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token && pg_strcasecmp(token, "as") == 0) if (token && pg_strcasecmp(token, "as") == 0)
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token) if (token)
result->quote = pg_strdup(token); result->quote = pg_strdup(token);
else else
@ -326,10 +326,10 @@ parse_slash_copy(const char *args)
else if (pg_strcasecmp(token, "escape") == 0) else if (pg_strcasecmp(token, "escape") == 0)
{ {
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token && pg_strcasecmp(token, "as") == 0) if (token && pg_strcasecmp(token, "as") == 0)
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
'\\', false, pset.encoding); standard_strings() ? 0 : '\\', false, pset.encoding);
if (token) if (token)
result->escape = pg_strdup(token); result->escape = pg_strdup(token);
else else
@ -462,20 +462,22 @@ do_copy(const char *args)
if (options->delim) if (options->delim)
{ {
if (options->delim[0] == '\'') if (options->delim[0] == '\'')
appendPQExpBuffer(&query, " USING DELIMITERS %s", appendPQExpBuffer(&query, " USING DELIMITERS %c%s",
options->delim); NEED_E_STR(options->delim), options->delim);
else else
appendPQExpBuffer(&query, " USING DELIMITERS '%s'", appendPQExpBuffer(&query, " USING DELIMITERS %c'%s'",
options->delim); NEED_E_STR(options->delim), options->delim);
} }
/* There is no backward-compatible CSV syntax */ /* There is no backward-compatible CSV syntax */
if (options->null) if (options->null)
{ {
if (options->null[0] == '\'') if (options->null[0] == '\'')
appendPQExpBuffer(&query, " WITH NULL AS %s", options->null); appendPQExpBuffer(&query, " WITH NULL AS %c%s",
NEED_E_STR(options->null), options->null);
else else
appendPQExpBuffer(&query, " WITH NULL AS '%s'", options->null); appendPQExpBuffer(&query, " WITH NULL AS %c'%s'",
NEED_E_STR(options->null), options->null);
} }
if (options->csv_mode) if (options->csv_mode)
@ -487,17 +489,21 @@ do_copy(const char *args)
if (options->quote) if (options->quote)
{ {
if (options->quote[0] == '\'') if (options->quote[0] == '\'')
appendPQExpBuffer(&query, " QUOTE AS %s", options->quote); appendPQExpBuffer(&query, " QUOTE AS %c%s",
NEED_E_STR(options->quote), options->quote);
else else
appendPQExpBuffer(&query, " QUOTE AS '%s'", options->quote); appendPQExpBuffer(&query, " QUOTE AS %c'%s'",
NEED_E_STR(options->quote), options->quote);
} }
if (options->escape) if (options->escape)
{ {
if (options->escape[0] == '\'') if (options->escape[0] == '\'')
appendPQExpBuffer(&query, " ESCAPE AS %s", options->escape); appendPQExpBuffer(&query, " ESCAPE AS %c%s",
NEED_E_STR(options->escape), options->escape);
else else
appendPQExpBuffer(&query, " ESCAPE AS '%s'", options->escape); appendPQExpBuffer(&query, " ESCAPE AS %c'%s'",
NEED_E_STR(options->escape), options->escape);
} }
if (options->force_quote_list) if (options->force_quote_list)

View File

@ -3,7 +3,7 @@
* *
* Copyright (c) 2000-2006, PostgreSQL Global Development Group * Copyright (c) 2000-2006, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.137 2006/05/28 21:13:54 tgl Exp $ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.138 2006/05/31 11:02:42 momjian Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "describe.h" #include "describe.h"
@ -1907,14 +1907,17 @@ processNamePattern(PQExpBuffer buf, const char *pattern,
if (altnamevar) if (altnamevar)
{ {
appendPQExpBuffer(buf, "(%s ~ ", namevar); appendPQExpBuffer(buf, "(%s ~ ", namevar);
appendPQExpBufferChar(buf, NEED_E_STR(namebuf.data));
appendStringLiteralConn(buf, namebuf.data, pset.db); appendStringLiteralConn(buf, namebuf.data, pset.db);
appendPQExpBuffer(buf, "\n OR %s ~ ", altnamevar); appendPQExpBuffer(buf, "\n OR %s ~ ", altnamevar);
appendPQExpBufferChar(buf, NEED_E_STR(namebuf.data));
appendStringLiteralConn(buf, namebuf.data, pset.db); appendStringLiteralConn(buf, namebuf.data, pset.db);
appendPQExpBuffer(buf, ")\n"); appendPQExpBuffer(buf, ")\n");
} }
else else
{ {
appendPQExpBuffer(buf, "%s ~ ", namevar); appendPQExpBuffer(buf, "%s ~ ", namevar);
appendPQExpBufferChar(buf, NEED_E_STR(namebuf.data));
appendStringLiteralConn(buf, namebuf.data, pset.db); appendStringLiteralConn(buf, namebuf.data, pset.db);
appendPQExpBufferChar(buf, '\n'); appendPQExpBufferChar(buf, '\n');
} }
@ -1938,6 +1941,7 @@ processNamePattern(PQExpBuffer buf, const char *pattern,
{ {
WHEREAND(); WHEREAND();
appendPQExpBuffer(buf, "%s ~ ", schemavar); appendPQExpBuffer(buf, "%s ~ ", schemavar);
appendPQExpBufferChar(buf, NEED_E_STR(schemabuf.data));
appendStringLiteralConn(buf, schemabuf.data, pset.db); appendStringLiteralConn(buf, schemabuf.data, pset.db);
appendPQExpBufferChar(buf, '\n'); appendPQExpBufferChar(buf, '\n');
} }

View File

@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.19 2006/05/29 19:52:46 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.20 2006/05/31 11:02:42 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -185,6 +185,8 @@ main(int argc, char *argv[])
{ {
conn = connectDatabase(dbname, host, port, username, password, progname); conn = connectDatabase(dbname, host, port, username, password, progname);
executeCommand(conn, "SET escape_string_warning TO 'off'", progname, false);
printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname)); printfPQExpBuffer(&sql, "COMMENT ON DATABASE %s IS ", fmtId(dbname));
appendStringLiteralConn(&sql, comment, conn); appendStringLiteralConn(&sql, comment, conn);
appendPQExpBuffer(&sql, ";\n"); appendPQExpBuffer(&sql, ";\n");

View File

@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.30 2006/05/29 19:52:46 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.31 2006/05/31 11:02:42 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -243,6 +243,8 @@ main(int argc, char *argv[])
printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser)); printfPQExpBuffer(&sql, "CREATE ROLE %s", fmtId(newuser));
if (newpassword) if (newpassword)
{ {
executeCommand(conn, "SET escape_string_warning TO 'off'", progname, false);
if (encrypted == TRI_YES) if (encrypted == TRI_YES)
appendPQExpBuffer(&sql, " ENCRYPTED"); appendPQExpBuffer(&sql, " ENCRYPTED");
if (encrypted == TRI_NO) if (encrypted == TRI_NO)

View File

@ -5,7 +5,7 @@
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/bin/scripts/droplang.c,v 1.20 2006/05/29 19:52:46 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/droplang.c,v 1.21 2006/05/31 11:02:42 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -176,8 +176,7 @@ main(int argc, char *argv[])
* Force schema search path to be just pg_catalog, so that we don't have * Force schema search path to be just pg_catalog, so that we don't have
* to be paranoid about search paths below. * to be paranoid about search paths below.
*/ */
executeCommand(conn, "SET search_path = pg_catalog;", executeCommand(conn, "SET search_path = pg_catalog;", progname, echo);
progname, echo);
/* /*
* Make sure the language is installed and find the OIDs of the handler * Make sure the language is installed and find the OIDs of the handler