1*4543ef51SXin LI /* Tests in the "allocation" test case for the Expat test suite
2*4543ef51SXin LI __ __ _
3*4543ef51SXin LI ___\ \/ /_ __ __ _| |_
4*4543ef51SXin LI / _ \\ /| '_ \ / _` | __|
5*4543ef51SXin LI | __// \| |_) | (_| | |_
6*4543ef51SXin LI \___/_/\_\ .__/ \__,_|\__|
7*4543ef51SXin LI |_| XML parser
8*4543ef51SXin LI
9*4543ef51SXin LI Copyright (c) 2001-2006 Fred L. Drake, Jr. <fdrake@users.sourceforge.net>
10*4543ef51SXin LI Copyright (c) 2003 Greg Stein <gstein@users.sourceforge.net>
11*4543ef51SXin LI Copyright (c) 2005-2007 Steven Solie <steven@solie.ca>
12*4543ef51SXin LI Copyright (c) 2005-2012 Karl Waclawek <karl@waclawek.net>
13*4543ef51SXin LI Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org>
14*4543ef51SXin LI Copyright (c) 2017-2022 Rhodri James <rhodri@wildebeest.org.uk>
15*4543ef51SXin LI Copyright (c) 2017 Joe Orton <jorton@redhat.com>
16*4543ef51SXin LI Copyright (c) 2017 José Gutiérrez de la Concha <jose@zeroc.com>
17*4543ef51SXin LI Copyright (c) 2018 Marco Maggi <marco.maggi-ipsu@poste.it>
18*4543ef51SXin LI Copyright (c) 2019 David Loffredo <loffredo@steptools.com>
19*4543ef51SXin LI Copyright (c) 2020 Tim Gates <tim.gates@iress.com>
20*4543ef51SXin LI Copyright (c) 2021 Donghee Na <donghee.na@python.org>
21*4543ef51SXin LI Copyright (c) 2023 Sony Corporation / Snild Dolkow <snild@sony.com>
22*4543ef51SXin LI Licensed under the MIT license:
23*4543ef51SXin LI
24*4543ef51SXin LI Permission is hereby granted, free of charge, to any person obtaining
25*4543ef51SXin LI a copy of this software and associated documentation files (the
26*4543ef51SXin LI "Software"), to deal in the Software without restriction, including
27*4543ef51SXin LI without limitation the rights to use, copy, modify, merge, publish,
28*4543ef51SXin LI distribute, sublicense, and/or sell copies of the Software, and to permit
29*4543ef51SXin LI persons to whom the Software is furnished to do so, subject to the
30*4543ef51SXin LI following conditions:
31*4543ef51SXin LI
32*4543ef51SXin LI The above copyright notice and this permission notice shall be included
33*4543ef51SXin LI in all copies or substantial portions of the Software.
34*4543ef51SXin LI
35*4543ef51SXin LI THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
36*4543ef51SXin LI EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37*4543ef51SXin LI MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
38*4543ef51SXin LI NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
39*4543ef51SXin LI DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
40*4543ef51SXin LI OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
41*4543ef51SXin LI USE OR OTHER DEALINGS IN THE SOFTWARE.
42*4543ef51SXin LI */
43*4543ef51SXin LI
44*4543ef51SXin LI #if defined(NDEBUG)
45*4543ef51SXin LI # undef NDEBUG /* because test suite relies on assert(...) at the moment */
46*4543ef51SXin LI #endif
47*4543ef51SXin LI
48*4543ef51SXin LI #include <string.h>
49*4543ef51SXin LI #include <assert.h>
50*4543ef51SXin LI
51*4543ef51SXin LI #include "expat.h"
52*4543ef51SXin LI #include "common.h"
53*4543ef51SXin LI #include "minicheck.h"
54*4543ef51SXin LI #include "dummy.h"
55*4543ef51SXin LI #include "handlers.h"
56*4543ef51SXin LI #include "alloc_tests.h"
57*4543ef51SXin LI
58*4543ef51SXin LI static void
alloc_setup(void)59*4543ef51SXin LI alloc_setup(void) {
60*4543ef51SXin LI XML_Memory_Handling_Suite memsuite = {duff_allocator, duff_reallocator, free};
61*4543ef51SXin LI
62*4543ef51SXin LI /* Ensure the parser creation will go through */
63*4543ef51SXin LI g_allocation_count = ALLOC_ALWAYS_SUCCEED;
64*4543ef51SXin LI g_reallocation_count = REALLOC_ALWAYS_SUCCEED;
65*4543ef51SXin LI g_parser = XML_ParserCreate_MM(NULL, &memsuite, NULL);
66*4543ef51SXin LI if (g_parser == NULL)
67*4543ef51SXin LI fail("Parser not created");
68*4543ef51SXin LI }
69*4543ef51SXin LI
70*4543ef51SXin LI static void
alloc_teardown(void)71*4543ef51SXin LI alloc_teardown(void) {
72*4543ef51SXin LI basic_teardown();
73*4543ef51SXin LI }
74*4543ef51SXin LI
75*4543ef51SXin LI /* Test the effects of allocation failures on xml declaration processing */
START_TEST(test_alloc_parse_xdecl)76*4543ef51SXin LI START_TEST(test_alloc_parse_xdecl) {
77*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
78*4543ef51SXin LI "<doc>Hello, world</doc>";
79*4543ef51SXin LI int i;
80*4543ef51SXin LI const int max_alloc_count = 15;
81*4543ef51SXin LI
82*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
83*4543ef51SXin LI g_allocation_count = i;
84*4543ef51SXin LI XML_SetXmlDeclHandler(g_parser, dummy_xdecl_handler);
85*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
86*4543ef51SXin LI != XML_STATUS_ERROR)
87*4543ef51SXin LI break;
88*4543ef51SXin LI /* Resetting the parser is insufficient, because some memory
89*4543ef51SXin LI * allocations are cached within the parser. Instead we use
90*4543ef51SXin LI * the teardown and setup routines to ensure that we have the
91*4543ef51SXin LI * right sort of parser back in our hands.
92*4543ef51SXin LI */
93*4543ef51SXin LI alloc_teardown();
94*4543ef51SXin LI alloc_setup();
95*4543ef51SXin LI }
96*4543ef51SXin LI if (i == 0)
97*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
98*4543ef51SXin LI if (i == max_alloc_count)
99*4543ef51SXin LI fail("Parse failed with max allocations");
100*4543ef51SXin LI }
101*4543ef51SXin LI END_TEST
102*4543ef51SXin LI
103*4543ef51SXin LI /* As above, but with an encoding big enough to cause storing the
104*4543ef51SXin LI * version information to expand the string pool being used.
105*4543ef51SXin LI */
START_TEST(test_alloc_parse_xdecl_2)106*4543ef51SXin LI START_TEST(test_alloc_parse_xdecl_2) {
107*4543ef51SXin LI const char *text
108*4543ef51SXin LI = "<?xml version='1.0' encoding='"
109*4543ef51SXin LI /* Each line is 64 characters */
110*4543ef51SXin LI "ThisIsAStupidlyLongEncodingNameIntendedToTriggerPoolGrowth123456"
111*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
112*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
113*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
114*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
115*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
116*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
117*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
118*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
119*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
120*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
121*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
122*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
123*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
124*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
125*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMN"
126*4543ef51SXin LI "'?>"
127*4543ef51SXin LI "<doc>Hello, world</doc>";
128*4543ef51SXin LI int i;
129*4543ef51SXin LI const int max_alloc_count = 20;
130*4543ef51SXin LI
131*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
132*4543ef51SXin LI g_allocation_count = i;
133*4543ef51SXin LI XML_SetXmlDeclHandler(g_parser, dummy_xdecl_handler);
134*4543ef51SXin LI XML_SetUnknownEncodingHandler(g_parser, long_encoding_handler, NULL);
135*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
136*4543ef51SXin LI != XML_STATUS_ERROR)
137*4543ef51SXin LI break;
138*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
139*4543ef51SXin LI alloc_teardown();
140*4543ef51SXin LI alloc_setup();
141*4543ef51SXin LI }
142*4543ef51SXin LI if (i == 0)
143*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
144*4543ef51SXin LI if (i == max_alloc_count)
145*4543ef51SXin LI fail("Parse failed with max allocations");
146*4543ef51SXin LI }
147*4543ef51SXin LI END_TEST
148*4543ef51SXin LI
149*4543ef51SXin LI /* Test the effects of allocation failures on a straightforward parse */
START_TEST(test_alloc_parse_pi)150*4543ef51SXin LI START_TEST(test_alloc_parse_pi) {
151*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
152*4543ef51SXin LI "<?pi unknown?>\n"
153*4543ef51SXin LI "<doc>"
154*4543ef51SXin LI "Hello, world"
155*4543ef51SXin LI "</doc>";
156*4543ef51SXin LI int i;
157*4543ef51SXin LI const int max_alloc_count = 15;
158*4543ef51SXin LI
159*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
160*4543ef51SXin LI g_allocation_count = i;
161*4543ef51SXin LI XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler);
162*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
163*4543ef51SXin LI != XML_STATUS_ERROR)
164*4543ef51SXin LI break;
165*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
166*4543ef51SXin LI alloc_teardown();
167*4543ef51SXin LI alloc_setup();
168*4543ef51SXin LI }
169*4543ef51SXin LI if (i == 0)
170*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
171*4543ef51SXin LI if (i == max_alloc_count)
172*4543ef51SXin LI fail("Parse failed with max allocations");
173*4543ef51SXin LI }
174*4543ef51SXin LI END_TEST
175*4543ef51SXin LI
START_TEST(test_alloc_parse_pi_2)176*4543ef51SXin LI START_TEST(test_alloc_parse_pi_2) {
177*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
178*4543ef51SXin LI "<doc>"
179*4543ef51SXin LI "Hello, world"
180*4543ef51SXin LI "<?pi unknown?>\n"
181*4543ef51SXin LI "</doc>";
182*4543ef51SXin LI int i;
183*4543ef51SXin LI const int max_alloc_count = 15;
184*4543ef51SXin LI
185*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
186*4543ef51SXin LI g_allocation_count = i;
187*4543ef51SXin LI XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler);
188*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
189*4543ef51SXin LI != XML_STATUS_ERROR)
190*4543ef51SXin LI break;
191*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
192*4543ef51SXin LI alloc_teardown();
193*4543ef51SXin LI alloc_setup();
194*4543ef51SXin LI }
195*4543ef51SXin LI if (i == 0)
196*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
197*4543ef51SXin LI if (i == max_alloc_count)
198*4543ef51SXin LI fail("Parse failed with max allocations");
199*4543ef51SXin LI }
200*4543ef51SXin LI END_TEST
201*4543ef51SXin LI
START_TEST(test_alloc_parse_pi_3)202*4543ef51SXin LI START_TEST(test_alloc_parse_pi_3) {
203*4543ef51SXin LI const char *text
204*4543ef51SXin LI = "<?"
205*4543ef51SXin LI /* 64 characters per line */
206*4543ef51SXin LI "This processing instruction should be long enough to ensure that"
207*4543ef51SXin LI "it triggers the growth of an internal string pool when the "
208*4543ef51SXin LI "allocator fails at a cruicial moment FGHIJKLMNOPABCDEFGHIJKLMNOP"
209*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
210*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
211*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
212*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
213*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
214*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
215*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
216*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
217*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
218*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
219*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
220*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
221*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
222*4543ef51SXin LI "Q?><doc/>";
223*4543ef51SXin LI int i;
224*4543ef51SXin LI const int max_alloc_count = 20;
225*4543ef51SXin LI
226*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
227*4543ef51SXin LI g_allocation_count = i;
228*4543ef51SXin LI XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler);
229*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
230*4543ef51SXin LI != XML_STATUS_ERROR)
231*4543ef51SXin LI break;
232*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
233*4543ef51SXin LI alloc_teardown();
234*4543ef51SXin LI alloc_setup();
235*4543ef51SXin LI }
236*4543ef51SXin LI if (i == 0)
237*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
238*4543ef51SXin LI if (i == max_alloc_count)
239*4543ef51SXin LI fail("Parse failed with max allocations");
240*4543ef51SXin LI }
241*4543ef51SXin LI END_TEST
242*4543ef51SXin LI
START_TEST(test_alloc_parse_comment)243*4543ef51SXin LI START_TEST(test_alloc_parse_comment) {
244*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
245*4543ef51SXin LI "<!-- Test parsing this comment -->"
246*4543ef51SXin LI "<doc>Hi</doc>";
247*4543ef51SXin LI int i;
248*4543ef51SXin LI const int max_alloc_count = 15;
249*4543ef51SXin LI
250*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
251*4543ef51SXin LI g_allocation_count = i;
252*4543ef51SXin LI XML_SetCommentHandler(g_parser, dummy_comment_handler);
253*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
254*4543ef51SXin LI != XML_STATUS_ERROR)
255*4543ef51SXin LI break;
256*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
257*4543ef51SXin LI alloc_teardown();
258*4543ef51SXin LI alloc_setup();
259*4543ef51SXin LI }
260*4543ef51SXin LI if (i == 0)
261*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
262*4543ef51SXin LI if (i == max_alloc_count)
263*4543ef51SXin LI fail("Parse failed with max allocations");
264*4543ef51SXin LI }
265*4543ef51SXin LI END_TEST
266*4543ef51SXin LI
START_TEST(test_alloc_parse_comment_2)267*4543ef51SXin LI START_TEST(test_alloc_parse_comment_2) {
268*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='utf-8'?>\n"
269*4543ef51SXin LI "<doc>"
270*4543ef51SXin LI "Hello, world"
271*4543ef51SXin LI "<!-- Parse this comment too -->"
272*4543ef51SXin LI "</doc>";
273*4543ef51SXin LI int i;
274*4543ef51SXin LI const int max_alloc_count = 15;
275*4543ef51SXin LI
276*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
277*4543ef51SXin LI g_allocation_count = i;
278*4543ef51SXin LI XML_SetCommentHandler(g_parser, dummy_comment_handler);
279*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
280*4543ef51SXin LI != XML_STATUS_ERROR)
281*4543ef51SXin LI break;
282*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
283*4543ef51SXin LI alloc_teardown();
284*4543ef51SXin LI alloc_setup();
285*4543ef51SXin LI }
286*4543ef51SXin LI if (i == 0)
287*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
288*4543ef51SXin LI if (i == max_alloc_count)
289*4543ef51SXin LI fail("Parse failed with max allocations");
290*4543ef51SXin LI }
291*4543ef51SXin LI END_TEST
292*4543ef51SXin LI
293*4543ef51SXin LI /* Test that external parser creation running out of memory is
294*4543ef51SXin LI * correctly reported. Based on the external entity test cases.
295*4543ef51SXin LI */
START_TEST(test_alloc_create_external_parser)296*4543ef51SXin LI START_TEST(test_alloc_create_external_parser) {
297*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
298*4543ef51SXin LI "<!DOCTYPE doc SYSTEM 'foo'>\n"
299*4543ef51SXin LI "<doc>&entity;</doc>";
300*4543ef51SXin LI char foo_text[] = "<!ELEMENT doc (#PCDATA)*>";
301*4543ef51SXin LI
302*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
303*4543ef51SXin LI XML_SetUserData(g_parser, foo_text);
304*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_duff_loader);
305*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
306*4543ef51SXin LI != XML_STATUS_ERROR) {
307*4543ef51SXin LI fail("External parser allocator returned success incorrectly");
308*4543ef51SXin LI }
309*4543ef51SXin LI }
310*4543ef51SXin LI END_TEST
311*4543ef51SXin LI
312*4543ef51SXin LI /* More external parser memory allocation testing */
START_TEST(test_alloc_run_external_parser)313*4543ef51SXin LI START_TEST(test_alloc_run_external_parser) {
314*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='us-ascii'?>\n"
315*4543ef51SXin LI "<!DOCTYPE doc SYSTEM 'foo'>\n"
316*4543ef51SXin LI "<doc>&entity;</doc>";
317*4543ef51SXin LI char foo_text[] = "<!ELEMENT doc (#PCDATA)*>";
318*4543ef51SXin LI unsigned int i;
319*4543ef51SXin LI const unsigned int max_alloc_count = 15;
320*4543ef51SXin LI
321*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
322*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
323*4543ef51SXin LI XML_SetUserData(g_parser, foo_text);
324*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_null_loader);
325*4543ef51SXin LI g_allocation_count = i;
326*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
327*4543ef51SXin LI != XML_STATUS_ERROR)
328*4543ef51SXin LI break;
329*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
330*4543ef51SXin LI alloc_teardown();
331*4543ef51SXin LI alloc_setup();
332*4543ef51SXin LI }
333*4543ef51SXin LI if (i == 0)
334*4543ef51SXin LI fail("Parsing ignored failing allocator");
335*4543ef51SXin LI else if (i == max_alloc_count)
336*4543ef51SXin LI fail("Parsing failed with allocation count 10");
337*4543ef51SXin LI }
338*4543ef51SXin LI END_TEST
339*4543ef51SXin LI
340*4543ef51SXin LI /* Test that running out of memory in dtdCopy is correctly reported.
341*4543ef51SXin LI * Based on test_default_ns_from_ext_subset_and_ext_ge()
342*4543ef51SXin LI */
START_TEST(test_alloc_dtd_copy_default_atts)343*4543ef51SXin LI START_TEST(test_alloc_dtd_copy_default_atts) {
344*4543ef51SXin LI const char *text = "<?xml version='1.0'?>\n"
345*4543ef51SXin LI "<!DOCTYPE doc SYSTEM 'http://example.org/doc.dtd' [\n"
346*4543ef51SXin LI " <!ENTITY en SYSTEM 'http://example.org/entity.ent'>\n"
347*4543ef51SXin LI "]>\n"
348*4543ef51SXin LI "<doc xmlns='http://example.org/ns1'>\n"
349*4543ef51SXin LI "&en;\n"
350*4543ef51SXin LI "</doc>";
351*4543ef51SXin LI int callno = 0;
352*4543ef51SXin LI
353*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
354*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_dbl_handler);
355*4543ef51SXin LI XML_SetUserData(g_parser, &callno);
356*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
357*4543ef51SXin LI == XML_STATUS_ERROR)
358*4543ef51SXin LI xml_failure(g_parser);
359*4543ef51SXin LI }
360*4543ef51SXin LI END_TEST
361*4543ef51SXin LI
362*4543ef51SXin LI /* Test more external entity allocation failure paths */
START_TEST(test_alloc_external_entity)363*4543ef51SXin LI START_TEST(test_alloc_external_entity) {
364*4543ef51SXin LI const char *text = "<?xml version='1.0'?>\n"
365*4543ef51SXin LI "<!DOCTYPE doc SYSTEM 'http://example.org/doc.dtd' [\n"
366*4543ef51SXin LI " <!ENTITY en SYSTEM 'http://example.org/entity.ent'>\n"
367*4543ef51SXin LI "]>\n"
368*4543ef51SXin LI "<doc xmlns='http://example.org/ns1'>\n"
369*4543ef51SXin LI "&en;\n"
370*4543ef51SXin LI "</doc>";
371*4543ef51SXin LI int i;
372*4543ef51SXin LI const int alloc_test_max_repeats = 50;
373*4543ef51SXin LI int callno = 0;
374*4543ef51SXin LI
375*4543ef51SXin LI for (i = 0; i < alloc_test_max_repeats; i++) {
376*4543ef51SXin LI g_allocation_count = -1;
377*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
378*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_dbl_handler_2);
379*4543ef51SXin LI callno = 0;
380*4543ef51SXin LI XML_SetUserData(g_parser, &callno);
381*4543ef51SXin LI g_allocation_count = i;
382*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
383*4543ef51SXin LI == XML_STATUS_OK)
384*4543ef51SXin LI break;
385*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
386*4543ef51SXin LI alloc_teardown();
387*4543ef51SXin LI alloc_setup();
388*4543ef51SXin LI }
389*4543ef51SXin LI g_allocation_count = -1;
390*4543ef51SXin LI if (i == 0)
391*4543ef51SXin LI fail("External entity parsed despite duff allocator");
392*4543ef51SXin LI if (i == alloc_test_max_repeats)
393*4543ef51SXin LI fail("External entity not parsed at max allocation count");
394*4543ef51SXin LI }
395*4543ef51SXin LI END_TEST
396*4543ef51SXin LI
397*4543ef51SXin LI /* Test more allocation failure paths */
START_TEST(test_alloc_ext_entity_set_encoding)398*4543ef51SXin LI START_TEST(test_alloc_ext_entity_set_encoding) {
399*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
400*4543ef51SXin LI " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
401*4543ef51SXin LI "]>\n"
402*4543ef51SXin LI "<doc>&en;</doc>";
403*4543ef51SXin LI int i;
404*4543ef51SXin LI const int max_allocation_count = 30;
405*4543ef51SXin LI
406*4543ef51SXin LI for (i = 0; i < max_allocation_count; i++) {
407*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser,
408*4543ef51SXin LI external_entity_alloc_set_encoding);
409*4543ef51SXin LI g_allocation_count = i;
410*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
411*4543ef51SXin LI == XML_STATUS_OK)
412*4543ef51SXin LI break;
413*4543ef51SXin LI g_allocation_count = -1;
414*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
415*4543ef51SXin LI alloc_teardown();
416*4543ef51SXin LI alloc_setup();
417*4543ef51SXin LI }
418*4543ef51SXin LI if (i == 0)
419*4543ef51SXin LI fail("Encoding check succeeded despite failing allocator");
420*4543ef51SXin LI if (i == max_allocation_count)
421*4543ef51SXin LI fail("Encoding failed at max allocation count");
422*4543ef51SXin LI }
423*4543ef51SXin LI END_TEST
424*4543ef51SXin LI
425*4543ef51SXin LI /* Test the effects of allocation failure in internal entities.
426*4543ef51SXin LI * Based on test_unknown_encoding_internal_entity
427*4543ef51SXin LI */
START_TEST(test_alloc_internal_entity)428*4543ef51SXin LI START_TEST(test_alloc_internal_entity) {
429*4543ef51SXin LI const char *text = "<?xml version='1.0' encoding='unsupported-encoding'?>\n"
430*4543ef51SXin LI "<!DOCTYPE test [<!ENTITY foo 'bar'>]>\n"
431*4543ef51SXin LI "<test a='&foo;'/>";
432*4543ef51SXin LI unsigned int i;
433*4543ef51SXin LI const unsigned int max_alloc_count = 20;
434*4543ef51SXin LI
435*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
436*4543ef51SXin LI g_allocation_count = i;
437*4543ef51SXin LI XML_SetUnknownEncodingHandler(g_parser, unknown_released_encoding_handler,
438*4543ef51SXin LI NULL);
439*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
440*4543ef51SXin LI != XML_STATUS_ERROR)
441*4543ef51SXin LI break;
442*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
443*4543ef51SXin LI alloc_teardown();
444*4543ef51SXin LI alloc_setup();
445*4543ef51SXin LI }
446*4543ef51SXin LI if (i == 0)
447*4543ef51SXin LI fail("Internal entity worked despite failing allocations");
448*4543ef51SXin LI else if (i == max_alloc_count)
449*4543ef51SXin LI fail("Internal entity failed at max allocation count");
450*4543ef51SXin LI }
451*4543ef51SXin LI END_TEST
452*4543ef51SXin LI
453*4543ef51SXin LI /* Test the robustness against allocation failure of element handling
454*4543ef51SXin LI * Based on test_dtd_default_handling().
455*4543ef51SXin LI */
START_TEST(test_alloc_dtd_default_handling)456*4543ef51SXin LI START_TEST(test_alloc_dtd_default_handling) {
457*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
458*4543ef51SXin LI "<!ENTITY e SYSTEM 'http://example.org/e'>\n"
459*4543ef51SXin LI "<!NOTATION n SYSTEM 'http://example.org/n'>\n"
460*4543ef51SXin LI "<!ENTITY e1 SYSTEM 'http://example.org/e' NDATA n>\n"
461*4543ef51SXin LI "<!ELEMENT doc (#PCDATA)>\n"
462*4543ef51SXin LI "<!ATTLIST doc a CDATA #IMPLIED>\n"
463*4543ef51SXin LI "<?pi in dtd?>\n"
464*4543ef51SXin LI "<!--comment in dtd-->\n"
465*4543ef51SXin LI "]>\n"
466*4543ef51SXin LI "<doc><![CDATA[text in doc]]></doc>";
467*4543ef51SXin LI const XML_Char *expected = XCS("\n\n\n\n\n\n\n\n\n<doc>text in doc</doc>");
468*4543ef51SXin LI CharData storage;
469*4543ef51SXin LI int i;
470*4543ef51SXin LI const int max_alloc_count = 25;
471*4543ef51SXin LI
472*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
473*4543ef51SXin LI g_allocation_count = i;
474*4543ef51SXin LI init_dummy_handlers();
475*4543ef51SXin LI XML_SetDefaultHandler(g_parser, accumulate_characters);
476*4543ef51SXin LI XML_SetDoctypeDeclHandler(g_parser, dummy_start_doctype_handler,
477*4543ef51SXin LI dummy_end_doctype_handler);
478*4543ef51SXin LI XML_SetEntityDeclHandler(g_parser, dummy_entity_decl_handler);
479*4543ef51SXin LI XML_SetNotationDeclHandler(g_parser, dummy_notation_decl_handler);
480*4543ef51SXin LI XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
481*4543ef51SXin LI XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
482*4543ef51SXin LI XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler);
483*4543ef51SXin LI XML_SetCommentHandler(g_parser, dummy_comment_handler);
484*4543ef51SXin LI XML_SetCdataSectionHandler(g_parser, dummy_start_cdata_handler,
485*4543ef51SXin LI dummy_end_cdata_handler);
486*4543ef51SXin LI XML_SetUnparsedEntityDeclHandler(g_parser,
487*4543ef51SXin LI dummy_unparsed_entity_decl_handler);
488*4543ef51SXin LI CharData_Init(&storage);
489*4543ef51SXin LI XML_SetUserData(g_parser, &storage);
490*4543ef51SXin LI XML_SetCharacterDataHandler(g_parser, accumulate_characters);
491*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
492*4543ef51SXin LI != XML_STATUS_ERROR)
493*4543ef51SXin LI break;
494*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
495*4543ef51SXin LI alloc_teardown();
496*4543ef51SXin LI alloc_setup();
497*4543ef51SXin LI }
498*4543ef51SXin LI if (i == 0)
499*4543ef51SXin LI fail("Default DTD parsed despite allocation failures");
500*4543ef51SXin LI if (i == max_alloc_count)
501*4543ef51SXin LI fail("Default DTD not parsed with maximum alloc count");
502*4543ef51SXin LI CharData_CheckXMLChars(&storage, expected);
503*4543ef51SXin LI if (get_dummy_handler_flags()
504*4543ef51SXin LI != (DUMMY_START_DOCTYPE_HANDLER_FLAG | DUMMY_END_DOCTYPE_HANDLER_FLAG
505*4543ef51SXin LI | DUMMY_ENTITY_DECL_HANDLER_FLAG | DUMMY_NOTATION_DECL_HANDLER_FLAG
506*4543ef51SXin LI | DUMMY_ELEMENT_DECL_HANDLER_FLAG | DUMMY_ATTLIST_DECL_HANDLER_FLAG
507*4543ef51SXin LI | DUMMY_COMMENT_HANDLER_FLAG | DUMMY_PI_HANDLER_FLAG
508*4543ef51SXin LI | DUMMY_START_CDATA_HANDLER_FLAG | DUMMY_END_CDATA_HANDLER_FLAG
509*4543ef51SXin LI | DUMMY_UNPARSED_ENTITY_DECL_HANDLER_FLAG))
510*4543ef51SXin LI fail("Not all handlers were called");
511*4543ef51SXin LI }
512*4543ef51SXin LI END_TEST
513*4543ef51SXin LI
514*4543ef51SXin LI /* Test robustness of XML_SetEncoding() with a failing allocator */
START_TEST(test_alloc_explicit_encoding)515*4543ef51SXin LI START_TEST(test_alloc_explicit_encoding) {
516*4543ef51SXin LI int i;
517*4543ef51SXin LI const int max_alloc_count = 5;
518*4543ef51SXin LI
519*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
520*4543ef51SXin LI g_allocation_count = i;
521*4543ef51SXin LI if (XML_SetEncoding(g_parser, XCS("us-ascii")) == XML_STATUS_OK)
522*4543ef51SXin LI break;
523*4543ef51SXin LI }
524*4543ef51SXin LI if (i == 0)
525*4543ef51SXin LI fail("Encoding set despite failing allocator");
526*4543ef51SXin LI else if (i == max_alloc_count)
527*4543ef51SXin LI fail("Encoding not set at max allocation count");
528*4543ef51SXin LI }
529*4543ef51SXin LI END_TEST
530*4543ef51SXin LI
531*4543ef51SXin LI /* Test robustness of XML_SetBase against a failing allocator */
START_TEST(test_alloc_set_base)532*4543ef51SXin LI START_TEST(test_alloc_set_base) {
533*4543ef51SXin LI const XML_Char *new_base = XCS("/local/file/name.xml");
534*4543ef51SXin LI int i;
535*4543ef51SXin LI const int max_alloc_count = 5;
536*4543ef51SXin LI
537*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
538*4543ef51SXin LI g_allocation_count = i;
539*4543ef51SXin LI if (XML_SetBase(g_parser, new_base) == XML_STATUS_OK)
540*4543ef51SXin LI break;
541*4543ef51SXin LI }
542*4543ef51SXin LI if (i == 0)
543*4543ef51SXin LI fail("Base set despite failing allocator");
544*4543ef51SXin LI else if (i == max_alloc_count)
545*4543ef51SXin LI fail("Base not set with max allocation count");
546*4543ef51SXin LI }
547*4543ef51SXin LI END_TEST
548*4543ef51SXin LI
549*4543ef51SXin LI /* Test buffer extension in the face of a duff reallocator */
START_TEST(test_alloc_realloc_buffer)550*4543ef51SXin LI START_TEST(test_alloc_realloc_buffer) {
551*4543ef51SXin LI const char *text = get_buffer_test_text;
552*4543ef51SXin LI void *buffer;
553*4543ef51SXin LI int i;
554*4543ef51SXin LI const int max_realloc_count = 10;
555*4543ef51SXin LI
556*4543ef51SXin LI /* Get a smallish buffer */
557*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
558*4543ef51SXin LI g_reallocation_count = i;
559*4543ef51SXin LI buffer = XML_GetBuffer(g_parser, 1536);
560*4543ef51SXin LI if (buffer == NULL)
561*4543ef51SXin LI fail("1.5K buffer reallocation failed");
562*4543ef51SXin LI assert(buffer != NULL);
563*4543ef51SXin LI memcpy(buffer, text, strlen(text));
564*4543ef51SXin LI if (XML_ParseBuffer(g_parser, (int)strlen(text), XML_FALSE)
565*4543ef51SXin LI == XML_STATUS_OK)
566*4543ef51SXin LI break;
567*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
568*4543ef51SXin LI alloc_teardown();
569*4543ef51SXin LI alloc_setup();
570*4543ef51SXin LI }
571*4543ef51SXin LI g_reallocation_count = -1;
572*4543ef51SXin LI if (i == 0)
573*4543ef51SXin LI fail("Parse succeeded with no reallocation");
574*4543ef51SXin LI else if (i == max_realloc_count)
575*4543ef51SXin LI fail("Parse failed with max reallocation count");
576*4543ef51SXin LI }
577*4543ef51SXin LI END_TEST
578*4543ef51SXin LI
579*4543ef51SXin LI /* Same test for external entity parsers */
START_TEST(test_alloc_ext_entity_realloc_buffer)580*4543ef51SXin LI START_TEST(test_alloc_ext_entity_realloc_buffer) {
581*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
582*4543ef51SXin LI " <!ENTITY en SYSTEM 'http://example.org/dummy.ent'>\n"
583*4543ef51SXin LI "]>\n"
584*4543ef51SXin LI "<doc>&en;</doc>";
585*4543ef51SXin LI int i;
586*4543ef51SXin LI const int max_realloc_count = 10;
587*4543ef51SXin LI
588*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
589*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_reallocator);
590*4543ef51SXin LI XML_SetUserData(g_parser, &i);
591*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
592*4543ef51SXin LI == XML_STATUS_OK)
593*4543ef51SXin LI break;
594*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
595*4543ef51SXin LI alloc_teardown();
596*4543ef51SXin LI alloc_setup();
597*4543ef51SXin LI }
598*4543ef51SXin LI if (i == 0)
599*4543ef51SXin LI fail("Succeeded with no reallocations");
600*4543ef51SXin LI if (i == max_realloc_count)
601*4543ef51SXin LI fail("Failed with max reallocations");
602*4543ef51SXin LI }
603*4543ef51SXin LI END_TEST
604*4543ef51SXin LI
605*4543ef51SXin LI /* Test elements with many attributes are handled correctly */
START_TEST(test_alloc_realloc_many_attributes)606*4543ef51SXin LI START_TEST(test_alloc_realloc_many_attributes) {
607*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
608*4543ef51SXin LI "<!ATTLIST doc za CDATA 'default'>\n"
609*4543ef51SXin LI "<!ATTLIST doc zb CDATA 'def2'>\n"
610*4543ef51SXin LI "<!ATTLIST doc zc CDATA 'def3'>\n"
611*4543ef51SXin LI "]>\n"
612*4543ef51SXin LI "<doc a='1'"
613*4543ef51SXin LI " b='2'"
614*4543ef51SXin LI " c='3'"
615*4543ef51SXin LI " d='4'"
616*4543ef51SXin LI " e='5'"
617*4543ef51SXin LI " f='6'"
618*4543ef51SXin LI " g='7'"
619*4543ef51SXin LI " h='8'"
620*4543ef51SXin LI " i='9'"
621*4543ef51SXin LI " j='10'"
622*4543ef51SXin LI " k='11'"
623*4543ef51SXin LI " l='12'"
624*4543ef51SXin LI " m='13'"
625*4543ef51SXin LI " n='14'"
626*4543ef51SXin LI " p='15'"
627*4543ef51SXin LI " q='16'"
628*4543ef51SXin LI " r='17'"
629*4543ef51SXin LI " s='18'>"
630*4543ef51SXin LI "</doc>";
631*4543ef51SXin LI int i;
632*4543ef51SXin LI const int max_realloc_count = 10;
633*4543ef51SXin LI
634*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
635*4543ef51SXin LI g_reallocation_count = i;
636*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
637*4543ef51SXin LI != XML_STATUS_ERROR)
638*4543ef51SXin LI break;
639*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
640*4543ef51SXin LI alloc_teardown();
641*4543ef51SXin LI alloc_setup();
642*4543ef51SXin LI }
643*4543ef51SXin LI if (i == 0)
644*4543ef51SXin LI fail("Parse succeeded despite no reallocations");
645*4543ef51SXin LI if (i == max_realloc_count)
646*4543ef51SXin LI fail("Parse failed at max reallocations");
647*4543ef51SXin LI }
648*4543ef51SXin LI END_TEST
649*4543ef51SXin LI
650*4543ef51SXin LI /* Test handling of a public entity with failing allocator */
START_TEST(test_alloc_public_entity_value)651*4543ef51SXin LI START_TEST(test_alloc_public_entity_value) {
652*4543ef51SXin LI const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/'>\n"
653*4543ef51SXin LI "<doc></doc>\n";
654*4543ef51SXin LI char dtd_text[]
655*4543ef51SXin LI = "<!ELEMENT doc EMPTY>\n"
656*4543ef51SXin LI "<!ENTITY % e1 PUBLIC 'foo' 'bar.ent'>\n"
657*4543ef51SXin LI "<!ENTITY % "
658*4543ef51SXin LI /* Each line is 64 characters */
659*4543ef51SXin LI "ThisIsAStupidlyLongParameterNameIntendedToTriggerPoolGrowth12345"
660*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
661*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
662*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
663*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
664*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
665*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
666*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
667*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
668*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
669*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
670*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
671*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
672*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
673*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
674*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
675*4543ef51SXin LI " '%e1;'>\n"
676*4543ef51SXin LI "%e1;\n";
677*4543ef51SXin LI int i;
678*4543ef51SXin LI const int max_alloc_count = 50;
679*4543ef51SXin LI
680*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
681*4543ef51SXin LI g_allocation_count = i;
682*4543ef51SXin LI init_dummy_handlers();
683*4543ef51SXin LI XML_SetUserData(g_parser, dtd_text);
684*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
685*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_public);
686*4543ef51SXin LI /* Provoke a particular code path */
687*4543ef51SXin LI XML_SetEntityDeclHandler(g_parser, dummy_entity_decl_handler);
688*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
689*4543ef51SXin LI != XML_STATUS_ERROR)
690*4543ef51SXin LI break;
691*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
692*4543ef51SXin LI alloc_teardown();
693*4543ef51SXin LI alloc_setup();
694*4543ef51SXin LI }
695*4543ef51SXin LI if (i == 0)
696*4543ef51SXin LI fail("Parsing worked despite failing allocation");
697*4543ef51SXin LI if (i == max_alloc_count)
698*4543ef51SXin LI fail("Parsing failed at max allocation count");
699*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_ENTITY_DECL_HANDLER_FLAG)
700*4543ef51SXin LI fail("Entity declaration handler not called");
701*4543ef51SXin LI }
702*4543ef51SXin LI END_TEST
703*4543ef51SXin LI
START_TEST(test_alloc_realloc_subst_public_entity_value)704*4543ef51SXin LI START_TEST(test_alloc_realloc_subst_public_entity_value) {
705*4543ef51SXin LI const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/'>\n"
706*4543ef51SXin LI "<doc></doc>\n";
707*4543ef51SXin LI char dtd_text[]
708*4543ef51SXin LI = "<!ELEMENT doc EMPTY>\n"
709*4543ef51SXin LI "<!ENTITY % "
710*4543ef51SXin LI /* Each line is 64 characters */
711*4543ef51SXin LI "ThisIsAStupidlyLongParameterNameIntendedToTriggerPoolGrowth12345"
712*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
713*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
714*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
715*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
716*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
717*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
718*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
719*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
720*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
721*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
722*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
723*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
724*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
725*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
726*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
727*4543ef51SXin LI " PUBLIC 'foo' 'bar.ent'>\n"
728*4543ef51SXin LI "%ThisIsAStupidlyLongParameterNameIntendedToTriggerPoolGrowth12345"
729*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
730*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
731*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
732*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
733*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
734*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
735*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
736*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
737*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
738*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
739*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
740*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
741*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
742*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
743*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP;";
744*4543ef51SXin LI int i;
745*4543ef51SXin LI const int max_realloc_count = 10;
746*4543ef51SXin LI
747*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
748*4543ef51SXin LI g_reallocation_count = i;
749*4543ef51SXin LI XML_SetUserData(g_parser, dtd_text);
750*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
751*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_public);
752*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
753*4543ef51SXin LI != XML_STATUS_ERROR)
754*4543ef51SXin LI break;
755*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
756*4543ef51SXin LI alloc_teardown();
757*4543ef51SXin LI alloc_setup();
758*4543ef51SXin LI }
759*4543ef51SXin LI if (i == 0)
760*4543ef51SXin LI fail("Parsing worked despite failing reallocation");
761*4543ef51SXin LI if (i == max_realloc_count)
762*4543ef51SXin LI fail("Parsing failed at max reallocation count");
763*4543ef51SXin LI }
764*4543ef51SXin LI END_TEST
765*4543ef51SXin LI
START_TEST(test_alloc_parse_public_doctype)766*4543ef51SXin LI START_TEST(test_alloc_parse_public_doctype) {
767*4543ef51SXin LI const char *text
768*4543ef51SXin LI = "<?xml version='1.0' encoding='utf-8'?>\n"
769*4543ef51SXin LI "<!DOCTYPE doc PUBLIC '"
770*4543ef51SXin LI /* 64 characters per line */
771*4543ef51SXin LI "http://example.com/a/long/enough/name/to/trigger/pool/growth/zz/"
772*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
773*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
774*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
775*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
776*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
777*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
778*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
779*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
780*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
781*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
782*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
783*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
784*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
785*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
786*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
787*4543ef51SXin LI "' 'test'>\n"
788*4543ef51SXin LI "<doc></doc>";
789*4543ef51SXin LI int i;
790*4543ef51SXin LI const int max_alloc_count = 25;
791*4543ef51SXin LI
792*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
793*4543ef51SXin LI g_allocation_count = i;
794*4543ef51SXin LI init_dummy_handlers();
795*4543ef51SXin LI XML_SetDoctypeDeclHandler(g_parser, dummy_start_doctype_decl_handler,
796*4543ef51SXin LI dummy_end_doctype_decl_handler);
797*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
798*4543ef51SXin LI != XML_STATUS_ERROR)
799*4543ef51SXin LI break;
800*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
801*4543ef51SXin LI alloc_teardown();
802*4543ef51SXin LI alloc_setup();
803*4543ef51SXin LI }
804*4543ef51SXin LI if (i == 0)
805*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
806*4543ef51SXin LI if (i == max_alloc_count)
807*4543ef51SXin LI fail("Parse failed at maximum allocation count");
808*4543ef51SXin LI if (get_dummy_handler_flags()
809*4543ef51SXin LI != (DUMMY_START_DOCTYPE_DECL_HANDLER_FLAG
810*4543ef51SXin LI | DUMMY_END_DOCTYPE_DECL_HANDLER_FLAG))
811*4543ef51SXin LI fail("Doctype handler functions not called");
812*4543ef51SXin LI }
813*4543ef51SXin LI END_TEST
814*4543ef51SXin LI
START_TEST(test_alloc_parse_public_doctype_long_name)815*4543ef51SXin LI START_TEST(test_alloc_parse_public_doctype_long_name) {
816*4543ef51SXin LI const char *text
817*4543ef51SXin LI = "<?xml version='1.0' encoding='utf-8'?>\n"
818*4543ef51SXin LI "<!DOCTYPE doc PUBLIC 'http://example.com/foo' '"
819*4543ef51SXin LI /* 64 characters per line */
820*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
821*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
822*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
823*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
824*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
825*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
826*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
827*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
828*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
829*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
830*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
831*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
832*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
833*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
834*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
835*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNOP"
836*4543ef51SXin LI "'>\n"
837*4543ef51SXin LI "<doc></doc>";
838*4543ef51SXin LI int i;
839*4543ef51SXin LI const int max_alloc_count = 25;
840*4543ef51SXin LI
841*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
842*4543ef51SXin LI g_allocation_count = i;
843*4543ef51SXin LI XML_SetDoctypeDeclHandler(g_parser, dummy_start_doctype_decl_handler,
844*4543ef51SXin LI dummy_end_doctype_decl_handler);
845*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
846*4543ef51SXin LI != XML_STATUS_ERROR)
847*4543ef51SXin LI break;
848*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
849*4543ef51SXin LI alloc_teardown();
850*4543ef51SXin LI alloc_setup();
851*4543ef51SXin LI }
852*4543ef51SXin LI if (i == 0)
853*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
854*4543ef51SXin LI if (i == max_alloc_count)
855*4543ef51SXin LI fail("Parse failed at maximum allocation count");
856*4543ef51SXin LI }
857*4543ef51SXin LI END_TEST
858*4543ef51SXin LI
859*4543ef51SXin LI /* Test foreign DTD handling */
START_TEST(test_alloc_set_foreign_dtd)860*4543ef51SXin LI START_TEST(test_alloc_set_foreign_dtd) {
861*4543ef51SXin LI const char *text1 = "<?xml version='1.0' encoding='us-ascii'?>\n"
862*4543ef51SXin LI "<doc>&entity;</doc>";
863*4543ef51SXin LI char text2[] = "<!ELEMENT doc (#PCDATA)*>";
864*4543ef51SXin LI int i;
865*4543ef51SXin LI const int max_alloc_count = 25;
866*4543ef51SXin LI
867*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
868*4543ef51SXin LI g_allocation_count = i;
869*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
870*4543ef51SXin LI XML_SetUserData(g_parser, &text2);
871*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
872*4543ef51SXin LI if (XML_UseForeignDTD(g_parser, XML_TRUE) != XML_ERROR_NONE)
873*4543ef51SXin LI fail("Could not set foreign DTD");
874*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text1, (int)strlen(text1), XML_TRUE)
875*4543ef51SXin LI != XML_STATUS_ERROR)
876*4543ef51SXin LI break;
877*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
878*4543ef51SXin LI alloc_teardown();
879*4543ef51SXin LI alloc_setup();
880*4543ef51SXin LI }
881*4543ef51SXin LI if (i == 0)
882*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
883*4543ef51SXin LI if (i == max_alloc_count)
884*4543ef51SXin LI fail("Parse failed at maximum allocation count");
885*4543ef51SXin LI }
886*4543ef51SXin LI END_TEST
887*4543ef51SXin LI
888*4543ef51SXin LI /* Test based on ibm/valid/P32/ibm32v04.xml */
START_TEST(test_alloc_attribute_enum_value)889*4543ef51SXin LI START_TEST(test_alloc_attribute_enum_value) {
890*4543ef51SXin LI const char *text = "<?xml version='1.0' standalone='no'?>\n"
891*4543ef51SXin LI "<!DOCTYPE animal SYSTEM 'test.dtd'>\n"
892*4543ef51SXin LI "<animal>This is a \n <a/> \n\nyellow tiger</animal>";
893*4543ef51SXin LI char dtd_text[] = "<!ELEMENT animal (#PCDATA|a)*>\n"
894*4543ef51SXin LI "<!ELEMENT a EMPTY>\n"
895*4543ef51SXin LI "<!ATTLIST animal xml:space (default|preserve) 'preserve'>";
896*4543ef51SXin LI int i;
897*4543ef51SXin LI const int max_alloc_count = 30;
898*4543ef51SXin LI
899*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
900*4543ef51SXin LI g_allocation_count = i;
901*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
902*4543ef51SXin LI XML_SetUserData(g_parser, dtd_text);
903*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
904*4543ef51SXin LI /* An attribute list handler provokes a different code path */
905*4543ef51SXin LI XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
906*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
907*4543ef51SXin LI != XML_STATUS_ERROR)
908*4543ef51SXin LI break;
909*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
910*4543ef51SXin LI alloc_teardown();
911*4543ef51SXin LI alloc_setup();
912*4543ef51SXin LI }
913*4543ef51SXin LI if (i == 0)
914*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
915*4543ef51SXin LI if (i == max_alloc_count)
916*4543ef51SXin LI fail("Parse failed at maximum allocation count");
917*4543ef51SXin LI }
918*4543ef51SXin LI END_TEST
919*4543ef51SXin LI
920*4543ef51SXin LI /* Test attribute enums sufficient to overflow the string pool */
START_TEST(test_alloc_realloc_attribute_enum_value)921*4543ef51SXin LI START_TEST(test_alloc_realloc_attribute_enum_value) {
922*4543ef51SXin LI const char *text = "<?xml version='1.0' standalone='no'?>\n"
923*4543ef51SXin LI "<!DOCTYPE animal SYSTEM 'test.dtd'>\n"
924*4543ef51SXin LI "<animal>This is a yellow tiger</animal>";
925*4543ef51SXin LI /* We wish to define a collection of attribute enums that will
926*4543ef51SXin LI * cause the string pool storing them to have to expand. This
927*4543ef51SXin LI * means more than 1024 bytes, including the parentheses and
928*4543ef51SXin LI * separator bars.
929*4543ef51SXin LI */
930*4543ef51SXin LI char dtd_text[]
931*4543ef51SXin LI = "<!ELEMENT animal (#PCDATA)*>\n"
932*4543ef51SXin LI "<!ATTLIST animal thing "
933*4543ef51SXin LI "(default"
934*4543ef51SXin LI /* Each line is 64 characters */
935*4543ef51SXin LI "|ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
936*4543ef51SXin LI "|BBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
937*4543ef51SXin LI "|CBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
938*4543ef51SXin LI "|DBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
939*4543ef51SXin LI "|EBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
940*4543ef51SXin LI "|FBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
941*4543ef51SXin LI "|GBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
942*4543ef51SXin LI "|HBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
943*4543ef51SXin LI "|IBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
944*4543ef51SXin LI "|JBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
945*4543ef51SXin LI "|KBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
946*4543ef51SXin LI "|LBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
947*4543ef51SXin LI "|MBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
948*4543ef51SXin LI "|NBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
949*4543ef51SXin LI "|OBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
950*4543ef51SXin LI "|PBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO)"
951*4543ef51SXin LI " 'default'>";
952*4543ef51SXin LI int i;
953*4543ef51SXin LI const int max_realloc_count = 10;
954*4543ef51SXin LI
955*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
956*4543ef51SXin LI g_reallocation_count = i;
957*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
958*4543ef51SXin LI XML_SetUserData(g_parser, dtd_text);
959*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
960*4543ef51SXin LI /* An attribute list handler provokes a different code path */
961*4543ef51SXin LI XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
962*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
963*4543ef51SXin LI != XML_STATUS_ERROR)
964*4543ef51SXin LI break;
965*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
966*4543ef51SXin LI alloc_teardown();
967*4543ef51SXin LI alloc_setup();
968*4543ef51SXin LI }
969*4543ef51SXin LI if (i == 0)
970*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
971*4543ef51SXin LI if (i == max_realloc_count)
972*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
973*4543ef51SXin LI }
974*4543ef51SXin LI END_TEST
975*4543ef51SXin LI
976*4543ef51SXin LI /* Test attribute enums in a #IMPLIED attribute forcing pool growth */
START_TEST(test_alloc_realloc_implied_attribute)977*4543ef51SXin LI START_TEST(test_alloc_realloc_implied_attribute) {
978*4543ef51SXin LI /* Forcing this particular code path is a balancing act. The
979*4543ef51SXin LI * addition of the closing parenthesis and terminal NUL must be
980*4543ef51SXin LI * what pushes the string of enums over the 1024-byte limit,
981*4543ef51SXin LI * otherwise a different code path will pick up the realloc.
982*4543ef51SXin LI */
983*4543ef51SXin LI const char *text
984*4543ef51SXin LI = "<!DOCTYPE doc [\n"
985*4543ef51SXin LI "<!ELEMENT doc EMPTY>\n"
986*4543ef51SXin LI "<!ATTLIST doc a "
987*4543ef51SXin LI /* Each line is 64 characters */
988*4543ef51SXin LI "(ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
989*4543ef51SXin LI "|BBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
990*4543ef51SXin LI "|CBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
991*4543ef51SXin LI "|DBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
992*4543ef51SXin LI "|EBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
993*4543ef51SXin LI "|FBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
994*4543ef51SXin LI "|GBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
995*4543ef51SXin LI "|HBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
996*4543ef51SXin LI "|IBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
997*4543ef51SXin LI "|JBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
998*4543ef51SXin LI "|KBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
999*4543ef51SXin LI "|LBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1000*4543ef51SXin LI "|MBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1001*4543ef51SXin LI "|NBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1002*4543ef51SXin LI "|OBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1003*4543ef51SXin LI "|PBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMN)"
1004*4543ef51SXin LI " #IMPLIED>\n"
1005*4543ef51SXin LI "]><doc/>";
1006*4543ef51SXin LI int i;
1007*4543ef51SXin LI const int max_realloc_count = 10;
1008*4543ef51SXin LI
1009*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1010*4543ef51SXin LI g_reallocation_count = i;
1011*4543ef51SXin LI XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
1012*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1013*4543ef51SXin LI != XML_STATUS_ERROR)
1014*4543ef51SXin LI break;
1015*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1016*4543ef51SXin LI alloc_teardown();
1017*4543ef51SXin LI alloc_setup();
1018*4543ef51SXin LI }
1019*4543ef51SXin LI if (i == 0)
1020*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1021*4543ef51SXin LI if (i == max_realloc_count)
1022*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1023*4543ef51SXin LI }
1024*4543ef51SXin LI END_TEST
1025*4543ef51SXin LI
1026*4543ef51SXin LI /* Test attribute enums in a defaulted attribute forcing pool growth */
START_TEST(test_alloc_realloc_default_attribute)1027*4543ef51SXin LI START_TEST(test_alloc_realloc_default_attribute) {
1028*4543ef51SXin LI /* Forcing this particular code path is a balancing act. The
1029*4543ef51SXin LI * addition of the closing parenthesis and terminal NUL must be
1030*4543ef51SXin LI * what pushes the string of enums over the 1024-byte limit,
1031*4543ef51SXin LI * otherwise a different code path will pick up the realloc.
1032*4543ef51SXin LI */
1033*4543ef51SXin LI const char *text
1034*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1035*4543ef51SXin LI "<!ELEMENT doc EMPTY>\n"
1036*4543ef51SXin LI "<!ATTLIST doc a "
1037*4543ef51SXin LI /* Each line is 64 characters */
1038*4543ef51SXin LI "(ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1039*4543ef51SXin LI "|BBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1040*4543ef51SXin LI "|CBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1041*4543ef51SXin LI "|DBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1042*4543ef51SXin LI "|EBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1043*4543ef51SXin LI "|FBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1044*4543ef51SXin LI "|GBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1045*4543ef51SXin LI "|HBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1046*4543ef51SXin LI "|IBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1047*4543ef51SXin LI "|JBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1048*4543ef51SXin LI "|KBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1049*4543ef51SXin LI "|LBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1050*4543ef51SXin LI "|MBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1051*4543ef51SXin LI "|NBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1052*4543ef51SXin LI "|OBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO"
1053*4543ef51SXin LI "|PBCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMN)"
1054*4543ef51SXin LI " 'ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNO'"
1055*4543ef51SXin LI ">\n]><doc/>";
1056*4543ef51SXin LI int i;
1057*4543ef51SXin LI const int max_realloc_count = 10;
1058*4543ef51SXin LI
1059*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1060*4543ef51SXin LI g_reallocation_count = i;
1061*4543ef51SXin LI XML_SetAttlistDeclHandler(g_parser, dummy_attlist_decl_handler);
1062*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1063*4543ef51SXin LI != XML_STATUS_ERROR)
1064*4543ef51SXin LI break;
1065*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1066*4543ef51SXin LI alloc_teardown();
1067*4543ef51SXin LI alloc_setup();
1068*4543ef51SXin LI }
1069*4543ef51SXin LI if (i == 0)
1070*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1071*4543ef51SXin LI if (i == max_realloc_count)
1072*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1073*4543ef51SXin LI }
1074*4543ef51SXin LI END_TEST
1075*4543ef51SXin LI
1076*4543ef51SXin LI /* Test long notation name with dodgy allocator */
START_TEST(test_alloc_notation)1077*4543ef51SXin LI START_TEST(test_alloc_notation) {
1078*4543ef51SXin LI const char *text
1079*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1080*4543ef51SXin LI "<!NOTATION "
1081*4543ef51SXin LI /* Each line is 64 characters */
1082*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1083*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1084*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1085*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1086*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1087*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1088*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1089*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1090*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1091*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1092*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1093*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1094*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1095*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1096*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1097*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1098*4543ef51SXin LI " SYSTEM 'http://example.org/n'>\n"
1099*4543ef51SXin LI "<!ENTITY e SYSTEM 'http://example.org/e' NDATA "
1100*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1101*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1102*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1103*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1104*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1105*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1106*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1107*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1108*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1109*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1110*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1111*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1112*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1113*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1114*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1115*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1116*4543ef51SXin LI ">\n"
1117*4543ef51SXin LI "<!ELEMENT doc EMPTY>\n"
1118*4543ef51SXin LI "]>\n<doc/>";
1119*4543ef51SXin LI int i;
1120*4543ef51SXin LI const int max_alloc_count = 20;
1121*4543ef51SXin LI
1122*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1123*4543ef51SXin LI g_allocation_count = i;
1124*4543ef51SXin LI init_dummy_handlers();
1125*4543ef51SXin LI XML_SetNotationDeclHandler(g_parser, dummy_notation_decl_handler);
1126*4543ef51SXin LI XML_SetEntityDeclHandler(g_parser, dummy_entity_decl_handler);
1127*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1128*4543ef51SXin LI != XML_STATUS_ERROR)
1129*4543ef51SXin LI break;
1130*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1131*4543ef51SXin LI alloc_teardown();
1132*4543ef51SXin LI alloc_setup();
1133*4543ef51SXin LI }
1134*4543ef51SXin LI if (i == 0)
1135*4543ef51SXin LI fail("Parse succeeded despite allocation failures");
1136*4543ef51SXin LI if (i == max_alloc_count)
1137*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1138*4543ef51SXin LI if (get_dummy_handler_flags()
1139*4543ef51SXin LI != (DUMMY_ENTITY_DECL_HANDLER_FLAG | DUMMY_NOTATION_DECL_HANDLER_FLAG))
1140*4543ef51SXin LI fail("Entity declaration handler not called");
1141*4543ef51SXin LI }
1142*4543ef51SXin LI END_TEST
1143*4543ef51SXin LI
1144*4543ef51SXin LI /* Test public notation with dodgy allocator */
START_TEST(test_alloc_public_notation)1145*4543ef51SXin LI START_TEST(test_alloc_public_notation) {
1146*4543ef51SXin LI const char *text
1147*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1148*4543ef51SXin LI "<!NOTATION note PUBLIC '"
1149*4543ef51SXin LI /* 64 characters per line */
1150*4543ef51SXin LI "http://example.com/a/long/enough/name/to/trigger/pool/growth/zz/"
1151*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1152*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1153*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1154*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1155*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1156*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1157*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1158*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1159*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1160*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1161*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1162*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1163*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1164*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1165*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1166*4543ef51SXin LI "' 'foo'>\n"
1167*4543ef51SXin LI "<!ENTITY e SYSTEM 'http://example.com/e' NDATA note>\n"
1168*4543ef51SXin LI "<!ELEMENT doc EMPTY>\n"
1169*4543ef51SXin LI "]>\n<doc/>";
1170*4543ef51SXin LI int i;
1171*4543ef51SXin LI const int max_alloc_count = 20;
1172*4543ef51SXin LI
1173*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1174*4543ef51SXin LI g_allocation_count = i;
1175*4543ef51SXin LI init_dummy_handlers();
1176*4543ef51SXin LI XML_SetNotationDeclHandler(g_parser, dummy_notation_decl_handler);
1177*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1178*4543ef51SXin LI != XML_STATUS_ERROR)
1179*4543ef51SXin LI break;
1180*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1181*4543ef51SXin LI alloc_teardown();
1182*4543ef51SXin LI alloc_setup();
1183*4543ef51SXin LI }
1184*4543ef51SXin LI if (i == 0)
1185*4543ef51SXin LI fail("Parse succeeded despite allocation failures");
1186*4543ef51SXin LI if (i == max_alloc_count)
1187*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1188*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_NOTATION_DECL_HANDLER_FLAG)
1189*4543ef51SXin LI fail("Notation handler not called");
1190*4543ef51SXin LI }
1191*4543ef51SXin LI END_TEST
1192*4543ef51SXin LI
1193*4543ef51SXin LI /* Test public notation with dodgy allocator */
START_TEST(test_alloc_system_notation)1194*4543ef51SXin LI START_TEST(test_alloc_system_notation) {
1195*4543ef51SXin LI const char *text
1196*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1197*4543ef51SXin LI "<!NOTATION note SYSTEM '"
1198*4543ef51SXin LI /* 64 characters per line */
1199*4543ef51SXin LI "http://example.com/a/long/enough/name/to/trigger/pool/growth/zz/"
1200*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1201*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1202*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1203*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1204*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1205*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1206*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1207*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1208*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1209*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1210*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1211*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1212*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1213*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1214*4543ef51SXin LI "ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/ABCDEFGHIJKLMNO/"
1215*4543ef51SXin LI "'>\n"
1216*4543ef51SXin LI "<!ENTITY e SYSTEM 'http://example.com/e' NDATA note>\n"
1217*4543ef51SXin LI "<!ELEMENT doc EMPTY>\n"
1218*4543ef51SXin LI "]>\n<doc/>";
1219*4543ef51SXin LI int i;
1220*4543ef51SXin LI const int max_alloc_count = 20;
1221*4543ef51SXin LI
1222*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1223*4543ef51SXin LI g_allocation_count = i;
1224*4543ef51SXin LI init_dummy_handlers();
1225*4543ef51SXin LI XML_SetNotationDeclHandler(g_parser, dummy_notation_decl_handler);
1226*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1227*4543ef51SXin LI != XML_STATUS_ERROR)
1228*4543ef51SXin LI break;
1229*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1230*4543ef51SXin LI alloc_teardown();
1231*4543ef51SXin LI alloc_setup();
1232*4543ef51SXin LI }
1233*4543ef51SXin LI if (i == 0)
1234*4543ef51SXin LI fail("Parse succeeded despite allocation failures");
1235*4543ef51SXin LI if (i == max_alloc_count)
1236*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1237*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_NOTATION_DECL_HANDLER_FLAG)
1238*4543ef51SXin LI fail("Notation handler not called");
1239*4543ef51SXin LI }
1240*4543ef51SXin LI END_TEST
1241*4543ef51SXin LI
START_TEST(test_alloc_nested_groups)1242*4543ef51SXin LI START_TEST(test_alloc_nested_groups) {
1243*4543ef51SXin LI const char *text
1244*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1245*4543ef51SXin LI "<!ELEMENT doc "
1246*4543ef51SXin LI /* Sixteen elements per line */
1247*4543ef51SXin LI "(e,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,"
1248*4543ef51SXin LI "(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?"
1249*4543ef51SXin LI "))))))))))))))))))))))))))))))))>\n"
1250*4543ef51SXin LI "<!ELEMENT e EMPTY>"
1251*4543ef51SXin LI "]>\n"
1252*4543ef51SXin LI "<doc><e/></doc>";
1253*4543ef51SXin LI CharData storage;
1254*4543ef51SXin LI int i;
1255*4543ef51SXin LI const int max_alloc_count = 20;
1256*4543ef51SXin LI
1257*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1258*4543ef51SXin LI g_allocation_count = i;
1259*4543ef51SXin LI CharData_Init(&storage);
1260*4543ef51SXin LI XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
1261*4543ef51SXin LI XML_SetStartElementHandler(g_parser, record_element_start_handler);
1262*4543ef51SXin LI XML_SetUserData(g_parser, &storage);
1263*4543ef51SXin LI init_dummy_handlers();
1264*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1265*4543ef51SXin LI != XML_STATUS_ERROR)
1266*4543ef51SXin LI break;
1267*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1268*4543ef51SXin LI alloc_teardown();
1269*4543ef51SXin LI alloc_setup();
1270*4543ef51SXin LI }
1271*4543ef51SXin LI
1272*4543ef51SXin LI if (i == 0)
1273*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1274*4543ef51SXin LI if (i == max_alloc_count)
1275*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1276*4543ef51SXin LI CharData_CheckXMLChars(&storage, XCS("doce"));
1277*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_ELEMENT_DECL_HANDLER_FLAG)
1278*4543ef51SXin LI fail("Element handler not fired");
1279*4543ef51SXin LI }
1280*4543ef51SXin LI END_TEST
1281*4543ef51SXin LI
START_TEST(test_alloc_realloc_nested_groups)1282*4543ef51SXin LI START_TEST(test_alloc_realloc_nested_groups) {
1283*4543ef51SXin LI const char *text
1284*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1285*4543ef51SXin LI "<!ELEMENT doc "
1286*4543ef51SXin LI /* Sixteen elements per line */
1287*4543ef51SXin LI "(e,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,"
1288*4543ef51SXin LI "(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?,(e?"
1289*4543ef51SXin LI "))))))))))))))))))))))))))))))))>\n"
1290*4543ef51SXin LI "<!ELEMENT e EMPTY>"
1291*4543ef51SXin LI "]>\n"
1292*4543ef51SXin LI "<doc><e/></doc>";
1293*4543ef51SXin LI CharData storage;
1294*4543ef51SXin LI int i;
1295*4543ef51SXin LI const int max_realloc_count = 10;
1296*4543ef51SXin LI
1297*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1298*4543ef51SXin LI g_reallocation_count = i;
1299*4543ef51SXin LI CharData_Init(&storage);
1300*4543ef51SXin LI XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
1301*4543ef51SXin LI XML_SetStartElementHandler(g_parser, record_element_start_handler);
1302*4543ef51SXin LI XML_SetUserData(g_parser, &storage);
1303*4543ef51SXin LI init_dummy_handlers();
1304*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1305*4543ef51SXin LI != XML_STATUS_ERROR)
1306*4543ef51SXin LI break;
1307*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1308*4543ef51SXin LI alloc_teardown();
1309*4543ef51SXin LI alloc_setup();
1310*4543ef51SXin LI }
1311*4543ef51SXin LI
1312*4543ef51SXin LI if (i == 0)
1313*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1314*4543ef51SXin LI if (i == max_realloc_count)
1315*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1316*4543ef51SXin LI CharData_CheckXMLChars(&storage, XCS("doce"));
1317*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_ELEMENT_DECL_HANDLER_FLAG)
1318*4543ef51SXin LI fail("Element handler not fired");
1319*4543ef51SXin LI }
1320*4543ef51SXin LI END_TEST
1321*4543ef51SXin LI
START_TEST(test_alloc_large_group)1322*4543ef51SXin LI START_TEST(test_alloc_large_group) {
1323*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
1324*4543ef51SXin LI "<!ELEMENT doc ("
1325*4543ef51SXin LI "a1|a2|a3|a4|a5|a6|a7|a8|"
1326*4543ef51SXin LI "b1|b2|b3|b4|b5|b6|b7|b8|"
1327*4543ef51SXin LI "c1|c2|c3|c4|c5|c6|c7|c8|"
1328*4543ef51SXin LI "d1|d2|d3|d4|d5|d6|d7|d8|"
1329*4543ef51SXin LI "e1"
1330*4543ef51SXin LI ")+>\n"
1331*4543ef51SXin LI "]>\n"
1332*4543ef51SXin LI "<doc>\n"
1333*4543ef51SXin LI "<a1/>\n"
1334*4543ef51SXin LI "</doc>\n";
1335*4543ef51SXin LI int i;
1336*4543ef51SXin LI const int max_alloc_count = 50;
1337*4543ef51SXin LI
1338*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1339*4543ef51SXin LI g_allocation_count = i;
1340*4543ef51SXin LI XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
1341*4543ef51SXin LI init_dummy_handlers();
1342*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1343*4543ef51SXin LI != XML_STATUS_ERROR)
1344*4543ef51SXin LI break;
1345*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1346*4543ef51SXin LI alloc_teardown();
1347*4543ef51SXin LI alloc_setup();
1348*4543ef51SXin LI }
1349*4543ef51SXin LI if (i == 0)
1350*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
1351*4543ef51SXin LI if (i == max_alloc_count)
1352*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1353*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_ELEMENT_DECL_HANDLER_FLAG)
1354*4543ef51SXin LI fail("Element handler flag not raised");
1355*4543ef51SXin LI }
1356*4543ef51SXin LI END_TEST
1357*4543ef51SXin LI
START_TEST(test_alloc_realloc_group_choice)1358*4543ef51SXin LI START_TEST(test_alloc_realloc_group_choice) {
1359*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
1360*4543ef51SXin LI "<!ELEMENT doc ("
1361*4543ef51SXin LI "a1|a2|a3|a4|a5|a6|a7|a8|"
1362*4543ef51SXin LI "b1|b2|b3|b4|b5|b6|b7|b8|"
1363*4543ef51SXin LI "c1|c2|c3|c4|c5|c6|c7|c8|"
1364*4543ef51SXin LI "d1|d2|d3|d4|d5|d6|d7|d8|"
1365*4543ef51SXin LI "e1"
1366*4543ef51SXin LI ")+>\n"
1367*4543ef51SXin LI "]>\n"
1368*4543ef51SXin LI "<doc>\n"
1369*4543ef51SXin LI "<a1/>\n"
1370*4543ef51SXin LI "<b2 attr='foo'>This is a foo</b2>\n"
1371*4543ef51SXin LI "<c3></c3>\n"
1372*4543ef51SXin LI "</doc>\n";
1373*4543ef51SXin LI int i;
1374*4543ef51SXin LI const int max_realloc_count = 10;
1375*4543ef51SXin LI
1376*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1377*4543ef51SXin LI g_reallocation_count = i;
1378*4543ef51SXin LI XML_SetElementDeclHandler(g_parser, dummy_element_decl_handler);
1379*4543ef51SXin LI init_dummy_handlers();
1380*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1381*4543ef51SXin LI != XML_STATUS_ERROR)
1382*4543ef51SXin LI break;
1383*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1384*4543ef51SXin LI alloc_teardown();
1385*4543ef51SXin LI alloc_setup();
1386*4543ef51SXin LI }
1387*4543ef51SXin LI if (i == 0)
1388*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1389*4543ef51SXin LI if (i == max_realloc_count)
1390*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1391*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_ELEMENT_DECL_HANDLER_FLAG)
1392*4543ef51SXin LI fail("Element handler flag not raised");
1393*4543ef51SXin LI }
1394*4543ef51SXin LI END_TEST
1395*4543ef51SXin LI
START_TEST(test_alloc_pi_in_epilog)1396*4543ef51SXin LI START_TEST(test_alloc_pi_in_epilog) {
1397*4543ef51SXin LI const char *text = "<doc></doc>\n"
1398*4543ef51SXin LI "<?pi in epilog?>";
1399*4543ef51SXin LI int i;
1400*4543ef51SXin LI const int max_alloc_count = 15;
1401*4543ef51SXin LI
1402*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1403*4543ef51SXin LI g_allocation_count = i;
1404*4543ef51SXin LI XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler);
1405*4543ef51SXin LI init_dummy_handlers();
1406*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1407*4543ef51SXin LI != XML_STATUS_ERROR)
1408*4543ef51SXin LI break;
1409*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1410*4543ef51SXin LI alloc_teardown();
1411*4543ef51SXin LI alloc_setup();
1412*4543ef51SXin LI }
1413*4543ef51SXin LI if (i == 0)
1414*4543ef51SXin LI fail("Parse completed despite failing allocator");
1415*4543ef51SXin LI if (i == max_alloc_count)
1416*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1417*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_PI_HANDLER_FLAG)
1418*4543ef51SXin LI fail("Processing instruction handler not invoked");
1419*4543ef51SXin LI }
1420*4543ef51SXin LI END_TEST
1421*4543ef51SXin LI
START_TEST(test_alloc_comment_in_epilog)1422*4543ef51SXin LI START_TEST(test_alloc_comment_in_epilog) {
1423*4543ef51SXin LI const char *text = "<doc></doc>\n"
1424*4543ef51SXin LI "<!-- comment in epilog -->";
1425*4543ef51SXin LI int i;
1426*4543ef51SXin LI const int max_alloc_count = 15;
1427*4543ef51SXin LI
1428*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1429*4543ef51SXin LI g_allocation_count = i;
1430*4543ef51SXin LI XML_SetCommentHandler(g_parser, dummy_comment_handler);
1431*4543ef51SXin LI init_dummy_handlers();
1432*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1433*4543ef51SXin LI != XML_STATUS_ERROR)
1434*4543ef51SXin LI break;
1435*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1436*4543ef51SXin LI alloc_teardown();
1437*4543ef51SXin LI alloc_setup();
1438*4543ef51SXin LI }
1439*4543ef51SXin LI if (i == 0)
1440*4543ef51SXin LI fail("Parse completed despite failing allocator");
1441*4543ef51SXin LI if (i == max_alloc_count)
1442*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1443*4543ef51SXin LI if (get_dummy_handler_flags() != DUMMY_COMMENT_HANDLER_FLAG)
1444*4543ef51SXin LI fail("Processing instruction handler not invoked");
1445*4543ef51SXin LI }
1446*4543ef51SXin LI END_TEST
1447*4543ef51SXin LI
START_TEST(test_alloc_realloc_long_attribute_value)1448*4543ef51SXin LI START_TEST(test_alloc_realloc_long_attribute_value) {
1449*4543ef51SXin LI const char *text
1450*4543ef51SXin LI = "<!DOCTYPE doc [<!ENTITY foo '"
1451*4543ef51SXin LI /* Each line is 64 characters */
1452*4543ef51SXin LI "This entity will be substituted as an attribute value, and is "
1453*4543ef51SXin LI "calculated to be exactly long enough that the terminating NUL "
1454*4543ef51SXin LI "that the library adds internally will trigger the string pool to"
1455*4543ef51SXin LI "grow. GHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1456*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1457*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1458*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1459*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1460*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1461*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1462*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1463*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1464*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1465*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1466*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1467*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1468*4543ef51SXin LI "'>]>\n"
1469*4543ef51SXin LI "<doc a='&foo;'></doc>";
1470*4543ef51SXin LI int i;
1471*4543ef51SXin LI const int max_realloc_count = 10;
1472*4543ef51SXin LI
1473*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1474*4543ef51SXin LI g_reallocation_count = i;
1475*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1476*4543ef51SXin LI != XML_STATUS_ERROR)
1477*4543ef51SXin LI break;
1478*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1479*4543ef51SXin LI alloc_teardown();
1480*4543ef51SXin LI alloc_setup();
1481*4543ef51SXin LI }
1482*4543ef51SXin LI if (i == 0)
1483*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1484*4543ef51SXin LI if (i == max_realloc_count)
1485*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1486*4543ef51SXin LI }
1487*4543ef51SXin LI END_TEST
1488*4543ef51SXin LI
START_TEST(test_alloc_attribute_whitespace)1489*4543ef51SXin LI START_TEST(test_alloc_attribute_whitespace) {
1490*4543ef51SXin LI const char *text = "<doc a=' '></doc>";
1491*4543ef51SXin LI int i;
1492*4543ef51SXin LI const int max_alloc_count = 15;
1493*4543ef51SXin LI
1494*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1495*4543ef51SXin LI g_allocation_count = i;
1496*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1497*4543ef51SXin LI != XML_STATUS_ERROR)
1498*4543ef51SXin LI break;
1499*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1500*4543ef51SXin LI alloc_teardown();
1501*4543ef51SXin LI alloc_setup();
1502*4543ef51SXin LI }
1503*4543ef51SXin LI if (i == 0)
1504*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
1505*4543ef51SXin LI if (i == max_alloc_count)
1506*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1507*4543ef51SXin LI }
1508*4543ef51SXin LI END_TEST
1509*4543ef51SXin LI
START_TEST(test_alloc_attribute_predefined_entity)1510*4543ef51SXin LI START_TEST(test_alloc_attribute_predefined_entity) {
1511*4543ef51SXin LI const char *text = "<doc a='&'></doc>";
1512*4543ef51SXin LI int i;
1513*4543ef51SXin LI const int max_alloc_count = 15;
1514*4543ef51SXin LI
1515*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1516*4543ef51SXin LI g_allocation_count = i;
1517*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1518*4543ef51SXin LI != XML_STATUS_ERROR)
1519*4543ef51SXin LI break;
1520*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1521*4543ef51SXin LI alloc_teardown();
1522*4543ef51SXin LI alloc_setup();
1523*4543ef51SXin LI }
1524*4543ef51SXin LI if (i == 0)
1525*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
1526*4543ef51SXin LI if (i == max_alloc_count)
1527*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1528*4543ef51SXin LI }
1529*4543ef51SXin LI END_TEST
1530*4543ef51SXin LI
1531*4543ef51SXin LI /* Test that a character reference at the end of a suitably long
1532*4543ef51SXin LI * default value for an attribute can trigger pool growth, and recovers
1533*4543ef51SXin LI * if the allocator fails on it.
1534*4543ef51SXin LI */
START_TEST(test_alloc_long_attr_default_with_char_ref)1535*4543ef51SXin LI START_TEST(test_alloc_long_attr_default_with_char_ref) {
1536*4543ef51SXin LI const char *text
1537*4543ef51SXin LI = "<!DOCTYPE doc [<!ATTLIST doc a CDATA '"
1538*4543ef51SXin LI /* 64 characters per line */
1539*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1540*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1541*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1542*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1543*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1544*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1545*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1546*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1547*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1548*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1549*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1550*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1551*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1552*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1553*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1554*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHI"
1555*4543ef51SXin LI "1'>]>\n"
1556*4543ef51SXin LI "<doc/>";
1557*4543ef51SXin LI int i;
1558*4543ef51SXin LI const int max_alloc_count = 20;
1559*4543ef51SXin LI
1560*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1561*4543ef51SXin LI g_allocation_count = i;
1562*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1563*4543ef51SXin LI != XML_STATUS_ERROR)
1564*4543ef51SXin LI break;
1565*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1566*4543ef51SXin LI alloc_teardown();
1567*4543ef51SXin LI alloc_setup();
1568*4543ef51SXin LI }
1569*4543ef51SXin LI if (i == 0)
1570*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
1571*4543ef51SXin LI if (i == max_alloc_count)
1572*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1573*4543ef51SXin LI }
1574*4543ef51SXin LI END_TEST
1575*4543ef51SXin LI
1576*4543ef51SXin LI /* Test that a long character reference substitution triggers a pool
1577*4543ef51SXin LI * expansion correctly for an attribute value.
1578*4543ef51SXin LI */
START_TEST(test_alloc_long_attr_value)1579*4543ef51SXin LI START_TEST(test_alloc_long_attr_value) {
1580*4543ef51SXin LI const char *text
1581*4543ef51SXin LI = "<!DOCTYPE test [<!ENTITY foo '\n"
1582*4543ef51SXin LI /* 64 characters per line */
1583*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1584*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1585*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1586*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1587*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1588*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1589*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1590*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1591*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1592*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1593*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1594*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1595*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1596*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1597*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1598*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1599*4543ef51SXin LI "'>]>\n"
1600*4543ef51SXin LI "<test a='&foo;'/>";
1601*4543ef51SXin LI int i;
1602*4543ef51SXin LI const int max_alloc_count = 25;
1603*4543ef51SXin LI
1604*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1605*4543ef51SXin LI g_allocation_count = i;
1606*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1607*4543ef51SXin LI != XML_STATUS_ERROR)
1608*4543ef51SXin LI break;
1609*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1610*4543ef51SXin LI alloc_teardown();
1611*4543ef51SXin LI alloc_setup();
1612*4543ef51SXin LI }
1613*4543ef51SXin LI if (i == 0)
1614*4543ef51SXin LI fail("Parse succeeded despite failing allocator");
1615*4543ef51SXin LI if (i == max_alloc_count)
1616*4543ef51SXin LI fail("Parse failed at maximum allocation count");
1617*4543ef51SXin LI }
1618*4543ef51SXin LI END_TEST
1619*4543ef51SXin LI
1620*4543ef51SXin LI /* Test that an error in a nested parameter entity substitution is
1621*4543ef51SXin LI * handled correctly. It seems unlikely that the code path being
1622*4543ef51SXin LI * exercised can be reached purely by carefully crafted XML, but an
1623*4543ef51SXin LI * allocation error in the right place will definitely do it.
1624*4543ef51SXin LI */
START_TEST(test_alloc_nested_entities)1625*4543ef51SXin LI START_TEST(test_alloc_nested_entities) {
1626*4543ef51SXin LI const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/one.ent'>\n"
1627*4543ef51SXin LI "<doc />";
1628*4543ef51SXin LI ExtFaults test_data
1629*4543ef51SXin LI = {"<!ENTITY % pe1 '"
1630*4543ef51SXin LI /* 64 characters per line */
1631*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1632*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1633*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1634*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1635*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1636*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1637*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1638*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1639*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1640*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1641*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1642*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1643*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1644*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1645*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1646*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1647*4543ef51SXin LI "'>\n"
1648*4543ef51SXin LI "<!ENTITY % pe2 '%pe1;'>\n"
1649*4543ef51SXin LI "<!ENTITY % pe3 '%pe2;'>",
1650*4543ef51SXin LI "Memory Fail not faulted", NULL, XML_ERROR_NO_MEMORY};
1651*4543ef51SXin LI
1652*4543ef51SXin LI /* Causes an allocation error in a nested storeEntityValue() */
1653*4543ef51SXin LI g_allocation_count = 12;
1654*4543ef51SXin LI XML_SetUserData(g_parser, &test_data);
1655*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1656*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_faulter);
1657*4543ef51SXin LI expect_failure(text, XML_ERROR_EXTERNAL_ENTITY_HANDLING,
1658*4543ef51SXin LI "Entity allocation failure not noted");
1659*4543ef51SXin LI }
1660*4543ef51SXin LI END_TEST
1661*4543ef51SXin LI
START_TEST(test_alloc_realloc_param_entity_newline)1662*4543ef51SXin LI START_TEST(test_alloc_realloc_param_entity_newline) {
1663*4543ef51SXin LI const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/'>\n"
1664*4543ef51SXin LI "<doc/>";
1665*4543ef51SXin LI char dtd_text[]
1666*4543ef51SXin LI = "<!ENTITY % pe '<!ATTLIST doc att CDATA \""
1667*4543ef51SXin LI /* 64 characters per line */
1668*4543ef51SXin LI "This default value is carefully crafted so that the carriage "
1669*4543ef51SXin LI "return right at the end of the entity string causes an internal "
1670*4543ef51SXin LI "string pool to have to grow. This allows us to test the alloc "
1671*4543ef51SXin LI "failure path from that point. OPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1672*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1673*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1674*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1675*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1676*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1677*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1678*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1679*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1680*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1681*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1682*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1683*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDE"
1684*4543ef51SXin LI "\">\n'>"
1685*4543ef51SXin LI "%pe;\n";
1686*4543ef51SXin LI int i;
1687*4543ef51SXin LI const int max_realloc_count = 5;
1688*4543ef51SXin LI
1689*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1690*4543ef51SXin LI g_reallocation_count = i;
1691*4543ef51SXin LI XML_SetUserData(g_parser, dtd_text);
1692*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1693*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
1694*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1695*4543ef51SXin LI != XML_STATUS_ERROR)
1696*4543ef51SXin LI break;
1697*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1698*4543ef51SXin LI alloc_teardown();
1699*4543ef51SXin LI alloc_setup();
1700*4543ef51SXin LI }
1701*4543ef51SXin LI if (i == 0)
1702*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1703*4543ef51SXin LI if (i == max_realloc_count)
1704*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1705*4543ef51SXin LI }
1706*4543ef51SXin LI END_TEST
1707*4543ef51SXin LI
START_TEST(test_alloc_realloc_ce_extends_pe)1708*4543ef51SXin LI START_TEST(test_alloc_realloc_ce_extends_pe) {
1709*4543ef51SXin LI const char *text = "<!DOCTYPE doc SYSTEM 'http://example.org/'>\n"
1710*4543ef51SXin LI "<doc/>";
1711*4543ef51SXin LI char dtd_text[]
1712*4543ef51SXin LI = "<!ENTITY % pe '<!ATTLIST doc att CDATA \""
1713*4543ef51SXin LI /* 64 characters per line */
1714*4543ef51SXin LI "This default value is carefully crafted so that the character "
1715*4543ef51SXin LI "entity at the end causes an internal string pool to have to "
1716*4543ef51SXin LI "grow. This allows us to test the allocation failure path from "
1717*4543ef51SXin LI "that point onwards. EFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1718*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1719*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1720*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1721*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1722*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1723*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1724*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1725*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1726*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1727*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1728*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGHIJKLMNOP"
1729*4543ef51SXin LI "ABCDEFGHIJKLMNOPABCDEFGHIJKLMNOPABCDEFGQ"
1730*4543ef51SXin LI "\">\n'>"
1731*4543ef51SXin LI "%pe;\n";
1732*4543ef51SXin LI int i;
1733*4543ef51SXin LI const int max_realloc_count = 5;
1734*4543ef51SXin LI
1735*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1736*4543ef51SXin LI g_reallocation_count = i;
1737*4543ef51SXin LI XML_SetUserData(g_parser, dtd_text);
1738*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1739*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
1740*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1741*4543ef51SXin LI != XML_STATUS_ERROR)
1742*4543ef51SXin LI break;
1743*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1744*4543ef51SXin LI alloc_teardown();
1745*4543ef51SXin LI alloc_setup();
1746*4543ef51SXin LI }
1747*4543ef51SXin LI if (i == 0)
1748*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1749*4543ef51SXin LI if (i == max_realloc_count)
1750*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1751*4543ef51SXin LI }
1752*4543ef51SXin LI END_TEST
1753*4543ef51SXin LI
START_TEST(test_alloc_realloc_attributes)1754*4543ef51SXin LI START_TEST(test_alloc_realloc_attributes) {
1755*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
1756*4543ef51SXin LI " <!ATTLIST doc\n"
1757*4543ef51SXin LI " a1 (a|b|c) 'a'\n"
1758*4543ef51SXin LI " a2 (foo|bar) #IMPLIED\n"
1759*4543ef51SXin LI " a3 NMTOKEN #IMPLIED\n"
1760*4543ef51SXin LI " a4 NMTOKENS #IMPLIED\n"
1761*4543ef51SXin LI " a5 ID #IMPLIED\n"
1762*4543ef51SXin LI " a6 IDREF #IMPLIED\n"
1763*4543ef51SXin LI " a7 IDREFS #IMPLIED\n"
1764*4543ef51SXin LI " a8 ENTITY #IMPLIED\n"
1765*4543ef51SXin LI " a9 ENTITIES #IMPLIED\n"
1766*4543ef51SXin LI " a10 CDATA #IMPLIED\n"
1767*4543ef51SXin LI " >]>\n"
1768*4543ef51SXin LI "<doc>wombat</doc>\n";
1769*4543ef51SXin LI int i;
1770*4543ef51SXin LI const int max_realloc_count = 5;
1771*4543ef51SXin LI
1772*4543ef51SXin LI for (i = 0; i < max_realloc_count; i++) {
1773*4543ef51SXin LI g_reallocation_count = i;
1774*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1775*4543ef51SXin LI != XML_STATUS_ERROR)
1776*4543ef51SXin LI break;
1777*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1778*4543ef51SXin LI alloc_teardown();
1779*4543ef51SXin LI alloc_setup();
1780*4543ef51SXin LI }
1781*4543ef51SXin LI
1782*4543ef51SXin LI if (i == 0)
1783*4543ef51SXin LI fail("Parse succeeded despite failing reallocator");
1784*4543ef51SXin LI if (i == max_realloc_count)
1785*4543ef51SXin LI fail("Parse failed at maximum reallocation count");
1786*4543ef51SXin LI }
1787*4543ef51SXin LI END_TEST
1788*4543ef51SXin LI
START_TEST(test_alloc_long_doc_name)1789*4543ef51SXin LI START_TEST(test_alloc_long_doc_name) {
1790*4543ef51SXin LI const char *text =
1791*4543ef51SXin LI /* 64 characters per line */
1792*4543ef51SXin LI "<LongRootElementNameThatWillCauseTheNextAllocationToExpandTheStr"
1793*4543ef51SXin LI "ingPoolForTheDTDQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1794*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1795*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1796*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1797*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1798*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1799*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1800*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1801*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1802*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1803*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1804*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1805*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1806*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1807*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AZ"
1808*4543ef51SXin LI " a='1'/>";
1809*4543ef51SXin LI int i;
1810*4543ef51SXin LI const int max_alloc_count = 20;
1811*4543ef51SXin LI
1812*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1813*4543ef51SXin LI g_allocation_count = i;
1814*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1815*4543ef51SXin LI != XML_STATUS_ERROR)
1816*4543ef51SXin LI break;
1817*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1818*4543ef51SXin LI alloc_teardown();
1819*4543ef51SXin LI alloc_setup();
1820*4543ef51SXin LI }
1821*4543ef51SXin LI if (i == 0)
1822*4543ef51SXin LI fail("Parsing worked despite failing reallocations");
1823*4543ef51SXin LI else if (i == max_alloc_count)
1824*4543ef51SXin LI fail("Parsing failed even at max reallocation count");
1825*4543ef51SXin LI }
1826*4543ef51SXin LI END_TEST
1827*4543ef51SXin LI
START_TEST(test_alloc_long_base)1828*4543ef51SXin LI START_TEST(test_alloc_long_base) {
1829*4543ef51SXin LI const char *text = "<!DOCTYPE doc [\n"
1830*4543ef51SXin LI " <!ENTITY e SYSTEM 'foo'>\n"
1831*4543ef51SXin LI "]>\n"
1832*4543ef51SXin LI "<doc>&e;</doc>";
1833*4543ef51SXin LI char entity_text[] = "Hello world";
1834*4543ef51SXin LI const XML_Char *base =
1835*4543ef51SXin LI /* 64 characters per line */
1836*4543ef51SXin LI /* clang-format off */
1837*4543ef51SXin LI XCS("LongBaseURI/that/will/overflow/an/internal/buffer/and/cause/it/t")
1838*4543ef51SXin LI XCS("o/have/to/grow/PQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1839*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1840*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1841*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1842*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1843*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1844*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1845*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1846*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1847*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1848*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1849*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1850*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1851*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/")
1852*4543ef51SXin LI XCS("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789A/");
1853*4543ef51SXin LI /* clang-format on */
1854*4543ef51SXin LI int i;
1855*4543ef51SXin LI const int max_alloc_count = 25;
1856*4543ef51SXin LI
1857*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1858*4543ef51SXin LI g_allocation_count = i;
1859*4543ef51SXin LI XML_SetUserData(g_parser, entity_text);
1860*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1861*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
1862*4543ef51SXin LI if (XML_SetBase(g_parser, base) == XML_STATUS_ERROR) {
1863*4543ef51SXin LI XML_ParserReset(g_parser, NULL);
1864*4543ef51SXin LI continue;
1865*4543ef51SXin LI }
1866*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1867*4543ef51SXin LI != XML_STATUS_ERROR)
1868*4543ef51SXin LI break;
1869*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1870*4543ef51SXin LI alloc_teardown();
1871*4543ef51SXin LI alloc_setup();
1872*4543ef51SXin LI }
1873*4543ef51SXin LI if (i == 0)
1874*4543ef51SXin LI fail("Parsing worked despite failing allocations");
1875*4543ef51SXin LI else if (i == max_alloc_count)
1876*4543ef51SXin LI fail("Parsing failed even at max allocation count");
1877*4543ef51SXin LI }
1878*4543ef51SXin LI END_TEST
1879*4543ef51SXin LI
START_TEST(test_alloc_long_public_id)1880*4543ef51SXin LI START_TEST(test_alloc_long_public_id) {
1881*4543ef51SXin LI const char *text
1882*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1883*4543ef51SXin LI " <!ENTITY e PUBLIC '"
1884*4543ef51SXin LI /* 64 characters per line */
1885*4543ef51SXin LI "LongPublicIDThatShouldResultInAnInternalStringPoolGrowingAtASpec"
1886*4543ef51SXin LI "ificMomentKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1887*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1888*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1889*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1890*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1891*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1892*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1893*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1894*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1895*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1896*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1897*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1898*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1899*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1900*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1901*4543ef51SXin LI "' 'bar'>\n"
1902*4543ef51SXin LI "]>\n"
1903*4543ef51SXin LI "<doc>&e;</doc>";
1904*4543ef51SXin LI char entity_text[] = "Hello world";
1905*4543ef51SXin LI int i;
1906*4543ef51SXin LI const int max_alloc_count = 40;
1907*4543ef51SXin LI
1908*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1909*4543ef51SXin LI g_allocation_count = i;
1910*4543ef51SXin LI XML_SetUserData(g_parser, entity_text);
1911*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1912*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
1913*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1914*4543ef51SXin LI != XML_STATUS_ERROR)
1915*4543ef51SXin LI break;
1916*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1917*4543ef51SXin LI alloc_teardown();
1918*4543ef51SXin LI alloc_setup();
1919*4543ef51SXin LI }
1920*4543ef51SXin LI if (i == 0)
1921*4543ef51SXin LI fail("Parsing worked despite failing allocations");
1922*4543ef51SXin LI else if (i == max_alloc_count)
1923*4543ef51SXin LI fail("Parsing failed even at max allocation count");
1924*4543ef51SXin LI }
1925*4543ef51SXin LI END_TEST
1926*4543ef51SXin LI
START_TEST(test_alloc_long_entity_value)1927*4543ef51SXin LI START_TEST(test_alloc_long_entity_value) {
1928*4543ef51SXin LI const char *text
1929*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1930*4543ef51SXin LI " <!ENTITY e1 '"
1931*4543ef51SXin LI /* 64 characters per line */
1932*4543ef51SXin LI "Long entity value that should provoke a string pool to grow whil"
1933*4543ef51SXin LI "e setting up to parse the external entity below. xyz0123456789AB"
1934*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1935*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1936*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1937*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1938*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1939*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1940*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1941*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1942*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1943*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1944*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1945*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1946*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1947*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1948*4543ef51SXin LI "'>\n"
1949*4543ef51SXin LI " <!ENTITY e2 SYSTEM 'bar'>\n"
1950*4543ef51SXin LI "]>\n"
1951*4543ef51SXin LI "<doc>&e2;</doc>";
1952*4543ef51SXin LI char entity_text[] = "Hello world";
1953*4543ef51SXin LI int i;
1954*4543ef51SXin LI const int max_alloc_count = 40;
1955*4543ef51SXin LI
1956*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
1957*4543ef51SXin LI g_allocation_count = i;
1958*4543ef51SXin LI XML_SetUserData(g_parser, entity_text);
1959*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
1960*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_alloc);
1961*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
1962*4543ef51SXin LI != XML_STATUS_ERROR)
1963*4543ef51SXin LI break;
1964*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
1965*4543ef51SXin LI alloc_teardown();
1966*4543ef51SXin LI alloc_setup();
1967*4543ef51SXin LI }
1968*4543ef51SXin LI if (i == 0)
1969*4543ef51SXin LI fail("Parsing worked despite failing allocations");
1970*4543ef51SXin LI else if (i == max_alloc_count)
1971*4543ef51SXin LI fail("Parsing failed even at max allocation count");
1972*4543ef51SXin LI }
1973*4543ef51SXin LI END_TEST
1974*4543ef51SXin LI
START_TEST(test_alloc_long_notation)1975*4543ef51SXin LI START_TEST(test_alloc_long_notation) {
1976*4543ef51SXin LI const char *text
1977*4543ef51SXin LI = "<!DOCTYPE doc [\n"
1978*4543ef51SXin LI " <!NOTATION note SYSTEM '"
1979*4543ef51SXin LI /* 64 characters per line */
1980*4543ef51SXin LI "ALongNotationNameThatShouldProvokeStringPoolGrowthWhileCallingAn"
1981*4543ef51SXin LI "ExternalEntityParserUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1982*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1983*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1984*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1985*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1986*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1987*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1988*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1989*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1990*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1991*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1992*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1993*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1994*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1995*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
1996*4543ef51SXin LI "'>\n"
1997*4543ef51SXin LI " <!ENTITY e1 SYSTEM 'foo' NDATA "
1998*4543ef51SXin LI /* 64 characters per line */
1999*4543ef51SXin LI "ALongNotationNameThatShouldProvokeStringPoolGrowthWhileCallingAn"
2000*4543ef51SXin LI "ExternalEntityParserUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2001*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2002*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2003*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2004*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2005*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2006*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2007*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2008*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2009*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2010*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2011*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2012*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2013*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2014*4543ef51SXin LI "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789AB"
2015*4543ef51SXin LI ">\n"
2016*4543ef51SXin LI " <!ENTITY e2 SYSTEM 'bar'>\n"
2017*4543ef51SXin LI "]>\n"
2018*4543ef51SXin LI "<doc>&e2;</doc>";
2019*4543ef51SXin LI ExtOption options[]
2020*4543ef51SXin LI = {{XCS("foo"), "Entity Foo"}, {XCS("bar"), "Entity Bar"}, {NULL, NULL}};
2021*4543ef51SXin LI int i;
2022*4543ef51SXin LI const int max_alloc_count = 40;
2023*4543ef51SXin LI
2024*4543ef51SXin LI for (i = 0; i < max_alloc_count; i++) {
2025*4543ef51SXin LI g_allocation_count = i;
2026*4543ef51SXin LI XML_SetUserData(g_parser, options);
2027*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2028*4543ef51SXin LI XML_SetExternalEntityRefHandler(g_parser, external_entity_optioner);
2029*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2030*4543ef51SXin LI != XML_STATUS_ERROR)
2031*4543ef51SXin LI break;
2032*4543ef51SXin LI
2033*4543ef51SXin LI /* See comment in test_alloc_parse_xdecl() */
2034*4543ef51SXin LI alloc_teardown();
2035*4543ef51SXin LI alloc_setup();
2036*4543ef51SXin LI }
2037*4543ef51SXin LI if (i == 0)
2038*4543ef51SXin LI fail("Parsing worked despite failing allocations");
2039*4543ef51SXin LI else if (i == max_alloc_count)
2040*4543ef51SXin LI fail("Parsing failed even at max allocation count");
2041*4543ef51SXin LI }
2042*4543ef51SXin LI END_TEST
2043*4543ef51SXin LI
START_TEST(test_alloc_reset_after_external_entity_parser_create_fail)2044*4543ef51SXin LI START_TEST(test_alloc_reset_after_external_entity_parser_create_fail) {
2045*4543ef51SXin LI const char *const text = "<!DOCTYPE doc SYSTEM 'foo'><doc/>";
2046*4543ef51SXin LI
2047*4543ef51SXin LI XML_SetExternalEntityRefHandler(
2048*4543ef51SXin LI g_parser, external_entity_parser_create_alloc_fail_handler);
2049*4543ef51SXin LI XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
2050*4543ef51SXin LI
2051*4543ef51SXin LI if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
2052*4543ef51SXin LI != XML_STATUS_ERROR)
2053*4543ef51SXin LI fail("Call to parse was expected to fail");
2054*4543ef51SXin LI
2055*4543ef51SXin LI if (XML_GetErrorCode(g_parser) != XML_ERROR_EXTERNAL_ENTITY_HANDLING)
2056*4543ef51SXin LI fail("Call to parse was expected to fail from the external entity handler");
2057*4543ef51SXin LI
2058*4543ef51SXin LI XML_ParserReset(g_parser, NULL);
2059*4543ef51SXin LI }
2060*4543ef51SXin LI END_TEST
2061*4543ef51SXin LI
2062*4543ef51SXin LI void
make_alloc_test_case(Suite * s)2063*4543ef51SXin LI make_alloc_test_case(Suite *s) {
2064*4543ef51SXin LI TCase *tc_alloc = tcase_create("allocation tests");
2065*4543ef51SXin LI
2066*4543ef51SXin LI suite_add_tcase(s, tc_alloc);
2067*4543ef51SXin LI tcase_add_checked_fixture(tc_alloc, alloc_setup, alloc_teardown);
2068*4543ef51SXin LI
2069*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_xdecl);
2070*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_xdecl_2);
2071*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_pi);
2072*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_pi_2);
2073*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_pi_3);
2074*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_comment);
2075*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_comment_2);
2076*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_create_external_parser);
2077*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_run_external_parser);
2078*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_dtd_copy_default_atts);
2079*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_external_entity);
2080*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_ext_entity_set_encoding);
2081*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_internal_entity);
2082*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_dtd_default_handling);
2083*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_explicit_encoding);
2084*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_set_base);
2085*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_realloc_buffer);
2086*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_ext_entity_realloc_buffer);
2087*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_realloc_many_attributes);
2088*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_public_entity_value);
2089*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc,
2090*4543ef51SXin LI test_alloc_realloc_subst_public_entity_value);
2091*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_public_doctype);
2092*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_parse_public_doctype_long_name);
2093*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_set_foreign_dtd);
2094*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_attribute_enum_value);
2095*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc,
2096*4543ef51SXin LI test_alloc_realloc_attribute_enum_value);
2097*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_realloc_implied_attribute);
2098*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_realloc_default_attribute);
2099*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_notation);
2100*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_public_notation);
2101*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_system_notation);
2102*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_nested_groups);
2103*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_realloc_nested_groups);
2104*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_large_group);
2105*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_realloc_group_choice);
2106*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_pi_in_epilog);
2107*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_comment_in_epilog);
2108*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc,
2109*4543ef51SXin LI test_alloc_realloc_long_attribute_value);
2110*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_attribute_whitespace);
2111*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_attribute_predefined_entity);
2112*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_long_attr_default_with_char_ref);
2113*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_long_attr_value);
2114*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_nested_entities);
2115*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc,
2116*4543ef51SXin LI test_alloc_realloc_param_entity_newline);
2117*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_realloc_ce_extends_pe);
2118*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_realloc_attributes);
2119*4543ef51SXin LI tcase_add_test(tc_alloc, test_alloc_long_doc_name);
2120*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_long_base);
2121*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_long_public_id);
2122*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_long_entity_value);
2123*4543ef51SXin LI tcase_add_test__if_xml_ge(tc_alloc, test_alloc_long_notation);
2124*4543ef51SXin LI
2125*4543ef51SXin LI tcase_add_test__ifdef_xml_dtd(
2126*4543ef51SXin LI tc_alloc, test_alloc_reset_after_external_entity_parser_create_fail);
2127*4543ef51SXin LI }
2128