forked from GNOME/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testOOM.c
377 lines (326 loc) · 8.27 KB
/
testOOM.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
* testOOM.c: Test out-of-memory handling
*
* See Copyright for the status of this software.
*
*/
#include "libxml.h"
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <libxml/xmlreader.h>
#include "testOOMlib.h"
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#define EXIT_OOM 2
int error = FALSE;
int errcount = 0;
int noent = 0;
int count = 0;
int valid = 0;
int showErrs = 0;
/*
* Since we are using the xmlTextReader functions, we set up
* strings for the element types to help in debugging any error
* output
*/
const char *elementNames[] = {
"XML_READER_TYPE_NONE",
"XML_READER_TYPE_ELEMENT",
"XML_READER_TYPE_ATTRIBUTE",
"XML_READER_TYPE_TEXT",
"XML_READER_TYPE_CDATA",
"XML_READER_TYPE_ENTITY_REFERENCE",
"XML_READER_TYPE_ENTITY",
"XML_READER_TYPE_PROCESSING_INSTRUCTION",
"XML_READER_TYPE_COMMENT",
"XML_READER_TYPE_DOCUMENT",
"XML_READER_TYPE_DOCUMENT_TYPE",
"XML_READER_TYPE_DOCUMENT_FRAGMENT",
"XML_READER_TYPE_NOTATION",
"XML_READER_TYPE_WHITESPACE",
"XML_READER_TYPE_SIGNIFICANT_WHITESPACE",
"XML_READER_TYPE_END_ELEMENT",
"XML_READER_TYPE_END_ENTITY",
"XML_READER_TYPE_XML_DECLARATION"};
/* not using xmlBuff here because I don't want those
* mallocs to interfere */
struct buffer {
char *str;
size_t len;
size_t max;
};
static struct buffer *buffer_create (size_t init_len)
{
struct buffer *b;
b = malloc (sizeof *b);
if (b == NULL)
exit (EXIT_OOM);
if (init_len) {
b->str = malloc (init_len);
if (b->str == NULL)
exit (EXIT_OOM);
}
else
b->str = NULL;
b->len = 0;
b->max = init_len;
return b;
}
static void buffer_free (struct buffer *b)
{
free (b->str);
free (b);
}
static size_t buffer_get_length (struct buffer *b)
{
return b->len;
}
static void buffer_expand (struct buffer *b, size_t min)
{
void *new_str;
size_t new_size = b->max ? b->max : 512;
while (new_size < b->len + min)
new_size *= 2;
if (new_size > b->max) {
new_str = realloc (b->str, new_size);
if (new_str == NULL)
exit (EXIT_OOM);
b->str = new_str;
b->max = new_size;
}
}
static void buffer_add_char (struct buffer *b, char c)
{
buffer_expand (b, 1);
b->str[b->len] = c;
b->len += 1;
}
static void buffer_add_string (struct buffer *b, const char *s)
{
size_t size = strlen(s) + 1;
unsigned int ix;
for (ix=0; ix<size-1; ix++) {
if (s[ix] < 0x20)
printf ("binary data [0x%02x]?\n", (unsigned char)s[ix]);
}
buffer_expand (b, size);
strcpy (b->str + b->len, s);
b->str[b->len+size-1] = '\n'; /* replace string term with newline */
b->len += size;
}
static int buffer_equal (struct buffer *b1, struct buffer *b2)
{
return (b1->len == b2->len &&
(b1->len == 0 || (memcmp (b1->str, b2->str, b1->len) == 0)));
}
static void buffer_dump (struct buffer *b, const char *fname)
{
FILE *f = fopen (fname, "wb");
if (f != NULL) {
fwrite (b->str, 1, b->len, f);
fclose (f);
}
}
static void usage(const char *progname) {
printf("Usage : %s [options] XMLfiles ...\n", progname);
printf("\tParse the XML files using the xmlTextReader API\n");
printf("\t --count: count the number of attribute and elements\n");
printf("\t --valid: validate the document\n");
printf("\t --show: display the error messages encountered\n");
exit(1);
}
static unsigned int elem, attrs, chars;
static int processNode (xmlTextReaderPtr reader, void *data)
{
struct buffer *buff = data;
int type;
type = xmlTextReaderNodeType(reader);
if (count) {
if (type == 1) {
elem++;
attrs += xmlTextReaderAttributeCount(reader);
} else if (type == 3) {
const xmlChar *txt;
txt = xmlTextReaderConstValue(reader);
if (txt != NULL)
chars += xmlStrlen (txt);
else
return FALSE;
}
}
if (buff != NULL) {
int ret;
const char *s;
buffer_add_string (buff, elementNames[type]);
if (type == 1) {
s = (const char *)xmlTextReaderConstName (reader);
if (s == NULL) return FALSE;
buffer_add_string (buff, s);
while ((ret = xmlTextReaderMoveToNextAttribute (reader)) == 1) {
s = (const char *)xmlTextReaderConstName (reader);
if (s == NULL) return FALSE;
buffer_add_string (buff, s);
buffer_add_char (buff, '=');
s = (const char *)xmlTextReaderConstValue (reader);
if (s == NULL) return FALSE;
buffer_add_string (buff, s);
}
if (ret == -1) return FALSE;
}
else if (type == 3) {
s = (const char *)xmlTextReaderConstValue (reader);
if (s == NULL) return FALSE;
buffer_add_string (buff, s);
}
}
return TRUE;
}
struct file_params {
const char *filename;
struct buffer *verif_buff;
};
static void
error_func (void *data ATTRIBUTE_UNUSED, xmlErrorPtr err)
{
errcount++;
if (err->level == XML_ERR_ERROR ||
err->level == XML_ERR_FATAL)
error = TRUE;
if (showErrs) {
printf("%3d line %d: %s\n", error, err->line, err->message);
}
}
static int
check_load_file_memory_func (void *data)
{
struct file_params *p = data;
struct buffer *b;
xmlTextReaderPtr reader;
int ret, status, first_run;
if (count) {
elem = 0;
attrs = 0;
chars = 0;
}
first_run = p->verif_buff == NULL;
status = TRUE;
error = FALSE;
if (first_run)
b = buffer_create (0);
else
b = buffer_create (buffer_get_length (p->verif_buff));
reader = xmlNewTextReaderFilename (p->filename);
if (reader == NULL)
goto out;
xmlTextReaderSetStructuredErrorHandler (reader, error_func, NULL);
xmlSetStructuredErrorFunc(NULL, error_func);
if (valid) {
if (xmlTextReaderSetParserProp(reader, XML_PARSER_VALIDATE, 1) == -1)
goto out;
}
/*
* Process all nodes in sequence
*/
while ((ret = xmlTextReaderRead(reader)) == 1) {
if (!processNode(reader, b))
goto out;
}
if (ret == -1)
goto out;
if (error) {
fprintf (stdout, "error handler was called but parse completed successfully (last error #%d)\n", errcount);
return FALSE;
}
/*
* Done, cleanup and status
*/
if (! first_run) {
status = buffer_equal (p->verif_buff, b);
if (! status) {
buffer_dump (p->verif_buff, ".OOM.verif_buff");
buffer_dump (b, ".OOM.buff");
}
}
if (count)
{
fprintf (stdout, "# %s: %u elems, %u attrs, %u chars %s\n",
p->filename, elem, attrs, chars,
status ? "ok" : "wrong");
}
out:
if (first_run)
p->verif_buff = b;
else
buffer_free (b);
if (reader)
xmlFreeTextReader (reader);
return status;
}
int main(int argc, char **argv) {
int i;
int files = 0;
if (argc <= 1) {
usage(argv[0]);
return(1);
}
LIBXML_TEST_VERSION;
xmlMemSetup (test_free,
test_malloc,
test_realloc,
test_strdup);
xmlInitParser();
for (i = 1; i < argc ; i++) {
if ((!strcmp(argv[i], "-count")) || (!strcmp(argv[i], "--count")))
count++;
else if ((!strcmp(argv[i], "-valid")) || (!strcmp(argv[i], "--valid")))
valid++;
else if ((!strcmp(argv[i], "-noent")) ||
(!strcmp(argv[i], "--noent")))
noent++;
else if ((!strcmp(argv[i], "-show")) ||
(!strcmp(argv[i], "--show")))
showErrs++;
}
if (noent != 0)
xmlSubstituteEntitiesDefault(1);
for (i = 1; i < argc ; i++) {
if (argv[i][0] != '-') {
struct file_params p;
p.filename = argv[i];
p.verif_buff = NULL;
if (!test_oom_handling (check_load_file_memory_func,
&p)) {
fprintf (stdout, "Failed!\n");
return 1;
}
buffer_free (p.verif_buff);
xmlCleanupParser();
if (test_get_malloc_blocks_outstanding () > 0) {
fprintf (stdout, "%d blocks leaked\n",
test_get_malloc_blocks_outstanding ());
xmlMemoryDump();
return 1;
}
files ++;
}
}
xmlMemoryDump();
return 0;
}