1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-07-30 22:43:14 +03:00

more error/warning handling cleanups, the XML parser module should be okay

* parser.c include/libxml/xmlerror.h: more error/warning
  handling cleanups, the XML parser module should be okay now.
Daniel
This commit is contained in:
Daniel Veillard
2003-10-04 21:08:09 +00:00
parent 1ae42d653e
commit 24eb97851d
3 changed files with 117 additions and 79 deletions

View File

@ -1,3 +1,8 @@
Sat Oct 4 23:06:41 CEST 2003 Daniel Veillard <daniel@veillard.com>
* parser.c include/libxml/xmlerror.h: more error/warning
handling cleanups, the XML parser module should be okay now.
Sat Oct 4 01:58:27 CEST 2003 Daniel Veillard <daniel@veillard.com> Sat Oct 4 01:58:27 CEST 2003 Daniel Veillard <daniel@veillard.com>
* Makefile.am configure.in xmldwalk.c include/libxml/Makefile.am * Makefile.am configure.in xmldwalk.c include/libxml/Makefile.am

View File

@ -243,7 +243,11 @@ typedef enum {
XML_DTD_UNKNOWN_ELEM, XML_DTD_UNKNOWN_ELEM,
XML_DTD_UNKNOWN_ENTITY, XML_DTD_UNKNOWN_ENTITY,
XML_DTD_UNKNOWN_ID, XML_DTD_UNKNOWN_ID,
XML_DTD_UNKNOWN_NOTATION XML_DTD_UNKNOWN_NOTATION,
XML_WAR_UNKNOWN_VERSION,
XML_WAR_LANG_VALUE,
XML_WAR_NS_URI,
XML_WAR_NS_URI_RELATIVE
} xmlParserErrors; } xmlParserErrors;
/** /**

185
parser.c
View File

@ -405,6 +405,50 @@ xmlFatalErrMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
ctxt->disableSAX = 1; ctxt->disableSAX = 1;
} }
/**
* xmlWarningMsg:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: extra data
* @str2: extra data
*
* Handle a warning.
*/
static void
xmlWarningMsg(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1, const xmlChar *str2)
{
ctxt->errNo = error;
__xmlRaiseError((ctxt->sax) ? ctxt->sax->warning : NULL, ctxt->userData,
ctxt, NULL, XML_FROM_PARSER, error,
XML_ERR_WARNING, NULL, 0,
(const char *) str1, (const char *) str2, NULL, 0, 0,
msg, (const char *) str1, (const char *) str2);
}
/**
* xmlValidityError:
* @ctxt: an XML parser context
* @error: the error number
* @msg: the error message
* @str1: extra data
*
* Handle a warning.
*/
static void
xmlValidityError(xmlParserCtxtPtr ctxt, xmlParserErrors error,
const char *msg, const xmlChar *str1)
{
ctxt->errNo = error;
__xmlRaiseError(ctxt->vctxt.error, ctxt->vctxt.userData,
ctxt, NULL, XML_FROM_DTD, error,
XML_ERR_ERROR, NULL, 0, (const char *) str1,
NULL, NULL, 0, 0,
msg, (const char *) str1);
ctxt->valid = 0;
}
/** /**
* xmlFatalErrMsgInt: * xmlFatalErrMsgInt:
* @ctxt: an XML parser context * @ctxt: an XML parser context
@ -1658,14 +1702,14 @@ xmlParserHandlePEReference(xmlParserCtxtPtr ctxt) {
* ... The declaration of a parameter entity must precede * ... The declaration of a parameter entity must precede
* any reference to it... * any reference to it...
*/ */
if ((!ctxt->disableSAX) && if ((ctxt->validate) && (ctxt->vctxt.error != NULL)) {
(ctxt->validate) && (ctxt->vctxt.error != NULL)) { xmlValidityError(ctxt, XML_WAR_UNDECLARED_ENTITY,
ctxt->vctxt.error(ctxt->vctxt.userData, "PEReference: %%%s; not found\n",
"PEReference: %%%s; not found\n", name); name);
} else if ((!ctxt->disableSAX) && } else
(ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
ctxt->sax->warning(ctxt->userData, "PEReference: %%%s; not found\n",
"PEReference: %%%s; not found\n", name); name, NULL);
ctxt->valid = 0; ctxt->valid = 0;
} }
} else if (ctxt->input->free != deallocblankswrapper) { } else if (ctxt->input->free != deallocblankswrapper) {
@ -3836,11 +3880,9 @@ xmlParsePITarget(xmlParserCtxtPtr ctxt) {
if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i])) if (xmlStrEqual(name, (const xmlChar *)xmlW3CPIs[i]))
return(name); return(name);
} }
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) { xmlWarningMsg(ctxt, XML_ERR_RESERVED_XML_NAME,
ctxt->errNo = XML_ERR_RESERVED_XML_NAME; "xmlParsePITarget: invalid name prefix 'xml'\n",
ctxt->sax->warning(ctxt->userData, NULL, NULL);
"xmlParsePITarget: invalid name prefix 'xml'\n");
}
} }
return(name); return(name);
} }
@ -3899,10 +3941,9 @@ xmlParseCatalogPI(xmlParserCtxtPtr ctxt, const xmlChar *catalog) {
return; return;
error: error:
ctxt->errNo = XML_WAR_CATALOG_PI; xmlWarningMsg(ctxt, XML_WAR_CATALOG_PI,
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) "Catalog PI syntax error: %s\n",
ctxt->sax->warning(ctxt->userData, catalog, NULL);
"Catalog PI syntax error: %s\n", catalog);
if (URL != NULL) if (URL != NULL)
xmlFree(URL); xmlFree(URL);
} }
@ -4860,11 +4901,9 @@ xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
SHRINK; SHRINK;
if (RAW == ')') { if (RAW == ')') {
if ((ctxt->validate) && (ctxt->input->id != inputchk)) { if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
ctxt->errNo = XML_ERR_ENTITY_BOUNDARY; xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
if (ctxt->vctxt.error != NULL) "Element content declaration doesn't start and stop in the same entity\n",
ctxt->vctxt.error(ctxt->vctxt.userData, NULL);
"Element content declaration doesn't start and stop in the same entity\n");
ctxt->valid = 0;
} }
NEXT; NEXT;
ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA); ret = xmlNewElementContent(NULL, XML_ELEMENT_CONTENT_PCDATA);
@ -4918,11 +4957,9 @@ xmlParseElementMixedContentDecl(xmlParserCtxtPtr ctxt, int inputchk) {
} }
ret->ocur = XML_ELEMENT_CONTENT_MULT; ret->ocur = XML_ELEMENT_CONTENT_MULT;
if ((ctxt->validate) && (ctxt->input->id != inputchk)) { if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
ctxt->errNo = XML_ERR_ENTITY_BOUNDARY; xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
if (ctxt->vctxt.error != NULL) "Element content declaration doesn't start and stop in the same entity\n",
ctxt->vctxt.error(ctxt->vctxt.userData, NULL);
"Element content declaration doesn't start and stop in the same entity\n");
ctxt->valid = 0;
} }
SKIP(2); SKIP(2);
} else { } else {
@ -5145,11 +5182,9 @@ xmlParseElementChildrenContentDecl (xmlParserCtxtPtr ctxt, int inputchk) {
last->parent = cur; last->parent = cur;
} }
if ((ctxt->validate) && (ctxt->input->id != inputchk)) { if ((ctxt->validate) && (ctxt->input->id != inputchk)) {
ctxt->errNo = XML_ERR_ENTITY_BOUNDARY; xmlValidityError(ctxt, XML_ERR_ENTITY_BOUNDARY,
if (ctxt->vctxt.error != NULL) "Element content declaration doesn't start and stop in the same entity\n",
ctxt->vctxt.error(ctxt->vctxt.userData, NULL);
"Element content declaration doesn't start and stop in the same entity\n");
ctxt->valid = 0;
} }
NEXT; NEXT;
if (RAW == '?') { if (RAW == '?') {
@ -6295,11 +6330,11 @@ xmlParseStringEntityRef(xmlParserCtxtPtr ctxt, const xmlChar ** str) {
xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY, xmlFatalErrMsgStr(ctxt, XML_ERR_UNDECLARED_ENTITY,
"Entity '%s' not defined\n", name); "Entity '%s' not defined\n", name);
} else { } else {
ctxt->errNo = XML_WAR_UNDECLARED_ENTITY; xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) "Entity '%s' not defined\n",
ctxt->sax->warning(ctxt->userData, name, NULL);
"Entity '%s' not defined\n", name);
} }
/* TODO ? check regressions ctxt->valid = 0; */
} }
/* /*
@ -6455,10 +6490,9 @@ xmlParsePEReference(xmlParserCtxtPtr ctxt) {
* ... The declaration of a parameter entity must precede * ... The declaration of a parameter entity must precede
* any reference to it... * any reference to it...
*/ */
if ((!ctxt->disableSAX) && xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
(ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) "PEReference: %%%s; not found\n",
ctxt->sax->warning(ctxt->userData, name, NULL);
"PEReference: %%%s; not found\n", name);
ctxt->valid = 0; ctxt->valid = 0;
} }
} else { } else {
@ -6467,9 +6501,9 @@ xmlParsePEReference(xmlParserCtxtPtr ctxt) {
*/ */
if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
(entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
ctxt->sax->warning(ctxt->userData, "Internal: %%%s; is not a parameter entity\n",
"Internal: %%%s; is not a parameter entity\n", name); name, NULL);
} else if (ctxt->input->free != deallocblankswrapper) { } else if (ctxt->input->free != deallocblankswrapper) {
input = xmlNewBlanksWrapperInputStream(ctxt, entity); input = xmlNewBlanksWrapperInputStream(ctxt, entity);
xmlPushInput(ctxt, input); xmlPushInput(ctxt, input);
@ -6584,9 +6618,9 @@ xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
* ... The declaration of a parameter entity must * ... The declaration of a parameter entity must
* precede any reference to it... * precede any reference to it...
*/ */
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
ctxt->sax->warning(ctxt->userData, "PEReference: %%%s; not found\n",
"PEReference: %%%s; not found\n", name); name, NULL);
ctxt->valid = 0; ctxt->valid = 0;
} }
} else { } else {
@ -6595,9 +6629,9 @@ xmlParseStringPEReference(xmlParserCtxtPtr ctxt, const xmlChar **str) {
*/ */
if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) && if ((entity->etype != XML_INTERNAL_PARAMETER_ENTITY) &&
(entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) { (entity->etype != XML_EXTERNAL_PARAMETER_ENTITY)) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_UNDECLARED_ENTITY,
ctxt->sax->warning(ctxt->userData, "%%%s; is not a parameter entity\n",
"Internal: %%%s; is not a parameter entity\n", name); name, NULL);
} }
} }
ctxt->hasPErefs = 1; ctxt->hasPErefs = 1;
@ -6813,9 +6847,9 @@ xmlParseAttribute(xmlParserCtxtPtr ctxt, xmlChar **value) {
*/ */
if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
if (!xmlCheckLanguageID(val)) { if (!xmlCheckLanguageID(val)) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
ctxt->sax->warning(ctxt->userData, "Malformed value for xml:lang : %s\n",
"Malformed value for xml:lang : %s\n", val); val, NULL);
} }
} }
@ -7549,9 +7583,9 @@ xmlParseAttribute2(xmlParserCtxtPtr ctxt,
*/ */
if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) { if ((ctxt->pedantic) && (xmlStrEqual(name, BAD_CAST "xml:lang"))) {
if (!xmlCheckLanguageID(val)) { if (!xmlCheckLanguageID(val)) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_LANG_VALUE,
ctxt->sax->warning(ctxt->userData, "Malformed value for xml:lang : %s\n",
"Malformed value for xml:lang : %s\n", val); val, NULL);
} }
} }
@ -7673,15 +7707,14 @@ reparse:
if (*URL != 0) { if (*URL != 0) {
uri = xmlParseURI((const char *) URL); uri = xmlParseURI((const char *) URL);
if (uri == NULL) { if (uri == NULL) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_NS_URI,
ctxt->sax->warning(ctxt->userData, "xmlns: %s not a valid URI\n",
"xmlns: %s not a valid URI\n", URL); URL, NULL);
} else { } else {
if (uri->scheme == NULL) { if (uri->scheme == NULL) {
if ((ctxt->sax != NULL) && xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
(ctxt->sax->warning != NULL)) "xmlns: URI %s is not absolute\n",
ctxt->sax->warning(ctxt->userData, URL, NULL);
"xmlns: URI %s is not absolute\n", URL);
} }
xmlFreeURI(uri); xmlFreeURI(uri);
} }
@ -7719,17 +7752,14 @@ reparse:
} }
uri = xmlParseURI((const char *) URL); uri = xmlParseURI((const char *) URL);
if (uri == NULL) { if (uri == NULL) {
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_NS_URI,
ctxt->sax->warning(ctxt->userData, "xmlns:%s: '%s' is not a valid URI\n",
"xmlns:%s: '%s' is not a valid URI\n", attname, URL);
attname, URL);
} else { } else {
if ((ctxt->pedantic) && (uri->scheme == NULL)) { if ((ctxt->pedantic) && (uri->scheme == NULL)) {
if ((ctxt->sax != NULL) && xmlWarningMsg(ctxt, XML_WAR_NS_URI_RELATIVE,
(ctxt->sax->warning != NULL)) "xmlns:%s: URI %s is not absolute\n",
ctxt->sax->warning(ctxt->userData, attname, URL);
"xmlns:%s: URI %s is not absolute\n",
attname, URL);
} }
xmlFreeURI(uri); xmlFreeURI(uri);
} }
@ -8748,9 +8778,9 @@ xmlParseXMLDecl(xmlParserCtxtPtr ctxt) {
/* /*
* TODO: Blueberry should be detected here * TODO: Blueberry should be detected here
*/ */
if ((ctxt->sax != NULL) && (ctxt->sax->warning != NULL)) xmlWarningMsg(ctxt, XML_WAR_UNKNOWN_VERSION,
ctxt->sax->warning(ctxt->userData, "Unsupported version '%s'\n", "Unsupported version '%s'\n",
version); version, NULL);
} }
if (ctxt->version != NULL) if (ctxt->version != NULL)
xmlFree((void *) ctxt->version); xmlFree((void *) ctxt->version);
@ -10101,6 +10131,7 @@ xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size,
nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw); nbchars = xmlCharEncInFunc(in->encoder, in->buffer, in->raw);
if (nbchars < 0) { if (nbchars < 0) {
/* TODO 2.6.0 */
xmlGenericError(xmlGenericErrorContext, xmlGenericError(xmlGenericErrorContext,
"xmlParseChunk: encoder error\n"); "xmlParseChunk: encoder error\n");
return(XML_ERR_INVALID_ENCODING); return(XML_ERR_INVALID_ENCODING);
@ -10196,8 +10227,7 @@ xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data,
ctxt = xmlNewParserCtxt(); ctxt = xmlNewParserCtxt();
if (ctxt == NULL) { if (ctxt == NULL) {
xmlGenericError(xmlGenericErrorContext, xmlErrMemory(NULL, "creating parser: out of memory\n");
"xml parser: out of memory\n");
xmlFreeParserInputBuffer(buf); xmlFreeParserInputBuffer(buf);
return(NULL); return(NULL);
} }
@ -11602,8 +11632,7 @@ xmlSetupParserForBuffer(xmlParserCtxtPtr ctxt, const xmlChar* buffer,
input = xmlNewInputStream(ctxt); input = xmlNewInputStream(ctxt);
if (input == NULL) { if (input == NULL) {
xmlGenericError(xmlGenericErrorContext, xmlErrMemory(NULL, "parsing new buffer: out of memory\n");
"malloc");
xmlFree(ctxt); xmlFree(ctxt);
return; return;
} }