-
Notifications
You must be signed in to change notification settings - Fork 8
/
10312.diff
479 lines (440 loc) · 13.4 KB
/
10312.diff
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
commit 8c9daf790abfc06e8ca3a44652542c577bb67d49
Author: Nick Wellnhofer <[email protected]>
Date: Wed Sep 12 13:42:27 2018 +0200
Check return value of nodePush in xmlSAX2StartElement
If the maximum depth is exceeded, nodePush halts the parser which
results in freeing the input buffer since the previous commit. This
invalidates the attribute pointers, so the error condition must be
checked.
Found by OSS-Fuzz.
diff --git a/SAX2.c b/SAX2.c
index 0f261b7b..49ce566c 100644
--- a/SAX2.c
+++ b/SAX2.c
@@ -1592,206 +1592,209 @@ void
xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlNodePtr ret;
xmlNodePtr parent;
xmlNsPtr ns;
xmlChar *name;
xmlChar *prefix;
const xmlChar *att;
const xmlChar *value;
int i;
if ((ctx == NULL) || (fullname == NULL) || (ctxt->myDoc == NULL)) return;
parent = ctxt->node;
#ifdef DEBUG_SAX
xmlGenericError(xmlGenericErrorContext,
"SAX.xmlSAX2StartElement(%s)\n", fullname);
#endif
/*
* First check on validity:
*/
if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
((ctxt->myDoc->intSubset == NULL) ||
((ctxt->myDoc->intSubset->notations == NULL) &&
(ctxt->myDoc->intSubset->elements == NULL) &&
(ctxt->myDoc->intSubset->attributes == NULL) &&
(ctxt->myDoc->intSubset->entities == NULL)))) {
xmlErrValid(ctxt, XML_ERR_NO_DTD,
"Validation failed: no DTD found !", NULL, NULL);
ctxt->validate = 0;
}
/*
* Split the full name into a namespace prefix and the tag name
*/
name = xmlSplitQName(ctxt, fullname, &prefix);
/*
* Note : the namespace resolution is deferred until the end of the
* attributes parsing, since local namespace can be defined as
* an attribute at this level.
*/
ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
if (ret == NULL) {
if (prefix != NULL)
xmlFree(prefix);
xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
return;
}
if (ctxt->myDoc->children == NULL) {
#ifdef DEBUG_SAX_TREE
xmlGenericError(xmlGenericErrorContext, "Setting %s as root\n", name);
#endif
xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
} else if (parent == NULL) {
parent = ctxt->myDoc->children;
}
ctxt->nodemem = -1;
if (ctxt->linenumbers) {
if (ctxt->input != NULL) {
if (ctxt->input->line < 65535)
ret->line = (short) ctxt->input->line;
else
ret->line = 65535;
}
}
/*
* We are parsing a new node.
*/
#ifdef DEBUG_SAX_TREE
xmlGenericError(xmlGenericErrorContext, "pushing(%s)\n", name);
#endif
- nodePush(ctxt, ret);
+ if (nodePush(ctxt, ret) < 0) {
+ xmlFreeNode(ret);
+ return;
+ }
/*
* Link the child element
*/
if (parent != NULL) {
if (parent->type == XML_ELEMENT_NODE) {
#ifdef DEBUG_SAX_TREE
xmlGenericError(xmlGenericErrorContext,
"adding child %s to %s\n", name, parent->name);
#endif
xmlAddChild(parent, ret);
} else {
#ifdef DEBUG_SAX_TREE
xmlGenericError(xmlGenericErrorContext,
"adding sibling %s to ", name);
xmlDebugDumpOneNode(stderr, parent, 0);
#endif
xmlAddSibling(parent, ret);
}
}
/*
* Insert all the defaulted attributes from the DTD especially namespaces
*/
if ((!ctxt->html) &&
((ctxt->myDoc->intSubset != NULL) ||
(ctxt->myDoc->extSubset != NULL))) {
xmlCheckDefaultedAttributes(ctxt, name, prefix, atts);
}
/*
* process all the attributes whose name start with "xmlns"
*/
if (atts != NULL) {
i = 0;
att = atts[i++];
value = atts[i++];
if (!ctxt->html) {
while ((att != NULL) && (value != NULL)) {
if ((att[0] == 'x') && (att[1] == 'm') && (att[2] == 'l') &&
(att[3] == 'n') && (att[4] == 's'))
xmlSAX2AttributeInternal(ctxt, att, value, prefix);
att = atts[i++];
value = atts[i++];
}
}
}
/*
* Search the namespace, note that since the attributes have been
* processed, the local namespaces are available.
*/
ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
if ((ns == NULL) && (parent != NULL))
ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
if ((prefix != NULL) && (ns == NULL)) {
ns = xmlNewNs(ret, NULL, prefix);
xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
"Namespace prefix %s is not defined\n",
prefix, NULL);
}
/*
* set the namespace node, making sure that if the default namspace
* is unbound on a parent we simply kee it NULL
*/
if ((ns != NULL) && (ns->href != NULL) &&
((ns->href[0] != 0) || (ns->prefix != NULL)))
xmlSetNs(ret, ns);
/*
* process all the other attributes
*/
if (atts != NULL) {
i = 0;
att = atts[i++];
value = atts[i++];
if (ctxt->html) {
while (att != NULL) {
xmlSAX2AttributeInternal(ctxt, att, value, NULL);
att = atts[i++];
value = atts[i++];
}
} else {
while ((att != NULL) && (value != NULL)) {
if ((att[0] != 'x') || (att[1] != 'm') || (att[2] != 'l') ||
(att[3] != 'n') || (att[4] != 's'))
xmlSAX2AttributeInternal(ctxt, att, value, NULL);
/*
* Next ones
*/
att = atts[i++];
value = atts[i++];
}
}
}
#ifdef LIBXML_VALID_ENABLED
/*
* If it's the Document root, finish the DTD validation and
* check the document root element for validity
*/
if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
int chk;
chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
if (chk <= 0)
ctxt->valid = 0;
if (chk < 0)
ctxt->wellFormed = 0;
ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
}
#endif /* LIBXML_VALID_ENABLED */
if (prefix != NULL)
xmlFree(prefix);
}
/**
* xmlSAX2EndElement:
* @ctx: the user data (XML parser context)
* @name: The element name
*
* called when the end of an element has been detected.
*/
@@ -2204,246 +2207,249 @@ void
xmlSAX2StartElementNs(void *ctx,
const xmlChar *localname,
const xmlChar *prefix,
const xmlChar *URI,
int nb_namespaces,
const xmlChar **namespaces,
int nb_attributes,
int nb_defaulted,
const xmlChar **attributes)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlNodePtr ret;
xmlNodePtr parent;
xmlNsPtr last = NULL, ns;
const xmlChar *uri, *pref;
xmlChar *lname = NULL;
int i, j;
if (ctx == NULL) return;
parent = ctxt->node;
/*
* First check on validity:
*/
if (ctxt->validate && (ctxt->myDoc->extSubset == NULL) &&
((ctxt->myDoc->intSubset == NULL) ||
((ctxt->myDoc->intSubset->notations == NULL) &&
(ctxt->myDoc->intSubset->elements == NULL) &&
(ctxt->myDoc->intSubset->attributes == NULL) &&
(ctxt->myDoc->intSubset->entities == NULL)))) {
xmlErrValid(ctxt, XML_DTD_NO_DTD,
"Validation failed: no DTD found !", NULL, NULL);
ctxt->validate = 0;
}
/*
* Take care of the rare case of an undefined namespace prefix
*/
if ((prefix != NULL) && (URI == NULL)) {
if (ctxt->dictNames) {
const xmlChar *fullname;
fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
if (fullname != NULL)
localname = fullname;
} else {
lname = xmlBuildQName(localname, prefix, NULL, 0);
}
}
/*
* allocate the node
*/
if (ctxt->freeElems != NULL) {
ret = ctxt->freeElems;
ctxt->freeElems = ret->next;
ctxt->freeElemsNr--;
memset(ret, 0, sizeof(xmlNode));
ret->type = XML_ELEMENT_NODE;
if (ctxt->dictNames)
ret->name = localname;
else {
if (lname == NULL)
ret->name = xmlStrdup(localname);
else
ret->name = lname;
if (ret->name == NULL) {
xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
return;
}
}
if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
xmlRegisterNodeDefaultValue(ret);
} else {
if (ctxt->dictNames)
ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
(xmlChar *) localname, NULL);
else if (lname == NULL)
ret = xmlNewDocNode(ctxt->myDoc, NULL, localname, NULL);
else
ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL,
(xmlChar *) lname, NULL);
if (ret == NULL) {
xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
return;
}
}
if (ctxt->linenumbers) {
if (ctxt->input != NULL) {
if (ctxt->input->line < 65535)
ret->line = (short) ctxt->input->line;
else
ret->line = 65535;
}
}
if (parent == NULL) {
xmlAddChild((xmlNodePtr) ctxt->myDoc, (xmlNodePtr) ret);
}
/*
* Build the namespace list
*/
for (i = 0,j = 0;j < nb_namespaces;j++) {
pref = namespaces[i++];
uri = namespaces[i++];
ns = xmlNewNs(NULL, uri, pref);
if (ns != NULL) {
if (last == NULL) {
ret->nsDef = last = ns;
} else {
last->next = ns;
last = ns;
}
if ((URI != NULL) && (prefix == pref))
ret->ns = ns;
} else {
/*
* any out of memory error would already have been raised
* but we can't be guaranteed it's the actual error due to the
* API, best is to skip in this case
*/
continue;
}
#ifdef LIBXML_VALID_ENABLED
if ((!ctxt->html) && ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset) {
ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
ret, prefix, ns, uri);
}
#endif /* LIBXML_VALID_ENABLED */
}
ctxt->nodemem = -1;
/*
* We are parsing a new node.
*/
- nodePush(ctxt, ret);
+ if (nodePush(ctxt, ret) < 0) {
+ xmlFreeNode(ret);
+ return;
+ }
/*
* Link the child element
*/
if (parent != NULL) {
if (parent->type == XML_ELEMENT_NODE) {
xmlAddChild(parent, ret);
} else {
xmlAddSibling(parent, ret);
}
}
/*
* Insert the defaulted attributes from the DTD only if requested:
*/
if ((nb_defaulted != 0) &&
((ctxt->loadsubset & XML_COMPLETE_ATTRS) == 0))
nb_attributes -= nb_defaulted;
/*
* Search the namespace if it wasn't already found
* Note that, if prefix is NULL, this searches for the default Ns
*/
if ((URI != NULL) && (ret->ns == NULL)) {
ret->ns = xmlSearchNs(ctxt->myDoc, parent, prefix);
if ((ret->ns == NULL) && (xmlStrEqual(prefix, BAD_CAST "xml"))) {
ret->ns = xmlSearchNs(ctxt->myDoc, ret, prefix);
}
if (ret->ns == NULL) {
ns = xmlNewNs(ret, NULL, prefix);
if (ns == NULL) {
xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElementNs");
return;
}
if (prefix != NULL)
xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
"Namespace prefix %s was not found\n",
prefix, NULL);
else
xmlNsWarnMsg(ctxt, XML_NS_ERR_UNDEFINED_NAMESPACE,
"Namespace default prefix was not found\n",
NULL, NULL);
}
}
/*
* process all the other attributes
*/
if (nb_attributes > 0) {
for (j = 0,i = 0;i < nb_attributes;i++,j+=5) {
/*
* Handle the rare case of an undefined atribute prefix
*/
if ((attributes[j+1] != NULL) && (attributes[j+2] == NULL)) {
if (ctxt->dictNames) {
const xmlChar *fullname;
fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
attributes[j]);
if (fullname != NULL) {
xmlSAX2AttributeNs(ctxt, fullname, NULL,
attributes[j+3], attributes[j+4]);
continue;
}
} else {
lname = xmlBuildQName(attributes[j], attributes[j+1],
NULL, 0);
if (lname != NULL) {
xmlSAX2AttributeNs(ctxt, lname, NULL,
attributes[j+3], attributes[j+4]);
xmlFree(lname);
continue;
}
}
}
xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
attributes[j+3], attributes[j+4]);
}
}
#ifdef LIBXML_VALID_ENABLED
/*
* If it's the Document root, finish the DTD validation and
* check the document root element for validity
*/
if ((ctxt->validate) && (ctxt->vctxt.finishDtd == XML_CTXT_FINISH_DTD_0)) {
int chk;
chk = xmlValidateDtdFinal(&ctxt->vctxt, ctxt->myDoc);
if (chk <= 0)
ctxt->valid = 0;
if (chk < 0)
ctxt->wellFormed = 0;
ctxt->valid &= xmlValidateRoot(&ctxt->vctxt, ctxt->myDoc);
ctxt->vctxt.finishDtd = XML_CTXT_FINISH_DTD_1;
}
#endif /* LIBXML_VALID_ENABLED */
}
/**
* xmlSAX2EndElementNs:
* @ctx: the user data (XML parser context)
* @localname: the local name of the element
* @prefix: the element namespace prefix if available
* @URI: the element namespace name if available
*
* SAX2 callback when an element end has been detected by the parser.
* It provides the namespace informations for the element.
*/