2
* testOOM.c: Test out-of-memory handling
4
* See Copyright for the status of this software.
14
#ifdef HAVE_SYS_TYPES_H
15
#include <sys/types.h>
27
#include <libxml/xmlreader.h>
29
#include "testOOMlib.h"
48
* Since we are using the xmlTextReader functions, we set up
49
* strings for the element types to help in debugging any error
52
const char *elementNames[] = {
53
"XML_READER_TYPE_NONE",
54
"XML_READER_TYPE_ELEMENT",
55
"XML_READER_TYPE_ATTRIBUTE",
56
"XML_READER_TYPE_TEXT",
57
"XML_READER_TYPE_CDATA",
58
"XML_READER_TYPE_ENTITY_REFERENCE",
59
"XML_READER_TYPE_ENTITY",
60
"XML_READER_TYPE_PROCESSING_INSTRUCTION",
61
"XML_READER_TYPE_COMMENT",
62
"XML_READER_TYPE_DOCUMENT",
63
"XML_READER_TYPE_DOCUMENT_TYPE",
64
"XML_READER_TYPE_DOCUMENT_FRAGMENT",
65
"XML_READER_TYPE_NOTATION",
66
"XML_READER_TYPE_WHITESPACE",
67
"XML_READER_TYPE_SIGNIFICANT_WHITESPACE",
68
"XML_READER_TYPE_END_ELEMENT",
69
"XML_READER_TYPE_END_ENTITY",
70
"XML_READER_TYPE_XML_DECLARATION"};
72
/* not using xmlBuff here because I don't want those
73
* mallocs to interfere */
80
static struct buffer *buffer_create (size_t init_len)
83
b = malloc (sizeof *b);
87
b->str = malloc (init_len);
98
static void buffer_free (struct buffer *b)
104
static size_t buffer_get_length (struct buffer *b)
109
static void buffer_expand (struct buffer *b, size_t min)
112
size_t new_size = b->max ? b->max : 512;
113
while (new_size < b->len + min)
115
if (new_size > b->max) {
116
new_str = realloc (b->str, new_size);
124
static void buffer_add_char (struct buffer *b, char c)
126
buffer_expand (b, 1);
131
static void buffer_add_string (struct buffer *b, const char *s)
133
size_t size = strlen(s) + 1;
135
for (ix=0; ix<size-1; ix++) {
137
printf ("binary data [0x%02x]?\n", (unsigned char)s[ix]);
139
buffer_expand (b, size);
140
strcpy (b->str + b->len, s);
141
b->str[b->len+size-1] = '\n'; /* replace string term with newline */
145
static int buffer_equal (struct buffer *b1, struct buffer *b2)
147
return (b1->len == b2->len &&
148
(b1->len == 0 || (memcmp (b1->str, b2->str, b1->len) == 0)));
151
static void buffer_dump (struct buffer *b, const char *fname)
153
FILE *f = fopen (fname, "wb");
155
fwrite (b->str, 1, b->len, f);
161
static void usage(const char *progname) {
162
printf("Usage : %s [options] XMLfiles ...\n", progname);
163
printf("\tParse the XML files using the xmlTextReader API\n");
164
printf("\t --count: count the number of attribute and elements\n");
165
printf("\t --valid: validate the document\n");
166
printf("\t --show: display the error messages encountered\n");
169
static unsigned int elem, attrs, chars;
171
static int processNode (xmlTextReaderPtr reader, void *data)
173
struct buffer *buff = data;
176
type = xmlTextReaderNodeType(reader);
180
attrs += xmlTextReaderAttributeCount(reader);
181
} else if (type == 3) {
183
txt = xmlTextReaderConstValue(reader);
185
chars += xmlStrlen (txt);
195
buffer_add_string (buff, elementNames[type]);
198
s = (const char *)xmlTextReaderConstName (reader);
199
if (s == NULL) return FALSE;
200
buffer_add_string (buff, s);
201
while ((ret = xmlTextReaderMoveToNextAttribute (reader)) == 1) {
202
s = (const char *)xmlTextReaderConstName (reader);
203
if (s == NULL) return FALSE;
204
buffer_add_string (buff, s);
205
buffer_add_char (buff, '=');
206
s = (const char *)xmlTextReaderConstValue (reader);
207
if (s == NULL) return FALSE;
208
buffer_add_string (buff, s);
210
if (ret == -1) return FALSE;
212
else if (type == 3) {
213
s = (const char *)xmlTextReaderConstValue (reader);
214
if (s == NULL) return FALSE;
215
buffer_add_string (buff, s);
224
const char *filename;
225
struct buffer *verif_buff;
229
error_func (void *data ATTRIBUTE_UNUSED, xmlErrorPtr err)
233
if (err->level == XML_ERR_ERROR ||
234
err->level == XML_ERR_FATAL)
237
printf("%3d line %d: %s\n", error, err->line, err->message);
242
check_load_file_memory_func (void *data)
244
struct file_params *p = data;
246
xmlTextReaderPtr reader;
247
int ret, status, first_run;
255
first_run = p->verif_buff == NULL;
259
b = buffer_create (0);
261
b = buffer_create (buffer_get_length (p->verif_buff));
263
reader = xmlNewTextReaderFilename (p->filename);
267
xmlTextReaderSetStructuredErrorHandler (reader, error_func, NULL);
268
xmlSetStructuredErrorFunc(NULL, error_func);
271
if (xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1) == -1)
276
* Process all nodes in sequence
278
while ((ret = xmlTextReaderRead(reader)) == 1) {
279
if (!processNode(reader, b))
286
fprintf (stdout, "error handler was called but parse completed successfully (last error #%d)\n", errcount);
291
* Done, cleanup and status
294
status = buffer_equal (p->verif_buff, b);
296
buffer_dump (p->verif_buff, ".OOM.verif_buff");
297
buffer_dump (b, ".OOM.buff");
303
fprintf (stdout, "# %s: %u elems, %u attrs, %u chars %s\n",
304
p->filename, elem, attrs, chars,
305
status ? "ok" : "wrong");
314
xmlFreeTextReader (reader);
318
int main(int argc, char **argv) {
328
xmlMemSetup (test_free,
335
for (i = 1; i < argc ; i++) {
336
if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count")))
338
else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
340
else if ((!strcmp(argv[i], "-noent")) ||
341
(!strcmp(argv[i], "--noent")))
343
else if ((!strcmp(argv[i], "-show")) ||
344
(!strcmp(argv[i], "--show")))
348
xmlSubstituteEntitiesDefault(1);
349
for (i = 1; i < argc ; i++) {
350
if (argv[i][0] != '-') {
351
struct file_params p;
352
p.filename = argv[i];
355
if (!test_oom_handling (check_load_file_memory_func,
357
fprintf (stdout, "Failed!\n");
361
buffer_free (p.verif_buff);
364
if (test_get_malloc_blocks_outstanding () > 0) {
365
fprintf (stdout, "%d blocks leaked\n",
366
test_get_malloc_blocks_outstanding ());