1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-21 22:50:08 +03:00

SAX2: Report malloc failures

Fix many places where malloc failures aren't reported.

Improve error handling when parsing entity declarations.

Fixes #308.
This commit is contained in:
Nick Wellnhofer 2023-12-10 18:10:42 +01:00
parent f19a95108a
commit e58ea29f17
4 changed files with 228 additions and 250 deletions

469
SAX2.c
View File

@ -53,30 +53,7 @@
*/
static void LIBXML_ATTR_FORMAT(2,0)
xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
xmlStructuredErrorFunc schannel = NULL;
const char *str1 = "out of memory\n";
if (ctxt != NULL) {
ctxt->errNo = XML_ERR_NO_MEMORY;
if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
schannel = ctxt->sax->serror;
__xmlRaiseError(schannel,
ctxt->vctxt.error, ctxt->vctxt.userData,
ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
XML_ERR_ERROR, NULL, 0, (const char *) str1,
NULL, NULL, 0, 0,
msg, (const char *) str1, NULL);
ctxt->errNo = XML_ERR_NO_MEMORY;
ctxt->instate = XML_PARSER_EOF;
ctxt->disableSAX = 1;
} else {
__xmlRaiseError(schannel,
NULL, NULL,
ctxt, NULL, XML_FROM_PARSER, XML_ERR_NO_MEMORY,
XML_ERR_ERROR, NULL, 0, (const char *) str1,
NULL, NULL, 0, 0,
msg, (const char *) str1, NULL);
}
xmlErrMemory(ctxt, msg);
}
/**
@ -91,32 +68,12 @@ xmlSAX2ErrMemory(xmlParserCtxtPtr ctxt, const char *msg) {
*/
static void LIBXML_ATTR_FORMAT(3,0)
xmlErrValid(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const char *str1, const char *str2)
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
xmlStructuredErrorFunc schannel = NULL;
if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
(ctxt->instate == XML_PARSER_EOF))
return;
if (ctxt != NULL) {
ctxt->errNo = error;
if ((ctxt->sax != NULL) && (ctxt->sax->initialized == XML_SAX2_MAGIC))
schannel = ctxt->sax->serror;
__xmlRaiseError(schannel,
ctxt->vctxt.error, ctxt->vctxt.userData,
ctxt, NULL, XML_FROM_DTD, error,
XML_ERR_ERROR, NULL, 0, (const char *) str1,
(const char *) str2, NULL, 0, 0,
msg, (const char *) str1, (const char *) str2);
xmlErrParser(ctxt, NULL, XML_FROM_DTD, error, XML_ERR_ERROR,
str1, str2, NULL, 0, msg, str1, str2);
if (ctxt != NULL)
ctxt->valid = 0;
} else {
__xmlRaiseError(schannel,
NULL, NULL,
ctxt, NULL, XML_FROM_DTD, error,
XML_ERR_ERROR, NULL, 0, (const char *) str1,
(const char *) str2, NULL, 0, 0,
msg, (const char *) str1, (const char *) str2);
}
}
/**
@ -133,21 +90,8 @@ static void LIBXML_ATTR_FORMAT(3,0)
xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
(ctxt->instate == XML_PARSER_EOF))
return;
if (ctxt != NULL)
ctxt->errNo = error;
__xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
XML_ERR_FATAL, NULL, 0,
(const char *) str1, (const char *) str2,
NULL, 0, 0, msg, str1, str2);
if (ctxt != NULL) {
ctxt->wellFormed = 0;
ctxt->valid = 0;
if (ctxt->recovery == 0)
ctxt->disableSAX = 1;
}
xmlErrParser(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
@ -164,15 +108,8 @@ static void LIBXML_ATTR_FORMAT(3,0)
xmlWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1)
{
if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
(ctxt->instate == XML_PARSER_EOF))
return;
if (ctxt != NULL)
ctxt->errNo = error;
__xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER, error,
XML_ERR_WARNING, NULL, 0,
(const char *) str1, NULL,
NULL, 0, 0, msg, str1);
xmlErrParser(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_WARNING,
str1, NULL, NULL, 0, msg, str1);
}
/**
@ -188,15 +125,8 @@ static void LIBXML_ATTR_FORMAT(3,0)
xmlNsWarnMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
(ctxt->instate == XML_PARSER_EOF))
return;
if (ctxt != NULL)
ctxt->errNo = error;
__xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
XML_ERR_WARNING, NULL, 0,
(const char *) str1, (const char *) str2,
NULL, 0, 0, msg, str1, str2);
xmlErrParser(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_WARNING,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
@ -385,7 +315,11 @@ xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
return;
}
xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID);
if (xmlNewDtd(ctxt->myDoc, name, ExternalID, SystemID) == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
xmlFreeInputStream(input);
return;
}
/*
* make sure we won't destroy the main document context
@ -496,7 +430,8 @@ xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId
if (base == NULL)
base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base);
if (xmlBuildURISafe(systemId, (const xmlChar *) base, &URI) < 0)
xmlSAX2ErrMemory(ctxt, NULL);
ret = xmlLoadExternalEntity((const char *) URI,
(const char *) publicId, ctxt);
@ -590,50 +525,61 @@ xmlSAX2EntityDecl(void *ctx, const xmlChar *name, int type,
{
xmlEntityPtr ent;
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
int extSubset;
int res;
if (ctx == NULL) return;
if (ctxt->inSubset == 1) {
ent = xmlAddDocEntity(ctxt->myDoc, name, type, publicId,
systemId, content);
if ((ent == NULL) && (ctxt->pedantic))
xmlWarnMsg(ctxt, XML_WAR_ENTITY_REDEFINED,
"Entity(%s) already defined in the internal subset\n",
name);
if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
xmlChar *URI;
const char *base = NULL;
if ((ctxt == NULL) || (ctxt->myDoc == NULL))
return;
if (ctxt->input != NULL)
base = ctxt->input->filename;
if (base == NULL)
base = ctxt->directory;
extSubset = ctxt->inSubset == 2;
res = xmlAddEntity(ctxt->myDoc, extSubset, name, type, publicId, systemId,
content, &ent);
switch (res) {
case XML_ERR_OK:
break;
case XML_ERR_NO_MEMORY:
xmlSAX2ErrMemory(ctxt, NULL);
return;
case XML_WAR_ENTITY_REDEFINED:
if (ctxt->pedantic) {
if (extSubset)
xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the"
" external subset\n", name);
else
xmlWarnMsg(ctxt, res, "Entity(%s) already defined in the"
" internal subset\n", name);
}
return;
case XML_ERR_REDECL_PREDEF_ENTITY:
/*
* Technically an error but it's a common mistake to get double
* escaping according to "4.6 Predefined Entities" wrong.
*/
xmlWarnMsg(ctxt, res, "Invalid redeclaration of predefined"
" entity '%s'", name);
return;
default:
xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
"Unexpected error code from xmlAddEntity\n",
NULL, NULL);
return;
}
URI = xmlBuildURI(systemId, (const xmlChar *) base);
ent->URI = URI;
}
} else if (ctxt->inSubset == 2) {
ent = xmlAddDtdEntity(ctxt->myDoc, name, type, publicId,
systemId, content);
if ((ent == NULL) && (ctxt->pedantic) &&
(ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
ctxt->sax->warning(ctxt->userData,
"Entity(%s) already defined in the external subset\n", name);
if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
xmlChar *URI;
const char *base = NULL;
if ((ent->URI == NULL) && (systemId != NULL)) {
xmlChar *URI;
const char *base = NULL;
if (ctxt->input != NULL)
base = ctxt->input->filename;
if (base == NULL)
base = ctxt->directory;
if (ctxt->input != NULL)
base = ctxt->input->filename;
if (base == NULL)
base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base);
ent->URI = URI;
}
} else {
xmlFatalErrMsg(ctxt, XML_ERR_ENTITY_PROCESSING,
"SAX.xmlSAX2EntityDecl(%s) called while not in subset\n",
name, NULL);
if (base != NULL) {
if (xmlBuildURISafe(systemId, (const xmlChar *) base, &URI) < 0)
xmlSAX2ErrMemory(ctxt, NULL);
else
ent->URI = URI;
}
}
}
@ -676,6 +622,8 @@ xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
}
/* TODO: optimize name/prefix allocation */
name = xmlSplitQName(ctxt, fullname, &prefix);
if (name == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
ctxt->vctxt.valid = 1;
if (ctxt->inSubset == 1)
attr = xmlAddAttributeDecl(&ctxt->vctxt, ctxt->myDoc->intSubset, elem,
@ -690,6 +638,7 @@ xmlSAX2AttributeDecl(void *ctx, const xmlChar *elem, const xmlChar *fullname,
"SAX.xmlSAX2AttributeDecl(%s) called while not in subset\n",
name, NULL);
xmlFree(name);
xmlFree(prefix);
xmlFreeEnumeration(tree);
return;
}
@ -814,54 +763,8 @@ xmlSAX2UnparsedEntityDecl(void *ctx, const xmlChar *name,
const xmlChar *publicId, const xmlChar *systemId,
const xmlChar *notationName)
{
xmlEntityPtr ent;
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
if (ctx == NULL) return;
if (ctxt->inSubset == 1) {
ent = xmlAddDocEntity(ctxt->myDoc, name,
XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
publicId, systemId, notationName);
if ((ent == NULL) && (ctxt->pedantic) &&
(ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
ctxt->sax->warning(ctxt->userData,
"Entity(%s) already defined in the internal subset\n", name);
if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
xmlChar *URI;
const char *base = NULL;
if (ctxt->input != NULL)
base = ctxt->input->filename;
if (base == NULL)
base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base);
ent->URI = URI;
}
} else if (ctxt->inSubset == 2) {
ent = xmlAddDtdEntity(ctxt->myDoc, name,
XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
publicId, systemId, notationName);
if ((ent == NULL) && (ctxt->pedantic) &&
(ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
ctxt->sax->warning(ctxt->userData,
"Entity(%s) already defined in the external subset\n", name);
if ((ent != NULL) && (ent->URI == NULL) && (systemId != NULL)) {
xmlChar *URI;
const char *base = NULL;
if (ctxt->input != NULL)
base = ctxt->input->filename;
if (base == NULL)
base = ctxt->directory;
URI = xmlBuildURI(systemId, (const xmlChar *) base);
ent->URI = URI;
}
} else {
xmlFatalErrMsg(ctxt, XML_ERR_INTERNAL_ERROR,
"SAX.xmlSAX2UnparsedEntityDecl(%s) called while not in subset\n",
name, NULL);
}
xmlSAX2EntityDecl(ctx, name, XML_EXTERNAL_GENERAL_UNPARSED_ENTITY,
publicId, systemId, (xmlChar *) notationName);
}
/**
@ -891,8 +794,8 @@ xmlSAX2StartDocument(void *ctx)
if (ctx == NULL) return;
if (ctxt->html) {
#ifdef LIBXML_HTML_ENABLED
if (ctxt->html) {
if (ctxt->myDoc == NULL)
ctxt->myDoc = htmlNewDocNoDtD(NULL, NULL);
if (ctxt->myDoc == NULL) {
@ -901,15 +804,9 @@ xmlSAX2StartDocument(void *ctx)
}
ctxt->myDoc->properties = XML_DOC_HTML;
ctxt->myDoc->parseFlags = ctxt->options;
#else
xmlGenericError(xmlGenericErrorContext,
"libxml2 built without HTML support\n");
ctxt->errNo = XML_ERR_INTERNAL_ERROR;
ctxt->instate = XML_PARSER_EOF;
ctxt->disableSAX = 1;
return;
} else
#endif
} else {
{
doc = ctxt->myDoc = xmlNewDoc(ctxt->version);
if (doc != NULL) {
doc->properties = 0;
@ -925,6 +822,10 @@ xmlSAX2StartDocument(void *ctx)
doc->dict = ctxt->dict;
xmlDictReference(doc->dict);
}
if (xmlTreeEnsureXMLDecl(doc) == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
}
if ((ctxt->myDoc != NULL) && (ctxt->myDoc->URL == NULL) &&
(ctxt->input != NULL) && (ctxt->input->filename != NULL)) {
@ -990,15 +891,8 @@ static void LIBXML_ATTR_FORMAT(3,0)
xmlNsErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
(ctxt->instate == XML_PARSER_EOF))
return;
if (ctxt != NULL)
ctxt->errNo = error;
__xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_NAMESPACE, error,
XML_ERR_ERROR, NULL, 0,
(const char *) str1, (const char *) str2,
NULL, 0, 0, msg, str1, str2);
xmlErrParser(ctxt, NULL, XML_FROM_NAMESPACE, error, XML_ERR_ERROR,
str1, str2, NULL, 0, msg, str1, str2);
}
/**
@ -1061,6 +955,8 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
if ((ctxt->html) &&
(value == NULL) && (htmlIsBooleanAttr(fullname))) {
nval = xmlStrdup(fullname);
if (nval == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
value = (const xmlChar *) nval;
} else
#endif
@ -1117,16 +1013,16 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
if (val[0] != 0) {
xmlURIPtr uri;
uri = xmlParseURI((const char *)val);
if (xmlParseURISafe((const char *)val, &uri) < 0)
xmlSAX2ErrMemory(ctxt, NULL);
if (uri == NULL) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
ctxt->sax->warning(ctxt->userData,
"xmlns: %s not a valid URI\n", val);
xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
"xmlns:%s: %s not a valid URI\n", name, value);
} else {
if (uri->scheme == NULL) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL))
ctxt->sax->warning(ctxt->userData,
"xmlns: URI %s is not absolute\n", val);
xmlNsWarnMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
"xmlns:%s: URI %s is not absolute\n",
name, value);
}
xmlFreeURI(uri);
}
@ -1134,16 +1030,19 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
/* a default namespace definition */
nsret = xmlNewNs(ctxt->node, val, NULL);
if (nsret == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
}
#ifdef LIBXML_VALID_ENABLED
/*
* Validate also for namespace decls, they are attributes from
* an XML-1.0 perspective
*/
if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset)
else if (ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset) {
ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
ctxt->node, prefix, nsret, val);
}
#endif /* LIBXML_VALID_ENABLED */
if (name != NULL)
xmlFree(name);
@ -1187,7 +1086,8 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
if ((ctxt->pedantic != 0) && (val[0] != 0)) {
xmlURIPtr uri;
uri = xmlParseURI((const char *)val);
if (xmlParseURISafe((const char *)val, &uri) < 0)
xmlSAX2ErrMemory(ctxt, NULL);
if (uri == NULL) {
xmlNsWarnMsg(ctxt, XML_WAR_NS_URI,
"xmlns:%s: %s not a valid URI\n", name, value);
@ -1203,15 +1103,20 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
/* a standard namespace definition */
nsret = xmlNewNs(ctxt->node, val, name);
xmlFree(ns);
if (nsret == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
}
#ifdef LIBXML_VALID_ENABLED
/*
* Validate also for namespace decls, they are attributes from
* an XML-1.0 perspective
*/
if (nsret != NULL && ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset)
else if (ctxt->validate && ctxt->wellFormed &&
ctxt->myDoc && ctxt->myDoc->intSubset) {
ctxt->valid &= xmlValidateOneNamespace(&ctxt->vctxt, ctxt->myDoc,
ctxt->node, prefix, nsret, value);
}
#endif /* LIBXML_VALID_ENABLED */
if (name != NULL)
xmlFree(name);
@ -1257,13 +1162,19 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
/* !!!!!! <a toto:arg="" xmlns:toto="http://toto.com"> */
ret = xmlNewNsPropEatName(ctxt->node, namespace, name, NULL);
if (ret == NULL)
if (ret == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
goto error;
}
if ((ctxt->replaceEntities == 0) && (!ctxt->html)) {
xmlNodePtr tmp;
ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
if ((value != NULL) && (value[0] != 0)) {
ret->children = xmlStringGetNodeList(ctxt->myDoc, value);
if (ret->children == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
}
tmp = ret->children;
while (tmp != NULL) {
tmp->parent = (xmlNodePtr) ret;
@ -1273,9 +1184,12 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
}
} else if (value != NULL) {
ret->children = xmlNewDocText(ctxt->myDoc, value);
ret->last = ret->children;
if (ret->children != NULL)
if (ret->children == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
} else {
ret->last = ret->children;
ret->children->parent = (xmlNodePtr) ret;
}
}
#ifdef LIBXML_VALID_ENABLED
@ -1305,8 +1219,9 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
* It need to be done twice ... it's an extra burden related
* to the ability to keep xmlSAX2References in attributes
*/
nvalnorm = xmlValidNormalizeAttributeValue(ctxt->myDoc,
ctxt->node, fullname, val);
nvalnorm = xmlValidCtxtNormalizeAttributeValue(
&ctxt->vctxt, ctxt->myDoc,
ctxt->node, fullname, val);
if (nvalnorm != NULL) {
xmlFree(val);
val = nvalnorm;
@ -1342,14 +1257,20 @@ xmlSAX2AttributeInternal(void *ctx, const xmlChar *fullname,
*/
if (xmlValidateNCName(content, 1) != 0) {
xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
"xml:id : attribute value %s is not an NCName\n",
(const char *) content, NULL);
"xml:id : attribute value %s is not an NCName\n",
content, NULL);
}
xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
} else if (xmlIsID(ctxt->myDoc, ctxt->node, ret))
xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
} else {
int res = xmlIsID(ctxt->myDoc, ctxt->node, ret);
if (res < 0)
xmlErrMemory(ctxt, NULL);
else if (res > 0)
xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
}
}
error:
@ -1429,8 +1350,8 @@ process_external_subset:
if (att == NULL) {
xmlErrValid(ctxt, XML_DTD_STANDALONE_DEFAULTED,
"standalone: attribute %s on %s defaulted from external subset\n",
(const char *)fulln,
(const char *)attr->elem);
fulln,
attr->elem);
}
xmlFree(fulln);
}
@ -1557,6 +1478,10 @@ xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
* Split the full name into a namespace prefix and the tag name
*/
name = xmlSplitQName(ctxt, fullname, &prefix);
if (name == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
}
/*
@ -1566,8 +1491,7 @@ xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
*/
ret = xmlNewDocNodeEatName(ctxt->myDoc, NULL, name, NULL);
if (ret == NULL) {
if (prefix != NULL)
xmlFree(prefix);
xmlFree(prefix);
xmlSAX2ErrMemory(ctxt, "xmlSAX2StartElement");
return;
}
@ -1637,10 +1561,12 @@ xmlSAX2StartElement(void *ctx, const xmlChar *fullname, const xmlChar **atts)
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);
ns = xmlNewNs(ret, NULL, prefix);
if (ns == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
}
/*
@ -1782,6 +1708,11 @@ xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
} else if ((len <= 3) && ((cur == '"') || (cur == '\'') ||
((cur == '<') && (str[len + 1] != '!')))) {
intern = xmlDictLookup(ctxt->dict, str, len);
if (intern == NULL) {
xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
xmlFree(ret);
return(NULL);
}
} else if (IS_BLANK_CH(*str) && (len < 60) && (cur == '<') &&
(str[len + 1] != '!')) {
int i;
@ -1790,6 +1721,11 @@ xmlSAX2TextNode(xmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
if (!IS_BLANK_CH(str[i])) goto skip;
}
intern = xmlDictLookup(ctxt->dict, str, len);
if (intern == NULL) {
xmlSAX2ErrMemory(ctxt, "xmlSAX2TextNode");
xmlFree(ret);
return(NULL);
}
}
}
skip:
@ -1910,10 +1846,13 @@ xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
ret->doc = ctxt->node->doc;
ret->ns = namespace;
if (ctxt->dictNames)
if (ctxt->dictNames) {
ret->name = localname;
else
} else {
ret->name = xmlStrdup(localname);
if (ret->name == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
}
if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
xmlRegisterNodeDefaultValue((xmlNodePtr)ret);
@ -1934,9 +1873,11 @@ xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
tmp->doc = ret->doc;
tmp->parent = (xmlNodePtr) ret;
}
} else {
} else if (valueend > value) {
ret->children = xmlStringLenGetNodeList(ctxt->myDoc, value,
valueend - value);
if (ret->children == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
tmp = ret->children;
while (tmp != NULL) {
tmp->doc = ret->doc;
@ -1978,6 +1919,8 @@ xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
* entry points in the full validation code
*/
dup = xmlStrndup(value, valueend - value);
if (dup == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
ctxt->myDoc, ctxt->node, ret, dup);
@ -2022,6 +1965,8 @@ xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
* the attribute as passed is already normalized
*/
dup = xmlStrndup(value, valueend - value);
if (dup == NULL)
xmlSAX2ErrMemory(ctxt, NULL);
ctxt->valid &= xmlValidateOneAttribute(&ctxt->vctxt,
ctxt->myDoc, ctxt->node, ret, dup);
@ -2050,14 +1995,19 @@ xmlSAX2AttributeNs(xmlParserCtxtPtr ctxt,
*/
if (xmlValidateNCName(content, 1) != 0) {
xmlErrValid(ctxt, XML_DTD_XMLID_VALUE,
"xml:id : attribute value %s is not an NCName\n",
(const char *) content, NULL);
"xml:id : attribute value %s is not an NCName\n",
content, NULL);
}
xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
} else if (xmlIsID(ctxt->myDoc, ctxt->node, ret)) {
xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
} else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret)) {
xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
} else {
int res = xmlIsID(ctxt->myDoc, ctxt->node, ret);
if (res < 0)
xmlErrMemory(ctxt, NULL);
else if (res > 0)
xmlAddID(&ctxt->vctxt, ctxt->myDoc, content, ret);
else if (xmlIsRef(ctxt->myDoc, ctxt->node, ret))
xmlAddRef(&ctxt->vctxt, ctxt->myDoc, content, ret);
}
}
if (dup != NULL)
@ -2125,10 +2075,17 @@ xmlSAX2StartElementNs(void *ctx,
const xmlChar *fullname;
fullname = xmlDictQLookup(ctxt->dict, prefix, localname);
if (fullname != NULL)
localname = fullname;
if (fullname == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
localname = fullname;
} else {
lname = xmlBuildQName(localname, prefix, NULL, 0);
if (lname == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
}
}
/*
@ -2197,11 +2154,7 @@ xmlSAX2StartElementNs(void *ctx,
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
*/
xmlSAX2ErrMemory(ctxt, NULL);
continue;
}
@ -2288,22 +2241,26 @@ xmlSAX2StartElementNs(void *ctx,
fullname = xmlDictQLookup(ctxt->dict, attributes[j+1],
attributes[j]);
if (fullname != NULL) {
attr = xmlSAX2AttributeNs(ctxt, fullname, NULL,
attributes[j+3],
attributes[j+4]);
goto have_attr;
}
if (fullname == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
attr = xmlSAX2AttributeNs(ctxt, fullname, NULL,
attributes[j+3],
attributes[j+4]);
goto have_attr;
} else {
lname = xmlBuildQName(attributes[j], attributes[j+1],
NULL, 0);
if (lname != NULL) {
attr = xmlSAX2AttributeNs(ctxt, lname, NULL,
attributes[j+3],
attributes[j+4]);
xmlFree(lname);
goto have_attr;
}
if (lname == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
attr = xmlSAX2AttributeNs(ctxt, lname, NULL,
attributes[j+3],
attributes[j+4]);
xmlFree(lname);
goto have_attr;
}
}
attr = xmlSAX2AttributeNs(ctxt, attributes[j], attributes[j+1],
@ -2393,6 +2350,10 @@ xmlSAX2Reference(void *ctx, const xmlChar *name)
if (ctx == NULL) return;
ret = xmlNewReference(ctxt->myDoc, name);
if (ret == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
if (xmlAddChild(ctxt->node, ret) == NULL) {
xmlFreeNode(ret);
}
@ -2513,7 +2474,9 @@ xmlSAX2Text(xmlParserCtxtPtr ctxt, const xmlChar *ch, int len,
lastChild->doc = ctxt->myDoc;
} else
lastChild = xmlNewCDataBlock(ctxt->myDoc, ch, len);
if (lastChild != NULL) {
if (lastChild == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
} else {
xmlAddChild(ctxt->node, lastChild);
if (ctxt->node->children != NULL) {
ctxt->nodelen = len;
@ -2585,7 +2548,10 @@ xmlSAX2ProcessingInstruction(void *ctx, const xmlChar *target,
parent = ctxt->node;
ret = xmlNewDocPI(ctxt->myDoc, target, data);
if (ret == NULL) return;
if (ret == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
if (ctxt->linenumbers) {
if (ctxt->input != NULL) {
@ -2630,7 +2596,10 @@ xmlSAX2Comment(void *ctx, const xmlChar *value)
if (ctx == NULL) return;
parent = ctxt->node;
ret = xmlNewDocComment(ctxt->myDoc, value);
if (ret == NULL) return;
if (ret == NULL) {
xmlSAX2ErrMemory(ctxt, NULL);
return;
}
if (ctxt->linenumbers) {
if (ctxt->input != NULL) {
if ((unsigned) ctxt->input->line < (unsigned) USHRT_MAX)

View File

@ -0,0 +1,3 @@
./test/errors/ent_redecl.xml:2: parser warning : Invalid redeclaration of predefined entity 'lt'
<!ENTITY lt '<'>
^

View File

@ -0,0 +1,3 @@
./test/errors/ent_redecl.xml:2: parser warning : Invalid redeclaration of predefined entity 'lt'
<!ENTITY lt '<'>
^

View File

@ -0,0 +1,3 @@
./test/errors/ent_redecl.xml:2: parser warning : Invalid redeclaration of predefined entity 'lt'
<!ENTITY lt '<'>
^