Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 74b49a8

Browse files
committed
Add E'' to internally created SQL strings that contain backslashes.
Improve code clarity by using macros for E'' processing.
1 parent 654efe6 commit 74b49a8

File tree

12 files changed

+91
-89
lines changed

12 files changed

+91
-89
lines changed

src/backend/utils/adt/quote.c

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.15 2005/03/21 16:29:20 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/quote.c,v 1.16 2005/07/02 17:01:50 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -60,19 +60,25 @@ quote_literal(PG_FUNCTION_ARGS)
6060

6161
len = VARSIZE(t) - VARHDRSZ;
6262
/* We make a worst-case result area; wasting a little space is OK */
63-
result = (text *) palloc(len * 2 + 2 + VARHDRSZ);
63+
result = (text *) palloc(len * 2 + 3 + VARHDRSZ);
6464

6565
cp1 = VARDATA(t);
6666
cp2 = VARDATA(result);
6767

68+
for(; len-- > 0; cp1++)
69+
if (*cp1 == '\\')
70+
{
71+
*cp2++ = ESCAPE_STRING_SYNTAX;
72+
break;
73+
}
74+
75+
len = VARSIZE(t) - VARHDRSZ;
76+
cp1 = VARDATA(t);
6877
*cp2++ = '\'';
6978
while (len-- > 0)
7079
{
71-
if (*cp1 == '\'')
72-
*cp2++ = '\'';
73-
else if (*cp1 == '\\')
74-
*cp2++ = '\\';
75-
80+
if (SQL_STR_DOUBLE(*cp1))
81+
*cp2++ = *cp1;
7682
*cp2++ = *cp1++;
7783
}
7884
*cp2++ = '\'';

