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

Commit 936c4af

Browse files
committed
Fix up memory management problems in contrib/xml2.
Get rid of the code that attempted to funnel libxml2's memory allocations into palloc. We already knew from experience with the core xml datatype that trying to do this is simply not reliable. Unlike the core code, I did not bother adding a lot of PG_TRY/PG_CATCH logic to try to ensure that everything is cleaned up on error exit. Hence, we might leak some memory if one of these functions fails partway through. Given the deprecated status of this contrib module and the fact that errors partway through the functions shouldn't be too common, it doesn't seem worth worrying about. Also fix a separate bug in xpath_table, that it did the wrong things if given a result tuple descriptor with less than 2 columns. While such a case isn't very useful in practice, we shouldn't fail or stomp memory when it occurs. Add some simple regression tests based on all the reported crash cases that I have on hand. This should be back-patched, but let's see if the buildfarm likes it first.
1 parent 7d7db18 commit 936c4af

File tree

4 files changed

+253
-57
lines changed

4 files changed

+253
-57
lines changed

contrib/xml2/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# $PostgreSQL: pgsql/contrib/xml2/Makefile,v 1.12 2008/05/08 16:49:37 tgl Exp $
1+
# $PostgreSQL: pgsql/contrib/xml2/Makefile,v 1.13 2010/02/28 21:31:57 tgl Exp $
22

33
MODULE_big = pgxml
44

@@ -8,6 +8,7 @@ SHLIB_LINK += $(filter -lxslt, $(LIBS)) $(filter -lxml2, $(LIBS))
88

99
DATA_built = pgxml.sql
1010
DATA = uninstall_pgxml.sql
11+
REGRESS = xml2
1112

1213
ifdef USE_PGXS
1314
PG_CONFIG = pg_config

contrib/xml2/expected/xml2.out

