Fix up hack to suppress escape_string_warning so that it actually works

and there's only one place that's a kluge, ie, appendStringLiteralConn.
Note that pg_dump itself doesn't use appendStringLiteralConn, so its
behavior is not affected; only the other utility programs care.
This commit is contained in:
Tom Lane 2006-06-01 00:15:36 +00:00
parent 2703007501
commit 6178762fcf
7 changed files with 61 additions and 50 deletions

View File

@ -7,7 +7,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/pg_dump/dumputils.c,v 1.29 2006/05/28 21:13:54 tgl Exp $ * $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.30 2006/06/01 00:15:36 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -191,6 +191,21 @@ appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
{ {
size_t length = strlen(str); size_t length = strlen(str);
/*
* XXX This is a kluge to silence escape_string_warning in our utility
* programs. It should go away someday.
*/
if (strchr(str, '\\') != NULL && PQserverVersion(conn) >= 80100)
{
/* ensure we are not adjacent to an identifier */
if (buf->len > 0 && buf->data[buf->len-1] != ' ')
appendPQExpBufferChar(buf, ' ');
appendPQExpBufferChar(buf, ESCAPE_STRING_SYNTAX);
appendStringLiteral(buf, str, PQclientEncoding(conn), false);
return;
}
/* XXX end kluge */
if (!enlargePQExpBuffer(buf, 2 * length + 2)) if (!enlargePQExpBuffer(buf, 2 * length + 2))
return; return;
appendPQExpBufferChar(buf, '\''); appendPQExpBufferChar(buf, '\'');

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.167 2006/05/31 11:02:42 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.168 2006/06/01 00:15:36 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "command.h" #include "command.h"
@ -681,9 +681,9 @@ exec_command(const char *cmd,
PGresult *res; PGresult *res;
initPQExpBuffer(&buf); initPQExpBuffer(&buf);
printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD %c'%s';", printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
fmtId(user), NEED_E_STR(encrypted_password), fmtId(user));
encrypted_password); appendStringLiteralConn(&buf, encrypted_password, pset.db);
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.48 2006/05/31 11:02:42 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.49 2006/06/01 00:15:36 tgl Exp $
*/ */
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
@ -22,12 +22,6 @@
#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.62 2006/05/31 11:02:42 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.63 2006/06/01 00:15:36 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "copy.h" #include "copy.h"
@ -19,6 +19,7 @@
#include "libpq-fe.h" #include "libpq-fe.h"
#include "pqexpbuffer.h" #include "pqexpbuffer.h"
#include "pqsignal.h" #include "pqsignal.h"
#include "dumputils.h"
#include "settings.h" #include "settings.h"
#include "common.h" #include "common.h"
@ -113,6 +114,7 @@ parse_slash_copy(const char *args)
char *line; char *line;
char *token; char *token;
const char *whitespace = " \t\n\r"; const char *whitespace = " \t\n\r";
char nonstd_backslash = standard_strings() ? 0 : '\\';
if (args) if (args)
line = pg_strdup(args); line = pg_strdup(args);
@ -216,7 +218,7 @@ parse_slash_copy(const char *args)
goto error; goto error;
token = strtokx(NULL, whitespace, NULL, "'", token = strtokx(NULL, whitespace, NULL, "'",
standard_strings() ? 0 : '\\', true, pset.encoding); nonstd_backslash, true, pset.encoding);
if (!token) if (!token)
goto error; goto error;
@ -255,7 +257,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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, false, pset.encoding);
if (!token) if (!token)
goto error; goto error;
result->delim = pg_strdup(token); result->delim = pg_strdup(token);
@ -290,10 +292,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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, 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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, false, pset.encoding);
if (token) if (token)
result->delim = pg_strdup(token); result->delim = pg_strdup(token);
else else
@ -302,10 +304,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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, 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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, false, pset.encoding);
if (token) if (token)
result->null = pg_strdup(token); result->null = pg_strdup(token);
else else
@ -314,10 +316,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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, 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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, false, pset.encoding);
if (token) if (token)
result->quote = pg_strdup(token); result->quote = pg_strdup(token);
else else
@ -326,10 +328,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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, 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, "'",
standard_strings() ? 0 : '\\', false, pset.encoding); nonstd_backslash, false, pset.encoding);
if (token) if (token)
result->escape = pg_strdup(token); result->escape = pg_strdup(token);
else else
@ -461,23 +463,27 @@ do_copy(const char *args)
/* Uses old COPY syntax for backward compatibility 2002-06-19 */ /* Uses old COPY syntax for backward compatibility 2002-06-19 */
if (options->delim) if (options->delim)
{ {
/* if user gave a quoted string, use it as-is */
if (options->delim[0] == '\'') if (options->delim[0] == '\'')
appendPQExpBuffer(&query, " USING DELIMITERS %c%s", appendPQExpBuffer(&query, " USING DELIMITERS %s", options->delim);
NEED_E_STR(options->delim), options->delim);
else else
appendPQExpBuffer(&query, " USING DELIMITERS %c'%s'", {
NEED_E_STR(options->delim), options->delim); appendPQExpBuffer(&query, " USING DELIMITERS ");
appendStringLiteralConn(&query, options->delim, pset.db);
}
} }
/* There is no backward-compatible CSV syntax */ /* There is no backward-compatible CSV syntax */
if (options->null) if (options->null)
{ {
/* if user gave a quoted string, use it as-is */
if (options->null[0] == '\'') if (options->null[0] == '\'')
appendPQExpBuffer(&query, " WITH NULL AS %c%s", appendPQExpBuffer(&query, " WITH NULL AS %s", options->null);
NEED_E_STR(options->null), options->null);
else else
appendPQExpBuffer(&query, " WITH NULL AS %c'%s'", {
NEED_E_STR(options->null), options->null); appendPQExpBuffer(&query, " WITH NULL AS ");
appendStringLiteralConn(&query, options->null, pset.db);
}
} }
if (options->csv_mode) if (options->csv_mode)
@ -488,22 +494,26 @@ do_copy(const char *args)
if (options->quote) if (options->quote)
{ {
/* if user gave a quoted string, use it as-is */
if (options->quote[0] == '\'') if (options->quote[0] == '\'')
appendPQExpBuffer(&query, " QUOTE AS %c%s", appendPQExpBuffer(&query, " QUOTE AS %s", options->quote);
NEED_E_STR(options->quote), options->quote);
else else
appendPQExpBuffer(&query, " QUOTE AS %c'%s'", {
NEED_E_STR(options->quote), options->quote); appendPQExpBuffer(&query, " QUOTE AS ");
appendStringLiteralConn(&query, options->quote, pset.db);
}
} }
if (options->escape) if (options->escape)
{ {
/* if user gave a quoted string, use it as-is */
if (options->escape[0] == '\'') if (options->escape[0] == '\'')
appendPQExpBuffer(&query, " ESCAPE AS %c%s", appendPQExpBuffer(&query, " ESCAPE AS %s", options->escape);
NEED_E_STR(options->escape), options->escape);
else else
appendPQExpBuffer(&query, " ESCAPE AS %c'%s'", {
NEED_E_STR(options->escape), options->escape); appendPQExpBuffer(&query, " ESCAPE AS ");
appendStringLiteralConn(&query, options->escape, pset.db);
}
} }
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.138 2006/05/31 11:02:42 momjian Exp $ * $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.139 2006/06/01 00:15:36 tgl Exp $
*/ */
#include "postgres_fe.h" #include "postgres_fe.h"
#include "describe.h" #include "describe.h"
@ -1907,17 +1907,14 @@ 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');
} }
@ -1941,7 +1938,6 @@ 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.20 2006/05/31 11:02:42 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/createdb.c,v 1.21 2006/06/01 00:15:36 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -185,8 +185,6 @@ 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.31 2006/05/31 11:02:42 momjian Exp $ * $PostgreSQL: pgsql/src/bin/scripts/createuser.c,v 1.32 2006/06/01 00:15:36 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -243,8 +243,6 @@ 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)