src/backend/utils/adt/ruleutils.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.202 2005/06/28 05:09:01 tgl Exp $
6+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.203 2005/07/02 17:01:50 momjian Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -564,12 +564,14 @@ pg_get_triggerdef(PG_FUNCTION_ARGS)
564564
{
565565
if (i > 0)
566566
appendStringInfo(&buf, ", ");
567+
if (strchr(p, '\\') != NULL)
568+
appendStringInfoChar(&buf, ESCAPE_STRING_SYNTAX);
567569
appendStringInfoChar(&buf, '\'');
570+
568571
while (*p)
569572
{
570-
/* escape quotes and backslashes */
571-
if (*p == '\'' || *p == '\\')
572-
appendStringInfoChar(&buf, '\\');
573+
if (SQL_STR_DOUBLE(*p))
574+
appendStringInfoChar(&buf, *p);
573575
appendStringInfoChar(&buf, *p++);
574576
}
575577
p++;
@@ -3869,22 +3871,29 @@ get_const_expr(Const *constval, deparse_context *context)
38693871
break;
38703872

38713873
default:
3872-
38733874
/*
38743875
* We must quote any funny characters in the constant's
38753876
* representation. XXX Any MULTIBYTE considerations here?
38763877
*/
3878+
for (valptr = extval; *valptr; valptr++)
3879+
if (*valptr == '\\' ||
3880+
(unsigned char)*valptr < (unsigned char)' ')
3881+
{
3882+
appendStringInfoChar(buf, ESCAPE_STRING_SYNTAX);
3883+
break;
3884+
}
3885+
38773886
appendStringInfoChar(buf, '\'');
38783887
for (valptr = extval; *valptr; valptr++)
38793888
{
38803889
char ch = *valptr;
38813890

3882-
if (ch == '\'' || ch == '\\')
3891+
if (SQL_STR_DOUBLE(ch))
38833892
{
3884-
appendStringInfoChar(buf, '\\');
3893+
appendStringInfoChar(buf, ch);
38853894
appendStringInfoChar(buf, ch);
38863895
}
3887-
else if (((unsigned char) ch) < ((unsigned char) ' '))
3896+
else if ((unsigned char)ch < (unsigned char)' ')
38883897
appendStringInfo(buf, "\\%03o", (int) ch);
38893898
else
38903899
appendStringInfoChar(buf, ch);

src/bin/initdb/initdb.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* Portions Copyright (c) 1994, Regents of the University of California
4343
* Portions taken from FreeBSD.
4444
*
45-
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.89 2005/07/01 17:40:28 momjian Exp $
45+
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.90 2005/07/02 17:01:50 momjian Exp $
4646
*
4747
*-------------------------------------------------------------------------
4848
*/
@@ -1986,8 +1986,8 @@ escape_quotes(const char *src)
19861986

19871987
for (i = 0, j = 0; i < len; i++)
19881988
{
1989-
if (src[i] == '\\' || src[i] == '\'')
1990-
result[j++] = src[i]; /* double these */
1989+
if (SQL_STR_DOUBLE(src[i]))
1990+
result[j++] = src[i];
19911991
result[j++] = src[i];
19921992
}
19931993
result[j] = '\0';

src/bin/pg_dump/dumputils.c

+12-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.18 2005/07/01 21:03:25 momjian Exp $
10+
* $PostgreSQL: pgsql/src/bin/pg_dump/dumputils.c,v 1.19 2005/07/02 17:01:51 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -111,38 +111,33 @@ fmtId(const char *rawid)
111111
void
112112
appendStringLiteral(PQExpBuffer buf, const char *str, bool escapeAll)
113113
{
114-
bool has_escapes = false;
115-
const char *str2 = str;
114+
char ch;
115+
const char *p;
116116

117-
while (*str2)
117+
for (p = str; *p; p++)
118118
{
119-
char ch = *str2++;
120-
119+
ch = *p;
121120
if (ch == '\\' ||
122-
((unsigned char) ch < (unsigned char) ' ' &&
121+
((unsigned char)ch < (unsigned char)' ' &&
123122
(escapeAll ||
124123
(ch != '\t' && ch != '\n' && ch != '\v' &&
125124
ch != '\f' && ch != '\r'))))
126125
{
127-
has_escapes = true;
126+
appendPQExpBufferChar(buf, ESCAPE_STRING_SYNTAX);
128127
break;
129128
}
130129
}
131130

132-
if (has_escapes)
133-
appendPQExpBufferChar(buf, 'E');
134-
135131
appendPQExpBufferChar(buf, '\'');
136-
while (*str)
132+
for (p = str; *p; p++)
137133
{
138-
char ch = *str++;
139-
140-
if (ch == '\\' || ch == '\'')
134+
ch = *p;
135+
if (SQL_STR_DOUBLE(ch))
141136
{
142-
appendPQExpBufferChar(buf, ch); /* double these */
137+
appendPQExpBufferChar(buf, ch);
143138
appendPQExpBufferChar(buf, ch);
144139
}
145-
else if ((unsigned char) ch < (unsigned char) ' ' &&
140+
else if ((unsigned char)ch < (unsigned char)' ' &&
146141
(escapeAll ||
147142
(ch != '\t' && ch != '\n' && ch != '\v' &&
148143
ch != '\f' && ch != '\r')))

src/bin/pg_dump/pg_dump.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.412 2005/07/01 21:03:25 momjian Exp $
15+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.413 2005/07/02 17:01:51 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -7792,18 +7792,17 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
77927792
}
77937793
p--;
77947794

7795-
/* do we need E''? */
77967795
while (s2 < p)
77977796
if (*s2++ == '\\')
77987797
{
7799-
appendPQExpBufferChar(query, 'E');
7798+
appendPQExpBufferChar(query, ESCAPE_STRING_SYNTAX);
78007799
break;
78017800
}
78027801

78037802
appendPQExpBufferChar(query, '\'');
78047803
while (s < p)
78057804
{
7806-
if (*s == '\'')
7805+
if (*s == '\'') /* bytea already doubles backslashes */
78077806
appendPQExpBufferChar(query, '\'');
78087807
appendPQExpBufferChar(query, *s++);
78097808
}

src/bin/psql/describe.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.119 2005/07/01 17:40:28 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/describe.c,v 1.120 2005/07/02 17:01:52 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "describe.h"
@@ -1898,8 +1898,8 @@ processNamePattern(PQExpBuffer buf, const char *pattern,
18981898
appendPQExpBuffer(&namebuf, "\\\\");
18991899

19001900
/* Ensure chars special to string literals are passed properly */
1901-
if (*cp == '\'' || *cp == '\\')
1902-
appendPQExpBufferChar(&namebuf, *cp); /* double these */
1901+
if (SQL_STR_DOUBLE(*cp))
1902+
appendPQExpBufferChar(&namebuf, *cp);
19031903

19041904
i = PQmblen(cp, pset.encoding);
19051905
while (i--)

src/bin/psql/large_obj.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2005, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.38 2005/07/01 17:40:28 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/large_obj.c,v 1.39 2005/07/02 17:01:52 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "large_obj.h"
@@ -172,13 +172,17 @@ do_lo_import(const char *filename_arg, const char *comment_arg)
172172
if (!cmdbuf)
173173
return fail_lo_xact("\\lo_import", own_transaction);
174174
sprintf(cmdbuf,
175-
"COMMENT ON LARGE OBJECT %u IS '",
175+
"COMMENT ON LARGE OBJECT %u IS ",
176176
loid);
177177
bufptr = cmdbuf + strlen(cmdbuf);
178+
179+
if (strchr(comment_arg, '\\') != NULL)
180+
*bufptr++ = ESCAPE_STRING_SYNTAX;
181+
*bufptr++ = '\'';
178182
for (i = 0; i < slen; i++)
179183
{
180-
if (comment_arg[i] == '\'' || comment_arg[i] == '\\')
181-
*bufptr++ = comment_arg[i]; /* double these */
184+
if (SQL_STR_DOUBLE(comment_arg[i]))
185+
*bufptr++ = comment_arg[i];
182186
*bufptr++ = comment_arg[i];
183187
}
184188
strcpy(bufptr, "'");

src/include/c.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
15-
* $PostgreSQL: pgsql/src/include/c.h,v 1.186 2005/06/28 05:09:04 tgl Exp $
15+
* $PostgreSQL: pgsql/src/include/c.h,v 1.187 2005/07/02 17:01:52 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -479,6 +479,8 @@ typedef NameData *Name;
479479

480480
#define NameStr(name) ((name).data)
481481

482+
#define SQL_STR_DOUBLE(ch) ((ch) == '\'' || (ch) == '\\')
483+
#define ESCAPE_STRING_SYNTAX 'E'
482484

483485
/* ----------------------------------------------------------------
484486
* Section 4: IsValid macros for system types

src/interfaces/ecpg/ecpglib/execute.c

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.40 2005/06/02 12:35:11 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.41 2005/07/02 17:01:53 momjian Exp $ */
22

33
/*
44
* The aim is to get a simpler inteface to the database routines.
@@ -45,21 +45,14 @@ quote_postgres(char *arg, int lineno)
4545
if (!res)
4646
return (res);
4747

48+
if (strchr(arg, '\\') != NULL)
49+
res[ri++] = ESCAPE_STRING_SYNTAX;
4850
res[ri++] = '\'';
4951

5052
for (i = 0; arg[i]; i++, ri++)
5153
{
52-
switch (arg[i])
53-
{
54-
case '\'':
55-
res[ri++] = '\'';
56-
break;
57-
case '\\':
58-
res[ri++] = '\\';
59-
break;
60-
default:
61-
;
62-
}
54+
if (SQL_STR_DOUBLE(arg[i]))
55+
res[ri++] = arg[i];
6356
res[ri] = arg[i];
6457
}
6558

src/interfaces/ecpg/preproc/preproc.y

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.307 2005/02/10 08:06:35 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/preproc.y,v 1.308 2005/07/02 17:01:53 momjian Exp $ */
22

33
/* Copyright comment */
44
%{
@@ -4216,11 +4216,16 @@ Bconst: BCONST { $$ = make_name();};
42164216
Xconst: XCONST { $$ = make_name();};
42174217
Sconst: SCONST
42184218
{
4219-
$$ = (char *)mm_alloc(strlen($1) + 3);
4220-
$$[0]='\'';
4221-
strcpy($$+1, $1);
4222-
$$[strlen($1)+2]='\0';
4223-
$$[strlen($1)+1]='\'';
4219+
char *ret;
4220+
4221+
$$ = ret = (char *)mm_alloc(strlen($1) + 4);
4222+
if (strchr($1, '\\') != NULL)
4223+
*ret++ = ESCAPE_STRING_SYNTAX;
4224+
*ret++ = '\'';
4225+
strcpy(ret, $1);
4226+
ret += strlen($1);
4227+
*ret++ = '\'';
4228+
*ret++ = '\0';
42244229
free($1);
42254230
}
42264231
;

src/interfaces/libpq/fe-exec.c

+5-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.169 2005/06/12 00:00:21 neilc Exp $
11+
* $PostgreSQL: pgsql/src/interfaces/libpq/fe-exec.c,v 1.170 2005/07/02 17:01:54 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2368,23 +2368,9 @@ PQescapeString(char *to, const char *from, size_t length)
23682368

23692369
while (remaining > 0 && *source != '\0')
23702370
{
2371-
switch (*source)
2372-
{
2373-
case '\\':
2374-
*target++ = '\\';
2375-
*target++ = '\\';
2376-
break;
2377-
2378-
case '\'':
2379-
*target++ = '\'';
2380-
*target++ = '\'';
2381-
break;
2382-
2383-
default:
2384-
*target++ = *source;
2385-
break;
2386-
}
2387-
source++;
2371+
if (SQL_STR_DOUBLE(*source))
2372+
*target++ = *source;
2373+
*target++ = *source++;
23882374
remaining--;
23892375
}
23902376

@@ -2449,7 +2435,7 @@ PQescapeBytea(const unsigned char *bintext, size_t binlen, size_t *bytealen)
24492435
}
24502436
else if (*vp == '\'')
24512437
{
2452-
rp[0] = '\\';
2438+
rp[0] = '\'';
24532439
rp[1] = '\'';
24542440
rp += 2;
24552441
}

0 commit comments

Comments
 (0)