1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-26 00:37:43 +03:00

parser: Only enable SAX2 if there are SAX2 element handlers

This reverts part of commit 235b15a5 for backward compatibility and
adds some comments trying to clarify the whole mess.

Fixes #623.
This commit is contained in:
Nick Wellnhofer
2023-11-20 15:20:37 +01:00
parent 272cb22313
commit aca37d8c77
3 changed files with 30 additions and 2 deletions

View File

@@ -745,6 +745,19 @@ struct _xmlSAXHandler {
setDocumentLocatorSAXFunc setDocumentLocator; setDocumentLocatorSAXFunc setDocumentLocator;
startDocumentSAXFunc startDocument; startDocumentSAXFunc startDocument;
endDocumentSAXFunc endDocument; endDocumentSAXFunc endDocument;
/*
* `startElement` and `endElement` are only used by the legacy SAX1
* interface and should not be used in new software. If you really
* have to enable SAX1, the preferred way is set the `initialized`
* member to 1 instead of XML_SAX2_MAGIC.
*
* For backward compatibility, it's also possible to set the
* `startElementNs` and `endElementNs` handlers to NULL.
*
* You can also set the XML_PARSE_SAX1 parser option, but versions
* older than 2.12.0 will probably crash if this option is provided
* together with custom SAX callbacks.
*/
startElementSAXFunc startElement; startElementSAXFunc startElement;
endElementSAXFunc endElement; endElementSAXFunc endElement;
referenceSAXFunc reference; referenceSAXFunc reference;
@@ -758,8 +771,14 @@ struct _xmlSAXHandler {
getParameterEntitySAXFunc getParameterEntity; getParameterEntitySAXFunc getParameterEntity;
cdataBlockSAXFunc cdataBlock; cdataBlockSAXFunc cdataBlock;
externalSubsetSAXFunc externalSubset; externalSubsetSAXFunc externalSubset;
/*
* `initialized` should always be set to XML_SAX2_MAGIC to enable the
* modern SAX2 interface.
*/
unsigned int initialized; unsigned int initialized;
/* The following fields are extensions available only on version 2 */ /*
* The following members are only used by the SAX2 interface.
*/
void *_private; void *_private;
startElementNsSAX2Func startElementNs; startElementNsSAX2Func startElementNs;
endElementNsSAX2Func endElementNs; endElementNsSAX2Func endElementNs;

View File

@@ -863,7 +863,14 @@ xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
if (ctxt == NULL) return; if (ctxt == NULL) return;
sax = ctxt->sax; sax = ctxt->sax;
#ifdef LIBXML_SAX1_ENABLED #ifdef LIBXML_SAX1_ENABLED
if ((sax) && (sax->initialized == XML_SAX2_MAGIC)) /*
* Only enable SAX2 if there SAX2 element handlers, except when there
* are no element handlers at all.
*/
if ((sax) && (sax->initialized == XML_SAX2_MAGIC) &&
((sax->startElementNs != NULL) ||
(sax->endElementNs != NULL) ||
((sax->startElement == NULL) && (sax->endElement == NULL))))
ctxt->sax2 = 1; ctxt->sax2 = 1;
#else #else
ctxt->sax2 = 1; ctxt->sax2 = 1;

View File

@@ -646,6 +646,8 @@ static void
initSAX(xmlParserCtxtPtr ctxt) { initSAX(xmlParserCtxtPtr ctxt) {
ctxt->sax->startElementNs = NULL; ctxt->sax->startElementNs = NULL;
ctxt->sax->endElementNs = NULL; ctxt->sax->endElementNs = NULL;
ctxt->sax->startElement = NULL;
ctxt->sax->endElement = NULL;
ctxt->sax->characters = NULL; ctxt->sax->characters = NULL;
ctxt->sax->cdataBlock = NULL; ctxt->sax->cdataBlock = NULL;
ctxt->sax->ignorableWhitespace = NULL; ctxt->sax->ignorableWhitespace = NULL;