Skip to content

Commit

Permalink
MFH: Fixed Bug #27908 (xml default_handlers not being called)
Browse files Browse the repository at this point in the history
Fix memleak when entitydecls are parsed
remove old ifdefs
  • Loading branch information
Rob Richards committed Oct 12, 2005
1 parent 28a0aef commit 855f7df
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
67 changes: 44 additions & 23 deletions ext/xml/compat.c
Expand Up @@ -33,7 +33,6 @@ typedef struct _php_xml_ns {
((__ns) != NULL && strlen(__ns) == 5 && *(__ns) == 'x' && *((__ns)+1) == 'm' && \
*((__ns)+2) == 'l' && *((__ns)+3) == 'n' && *((__ns)+4) == 's')

#if LIBXML_VERSION >= 20600
static void
_qualify_namespace(XML_Parser parser, const xmlChar *name, const xmlChar *URI, xmlChar **qualified)
{
Expand All @@ -46,7 +45,6 @@ _qualify_namespace(XML_Parser parser, const xmlChar *name, const xmlChar *URI, x
*qualified = xmlStrdup(name);
}
}
#endif

static void
_start_element_handler(void *user, const xmlChar *name, const xmlChar **attributes)
Expand All @@ -55,6 +53,25 @@ _start_element_handler(void *user, const xmlChar *name, const xmlChar **attribut
xmlChar *qualified_name = NULL;

if (parser->h_start_element == NULL) {
if (parser->h_default) {
int attno = 0;

qualified_name = xmlStrncatNew((xmlChar *)"<", name, xmlStrlen(name));
if (attributes) {
while (attributes[attno] != NULL) {
qualified_name = xmlStrncat(qualified_name, (xmlChar *)" ", 1);
qualified_name = xmlStrcat(qualified_name, (xmlChar *)attributes[attno]);
qualified_name = xmlStrncat(qualified_name, (xmlChar *)"=\"", 2);
qualified_name = xmlStrcat(qualified_name, (xmlChar *)attributes[++attno]);
qualified_name = xmlStrncat(qualified_name, (xmlChar *)"\"", 1);
attno++;
}

}
qualified_name = xmlStrncat(qualified_name, (xmlChar *)">", 1);
parser->h_default(parser->user, (const XML_Char *) qualified_name, xmlStrlen(qualified_name));
xmlFree(qualified_name);
}
return;
}

Expand All @@ -65,7 +82,6 @@ _start_element_handler(void *user, const xmlChar *name, const xmlChar **attribut
xmlFree(qualified_name);
}

#if LIBXML_VERSION >= 20600
static void
_start_element_handler_ns(void *user, const xmlChar *name, const xmlChar *prefix, const xmlChar *URI, int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, int nb_defaulted, const xmlChar ** attributes)
{
Expand All @@ -84,7 +100,7 @@ _start_element_handler_ns(void *user, const xmlChar *name, const xmlChar *prefix
y = 0;
}

if (parser->h_start_element == NULL) {
if (parser->h_start_element == NULL && parser->h_default == NULL) {
return;
}
_qualify_namespace(parser, name, URI, &qualified_name);
Expand Down Expand Up @@ -117,7 +133,6 @@ _start_element_handler_ns(void *user, const xmlChar *name, const xmlChar *prefix
}
xmlFree(qualified_name);
}
#endif

static void
_namespace_handler(XML_Parser parser, xmlNsPtr nsptr)
Expand All @@ -135,6 +150,12 @@ _end_element_handler(void *user, const xmlChar *name)
XML_Parser parser = (XML_Parser) user;

if (parser->h_end_element == NULL) {
if (parser->h_default) {
qualified_name = xmlStrncatNew((xmlChar *)"</", name, xmlStrlen(name));
qualified_name = xmlStrncat(qualified_name, (xmlChar *)">", 1);
parser->h_default(parser->user, (const XML_Char *) qualified_name, xmlStrlen(qualified_name));
xmlFree(qualified_name);
}
return;
}

Expand All @@ -145,7 +166,6 @@ _end_element_handler(void *user, const xmlChar *name)
xmlFree(qualified_name);
}

#if LIBXML_VERSION >= 20600
static void
_end_element_handler_ns(void *user, const xmlChar *name, const xmlChar * prefix, const xmlChar *URI)
{
Expand All @@ -162,14 +182,16 @@ _end_element_handler_ns(void *user, const xmlChar *name, const xmlChar * prefix,

xmlFree(qualified_name);
}
#endif

static void
_cdata_handler(void *user, const xmlChar *cdata, int cdata_len)
{
XML_Parser parser = (XML_Parser) user;

if (parser->h_cdata == NULL) {
if (parser->h_default) {
parser->h_default(parser->user, (const XML_Char *) cdata, cdata_len);
}
return;
}

Expand All @@ -182,6 +204,16 @@ _pi_handler(void *user, const xmlChar *target, const xmlChar *data)
XML_Parser parser = (XML_Parser) user;

if (parser->h_pi == NULL) {
if (parser->h_default) {
xmlChar *full_pi;

full_pi = xmlStrncatNew((xmlChar *)"<?", target, xmlStrlen(target));
full_pi = xmlStrncat(full_pi, (xmlChar *)" ", 1);
full_pi = xmlStrcat(full_pi, data);
full_pi = xmlStrncat(full_pi, (xmlChar *)"?>", 2);
parser->h_default(parser->user, (const XML_Char *) full_pi, xmlStrlen(full_pi));
xmlFree(full_pi);
}
return;
}

Expand Down Expand Up @@ -335,16 +367,11 @@ php_xml_compat_handlers = {
NULL, /* getParameterEntity */
_cdata_handler, /* cdataBlock */
NULL, /* externalSubset */
#if LIBXML_VERSION < 20600
1,
#else
XML_SAX2_MAGIC,
NULL,
_start_element_handler_ns,
_end_element_handler_ns,
NULL
#endif

NULL
};

PHPAPI XML_Parser
Expand Down Expand Up @@ -390,16 +417,12 @@ XML_ParserCreate_MM(const XML_Char *encoding, const XML_Memory_Handling_Suite *m
parser->parser->wellFormed = 0;
if (sep != NULL) {
parser->use_namespace = 1;
#if LIBXML_VERSION >= 20600
parser->parser->sax2 = 1;
#endif
parser->_ns_seperator = xmlStrdup(sep);
#if LIBXML_VERSION >= 20600
} else {
/* Reset flag as XML_SAX2_MAGIC is needed for xmlCreatePushParserCtxt
so must be set in the handlers */
parser->parser->sax->initialized = 1;
#endif
}
return parser;
}
Expand Down Expand Up @@ -480,9 +503,7 @@ XML_SetEndNamespaceDeclHandler(XML_Parser parser, XML_EndNamespaceDeclHandler en
PHPAPI int
XML_Parse(XML_Parser parser, const XML_Char *data, int data_len, int is_final)
{
#if LIBXML_VERSION >= 20600
int error;
#endif

/* The following is a hack to keep BC with PHP 4 while avoiding
the inifite loop in libxml <= 2.6.17 which occurs when no encoding
Expand All @@ -508,7 +529,6 @@ has been defined and none can be detected */
}
#endif

#if LIBXML_VERSION >= 20600
error = xmlParseChunk(parser->parser, data, data_len, is_final);
if (!error) {
return 1;
Expand All @@ -517,9 +537,6 @@ has been defined and none can be detected */
} else {
return 1;
}
#else
return !xmlParseChunk(parser->parser, data, data_len, is_final);
#endif
}

PHPAPI int
Expand Down Expand Up @@ -684,6 +701,10 @@ XML_ParserFree(XML_Parser parser)
xmlFree(parser->_ns_seperator);
}
}
if (parser->parser->myDoc) {
xmlFreeDoc(parser->parser->myDoc);
parser->parser->myDoc = NULL;
}
xmlFreeParserCtxt(parser->parser);
efree(parser);
}
Expand Down
5 changes: 0 additions & 5 deletions ext/xml/xml.c
Expand Up @@ -1108,12 +1108,7 @@ PHP_FUNCTION(xml_parser_create)
Create an XML parser */
PHP_FUNCTION(xml_parser_create_ns)
{
#if defined(HAVE_LIBXML) && defined(HAVE_XML) && !defined(HAVE_LIBEXPAT) && LIBXML_VERSION < 20600
php_error_docref(NULL TSRMLS_CC, E_WARNING, "is broken with libxml2 %s. Please upgrade to libxml2 2.6", LIBXML_DOTTED_VERSION);
RETURN_FALSE;
#else
php_xml_parser_create_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
#endif
}
/* }}} */

Expand Down

0 comments on commit 855f7df

Please sign in to comment.