1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 03:55:04 +03:00

parser: Don't create reference in xmlLookupGeneralEntity

This should only be done in xmlParseReference.

The handling of undeclared entities is still somewhat inconsistent. In
element content we create references even if entity substitution is
enabled. In attribute values undeclared entities are always ignored.
This commit is contained in:
Nick Wellnhofer 2024-04-22 17:12:54 +02:00
parent b717abdd09
commit 45fe9924f0
9 changed files with 135 additions and 9 deletions

View File

@ -7363,9 +7363,22 @@ xmlParseReference(xmlParserCtxtPtr ctxt) {
* We are seeing an entity reference
*/
name = xmlParseEntityRefInternal(ctxt);
if (name != NULL)
ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 0);
if (ent == NULL) return;
if (name == NULL)
return;
ent = xmlLookupGeneralEntity(ctxt, name, /* isAttr */ 0);
if (ent == NULL) {
/*
* Create a reference for undeclared entities.
* TODO: Should we really create a reference if entity
* substitution is enabled?
*/
if ((ctxt->sax != NULL) &&
(ctxt->disableSAX == 0) &&
(ctxt->sax->reference != NULL)) {
ctxt->sax->reference(ctxt->userData, name);
}
return;
}
if (!ctxt->wellFormed)
return;
@ -7602,12 +7615,6 @@ xmlLookupGeneralEntity(xmlParserCtxtPtr ctxt, const xmlChar *name, int inAttr) {
} else {
xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
"Entity '%s' not defined\n", name, NULL);
if ((ctxt->inSubset == 0) &&
(ctxt->sax != NULL) &&
(ctxt->disableSAX == 0) &&
(ctxt->sax->reference != NULL)) {
ctxt->sax->reference(ctxt->userData, name);
}
}
ctxt->valid = 0;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!-- Having an external DTD makes undeclared entities a warning. -->
<!DOCTYPE doc SYSTEM "foo">
<doc>
<elem attr=""/>
<elem>&undeclared;</elem>
</doc>

View File

@ -0,0 +1,24 @@
SAX.setDocumentLocator()
SAX.startDocument()
SAX.comment( Having an external DTD makes undeclared entities a warning. )
SAX.internalSubset(doc, , foo)
SAX.externalSubset(doc, , foo)
SAX.startElementNs(doc, NULL, NULL, 0, 0, 0)
SAX.characters(
, 5)
SAX.getEntity(undeclared)
SAX.warning: Entity 'undeclared' not defined
SAX.startElementNs(elem, NULL, NULL, 0, 1, 0, attr='"/>
...', 0)
SAX.endElementNs(elem, NULL, NULL)
SAX.characters(
, 5)
SAX.startElementNs(elem, NULL, NULL, 0, 0, 0)
SAX.getEntity(undeclared)
SAX.warning: Entity 'undeclared' not defined
SAX.reference(undeclared)
SAX.endElementNs(elem, NULL, NULL)
SAX.characters(
, 1)
SAX.endElementNs(doc, NULL, NULL)
SAX.endDocument()

View File

@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!-- Having an external DTD makes undeclared entities a warning. -->
<!DOCTYPE doc SYSTEM "foo">
<doc>
<elem attr=""/>
<elem>&undeclared;</elem>
</doc>

View File

@ -0,0 +1,14 @@
0 8 #comment 0 1 Having an external DTD makes undeclared entities a warning.
0 10 doc 0 0
0 1 doc 0 0
1 14 #text 0 1
1 1 elem 1 0
1 14 #text 0 1
1 1 elem 0 0
2 5 undeclared 0 0
1 15 elem 0 0
1 14 #text 0 1
0 15 doc 0 0

View File

@ -0,0 +1,14 @@
0 8 #comment 0 1 Having an external DTD makes undeclared entities a warning.
0 10 doc 0 0
0 1 doc 0 0
1 14 #text 0 1
1 1 elem 1 0
1 14 #text 0 1
1 1 elem 0 0
2 5 undeclared 0 0
1 15 elem 0 0
1 14 #text 0 1
0 15 doc 0 0

View File

@ -0,0 +1,23 @@
SAX.setDocumentLocator()
SAX.startDocument()
SAX.comment( Having an external DTD makes undeclared entities a warning. )
SAX.internalSubset(doc, , foo)
SAX.externalSubset(doc, , foo)
SAX.startElement(doc)
SAX.characters(
, 5)
SAX.getEntity(undeclared)
SAX.warning: Entity 'undeclared' not defined
SAX.startElement(elem, attr='')
SAX.endElement(elem)
SAX.characters(
, 5)
SAX.startElement(elem)
SAX.getEntity(undeclared)
SAX.warning: Entity 'undeclared' not defined
SAX.reference(undeclared)
SAX.endElement(elem)
SAX.characters(
, 1)
SAX.endElement(doc)
SAX.endDocument()

View File

@ -0,0 +1,24 @@
SAX.setDocumentLocator()
SAX.startDocument()
SAX.comment( Having an external DTD makes undeclared entities a warning. )
SAX.internalSubset(doc, , foo)
SAX.externalSubset(doc, , foo)
SAX.startElementNs(doc, NULL, NULL, 0, 0, 0)
SAX.characters(
, 5)
SAX.getEntity(undeclared)
SAX.warning: Entity 'undeclared' not defined
SAX.startElementNs(elem, NULL, NULL, 0, 1, 0, attr='"/>
...', 0)
SAX.endElementNs(elem, NULL, NULL)
SAX.characters(
, 5)
SAX.startElementNs(elem, NULL, NULL, 0, 0, 0)
SAX.getEntity(undeclared)
SAX.warning: Entity 'undeclared' not defined
SAX.reference(undeclared)
SAX.endElementNs(elem, NULL, NULL)
SAX.characters(
, 1)
SAX.endElementNs(doc, NULL, NULL)
SAX.endDocument()

View File

@ -0,0 +1,6 @@
<!-- Having an external DTD makes undeclared entities a warning. -->
<!DOCTYPE doc SYSTEM "foo">
<doc>
<elem attr="&undeclared;"/>
<elem>&undeclared;</elem>
</doc>