Skip to content

Commit d807c7e

Browse files
committed
Some fine-tuning of xmlpi in corner cases:
- correct error codes - do syntax checks in correct order - strip leading spaces of argument
1 parent de9aa5a commit d807c7e

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

src/backend/executor/execQual.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.203 2007/01/05 22:19:27 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/execQual.c,v 1.204 2007/01/07 22:49:55 petere Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -2803,15 +2803,17 @@ ExecEvalXml(XmlExprState *xmlExpr, ExprContext *econtext,
28032803
e = (ExprState *) linitial(xmlExpr->args);
28042804
value = ExecEvalExpr(e, econtext, &isnull, NULL);
28052805
if (isnull)
2806-
return (Datum) 0;
2807-
arg = DatumGetTextP(value);
2806+
arg = NULL;
2807+
else
2808+
arg = DatumGetTextP(value);
28082809
}
28092810
else
2811+
{
28102812
arg = NULL;
2813+
isnull = false;
2814+
}
28112815

2812-
*isNull = false;
2813-
2814-
return PointerGetDatum(xmlpi(xexpr->name, arg));
2816+
return PointerGetDatum(xmlpi(xexpr->name, arg, isnull, isNull));
28152817
}
28162818
break;
28172819

src/backend/utils/adt/xml.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.12 2007/01/07 00:13:55 petere Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/xml.c,v 1.13 2007/01/07 22:49:56 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -257,18 +257,26 @@ xmlparse(text *data, bool is_document, bool preserve_whitespace)
257257

258258

259259
xmltype *
260-
xmlpi(char *target, text *arg)
260+
xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null)
261261
{
262262
#ifdef USE_LIBXML
263263
xmltype *result;
264264
StringInfoData buf;
265265

266266
if (pg_strncasecmp(target, "xml", 3) == 0)
267267
ereport(ERROR,
268-
(errcode(ERRCODE_INVALID_XML_PROCESSING_INSTRUCTION),
268+
(errcode(ERRCODE_SYNTAX_ERROR), /* really */
269269
errmsg("invalid XML processing instruction"),
270270
errdetail("XML processing instruction target name cannot start with \"xml\".")));
271271

272+
/*
273+
* Following the SQL standard, the null check comes after the
274+
* syntax check above.
275+
*/
276+
*result_is_null = arg_is_null;
277+
if (*result_is_null)
278+
return NULL;
279+
272280
initStringInfo(&buf);
273281

274282
appendStringInfo(&buf, "<?%s", target);
@@ -286,7 +294,7 @@ xmlpi(char *target, text *arg)
286294
errdetail("XML processing instruction cannot contain \"?>\".")));
287295

288296
appendStringInfoChar(&buf, ' ');
289-
appendStringInfoString(&buf, string);
297+
appendStringInfoString(&buf, string + strspn(string, " "));
290298
pfree(string);
291299
}
292300
appendStringInfoString(&buf, "?>");

src/include/utils/xml.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.6 2007/01/05 22:20:00 momjian Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/xml.h,v 1.7 2007/01/07 22:49:56 petere Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -33,7 +33,7 @@ extern Datum texttoxml(PG_FUNCTION_ARGS);
3333
extern Datum xmlvalidate(PG_FUNCTION_ARGS);
3434

3535
extern xmltype *xmlparse(text *data, bool is_doc, bool preserve_whitespace);
36-
extern xmltype *xmlpi(char *target, text *arg);
36+
extern xmltype *xmlpi(char *target, text *arg, bool arg_is_null, bool *result_is_null);
3737
extern xmltype *xmlroot(xmltype *data, text *version, int standalone);
3838

3939
extern char *map_sql_identifier_to_xml_name(char *ident, bool fully_escaped);

src/test/regress/expected/xml.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,21 @@ SELECT xmlpi(name foo, 'bar');
124124
SELECT xmlpi(name foo, 'in?>valid');
125125
ERROR: invalid XML processing instruction
126126
DETAIL: XML processing instruction cannot contain "?>".
127+
SELECT xmlpi(name foo, null);
128+
xmlpi
129+
-------
130+
131+
(1 row)
132+
133+
SELECT xmlpi(name xmlstuff, null);
134+
ERROR: invalid XML processing instruction
135+
DETAIL: XML processing instruction target name cannot start with "xml".
136+
SELECT xmlpi(name foo, ' bar');
137+
xmlpi
138+
-------------
139+
<?foo bar?>
140+
(1 row)
141+
127142
SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
128143
xmlroot
129144
-----------------------

src/test/regress/expected/xml_1.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ SELECT xmlpi(name foo, 'bar');
6262
ERROR: no XML support in this installation
6363
SELECT xmlpi(name foo, 'in?>valid');
6464
ERROR: no XML support in this installation
65+
SELECT xmlpi(name foo, null);
66+
ERROR: no XML support in this installation
67+
SELECT xmlpi(name xmlstuff, null);
68+
ERROR: no XML support in this installation
69+
SELECT xmlpi(name foo, ' bar');
70+
ERROR: no XML support in this installation
6571
SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
6672
ERROR: no XML support in this installation
6773
SELECT xmlroot(xml '<foo/>', version '2.0');

src/test/regress/sql/xml.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ SELECT xmlpi(name foo);
5151
SELECT xmlpi(name xmlstuff);
5252
SELECT xmlpi(name foo, 'bar');
5353
SELECT xmlpi(name foo, 'in?>valid');
54+
SELECT xmlpi(name foo, null);
55+
SELECT xmlpi(name xmlstuff, null);
56+
SELECT xmlpi(name foo, ' bar');
5457

5558
SELECT xmlroot(xml '<foo/>', version no value, standalone no value);
5659
SELECT xmlroot(xml '<foo/>', version '2.0');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy