1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 12:25:09 +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 0af49eb97a
commit 2bc8e7a307
3 changed files with 30 additions and 2 deletions

View File

@ -745,6 +745,19 @@ struct _xmlSAXHandler {
setDocumentLocatorSAXFunc setDocumentLocator;
startDocumentSAXFunc startDocument;
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;
endElementSAXFunc endElement;
referenceSAXFunc reference;
@ -758,8 +771,14 @@ struct _xmlSAXHandler {
getParameterEntitySAXFunc getParameterEntity;
cdataBlockSAXFunc cdataBlock;
externalSubsetSAXFunc externalSubset;
/*
* `initialized` should always be set to XML_SAX2_MAGIC to enable the
* modern SAX2 interface.
*/
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;
startElementNsSAX2Func startElementNs;
endElementNsSAX2Func endElementNs;

View File

@ -863,7 +863,14 @@ xmlDetectSAX2(xmlParserCtxtPtr ctxt) {
if (ctxt == NULL) return;
sax = ctxt->sax;
#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;
#else
ctxt->sax2 = 1;

View File

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