+147
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
--
2+
-- first, define the functions. Turn off echoing so that expected file
3+
-- does not depend on contents of pgxml.sql.
4+
--
5+
SET client_min_messages = warning;
6+
\set ECHO none
7+
RESET client_min_messages;
8+
select query_to_xml('select 1 as x',true,false,'');
9+
query_to_xml
10+
---------------------------------------------------------------
11+
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">+
12+
+
13+
<row> +
14+
<x>1</x> +
15+
</row> +
16+
+
17+
</table> +
18+
19+
(1 row)
20+
21+
select xslt_process( query_to_xml('select x from generate_series(1,5) as
22+
x',true,false,'')::text,
23+
$$<xsl:stylesheet version="1.0"
24+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
25+
<xsl:output method="xml" indent="yes" />
26+
<xsl:template match="*">
27+
<xsl:copy>
28+
<xsl:copy-of select="@*" />
29+
<xsl:apply-templates />
30+
</xsl:copy>
31+
</xsl:template>
32+
<xsl:template match="comment()|processing-instruction()">
33+
<xsl:copy />
34+
</xsl:template>
35+
</xsl:stylesheet>
36+
$$::text);
37+
xslt_process
38+
---------------------------------------------------------------
39+
<?xml version="1.0"?> +
40+
<table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">+
41+
+
42+
<row> +
43+
<x>1</x> +
44+
</row> +
45+
+
46+
<row> +
47+
<x>2</x> +
48+
</row> +
49+
+
50+
<row> +
51+
<x>3</x> +
52+
</row> +
53+
+
54+
<row> +
55+
<x>4</x> +
56+
</row> +
57+
+
58+
<row> +
59+
<x>5</x> +
60+
</row> +
61+
+
62+
</table> +
63+
64+
(1 row)
65+
66+
CREATE TABLE xpath_test (id integer NOT NULL, t xml);
67+
INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
68+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
69+
as t(id int4);
70+
id
71+
----
72+
(0 rows)
73+
74+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
75+
as t(id int4, doc int4);
76+
id | doc
77+
----+-----
78+
1 | 1
79+
(1 row)
80+
81+
DROP TABLE xpath_test;
82+
CREATE TABLE xpath_test (id integer NOT NULL, t text);
83+
INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
84+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
85+
as t(id int4);
86+
id
87+
----
88+
(0 rows)
89+
90+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
91+
as t(id int4, doc int4);
92+
id | doc
93+
----+-----
94+
1 | 1
95+
(1 row)
96+
97+
create table articles (article_id integer, article_xml xml, date_entered date);
98+
insert into articles (article_id, article_xml, date_entered)
99+
values (2, '<article><author>test</author><pages>37</pages></article>', now());
100+
SELECT * FROM
101+
xpath_table('article_id',
102+
'article_xml',
103+
'articles',
104+
'/article/author|/article/pages|/article/title',
105+
'date_entered > ''2003-01-01'' ')
106+
AS t(article_id integer, author text, page_count integer, title text);
107+
article_id | author | page_count | title
108+
------------+--------+------------+-------
109+
2 | test | 37 |
110+
(1 row)
111+
112+
-- this used to fail when invoked a second time
113+
select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
114+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
115+
<xsl:template match="@*|node()">
116+
<xsl:copy>
117+
<xsl:apply-templates select="@*|node()"/>
118+
</xsl:copy>
119+
</xsl:template>
120+
</xsl:stylesheet>$$)::xml;
121+
xslt_process
122+
--------------
123+
<aaa/> +
124+
125+
(1 row)
126+
127+
select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
128+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
129+
<xsl:template match="@*|node()">
130+
<xsl:copy>
131+
<xsl:apply-templates select="@*|node()"/>
132+
</xsl:copy>
133+
</xsl:template>
134+
</xsl:stylesheet>$$)::xml;
135+
xslt_process
136+
--------------
137+
<aaa/> +
138+
139+
(1 row)
140+
141+
create table t1 (id integer, xml_data xml);
142+
insert into t1 (id, xml_data)
143+
values
144+
(1, '<attributes><attribute name="attr_1">Some
145+
Value</attribute></attributes>');
146+
create index idx_xpath on t1 ( xpath_string
147+
('/attributes/attribute[@name="attr_1"]/text()', xml_data::text));

contrib/xml2/sql/xml2.sql

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--
2+
-- first, define the functions. Turn off echoing so that expected file
3+
-- does not depend on contents of pgxml.sql.
4+
--
5+
SET client_min_messages = warning;
6+
\set ECHO none
7+
\i pgxml.sql
8+
\set ECHO all
9+
RESET client_min_messages;
10+
11+
select query_to_xml('select 1 as x',true,false,'');
12+
13+
select xslt_process( query_to_xml('select x from generate_series(1,5) as
14+
x',true,false,'')::text,
15+
$$<xsl:stylesheet version="1.0"
16+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
17+
<xsl:output method="xml" indent="yes" />
18+
<xsl:template match="*">
19+
<xsl:copy>
20+
<xsl:copy-of select="@*" />
21+
<xsl:apply-templates />
22+
</xsl:copy>
23+
</xsl:template>
24+
<xsl:template match="comment()|processing-instruction()">
25+
<xsl:copy />
26+
</xsl:template>
27+
</xsl:stylesheet>
28+
$$::text);
29+
30+
CREATE TABLE xpath_test (id integer NOT NULL, t xml);
31+
INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
32+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
33+
as t(id int4);
34+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
35+
as t(id int4, doc int4);
36+
37+
DROP TABLE xpath_test;
38+
CREATE TABLE xpath_test (id integer NOT NULL, t text);
39+
INSERT INTO xpath_test VALUES (1, '<doc><int>1</int></doc>');
40+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
41+
as t(id int4);
42+
SELECT * FROM xpath_table('id', 't', 'xpath_test', '/doc/int', 'true')
43+
as t(id int4, doc int4);
44+
45+
create table articles (article_id integer, article_xml xml, date_entered date);
46+
insert into articles (article_id, article_xml, date_entered)
47+
values (2, '<article><author>test</author><pages>37</pages></article>', now());
48+
SELECT * FROM
49+
xpath_table('article_id',
50+
'article_xml',
51+
'articles',
52+
'/article/author|/article/pages|/article/title',
53+
'date_entered > ''2003-01-01'' ')
54+
AS t(article_id integer, author text, page_count integer, title text);
55+
56+
-- this used to fail when invoked a second time
57+
select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
58+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
59+
<xsl:template match="@*|node()">
60+
<xsl:copy>
61+
<xsl:apply-templates select="@*|node()"/>
62+
</xsl:copy>
63+
</xsl:template>
64+
</xsl:stylesheet>$$)::xml;
65+
66+
select xslt_process('<aaa/>',$$<xsl:stylesheet version="1.0"
67+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
68+
<xsl:template match="@*|node()">
69+
<xsl:copy>
70+
<xsl:apply-templates select="@*|node()"/>
71+
</xsl:copy>
72+
</xsl:template>
73+
</xsl:stylesheet>$$)::xml;
74+
75+
create table t1 (id integer, xml_data xml);
76+
insert into t1 (id, xml_data)
77+
values
78+
(1, '<attributes><attribute name="attr_1">Some
79+
Value</attribute></attributes>');
80+
81+
create index idx_xpath on t1 ( xpath_string
82+
('/attributes/attribute[@name="attr_1"]/text()', xml_data::text));

0 commit comments

Comments
 (0)