From 16b0dbc1b3d1b8bdf0340b60119f36e6b45fd7a7 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Fri, 29 Dec 2023 18:47:30 +0100 Subject: [PATCH] parser: Fix XML_ERR_UNSUPPORTED_ENCODING errors Commit 45157261 added the check in the wrong place. Also allow unsupported encoding in xmlNewInputInternal. Fixes #654. --- error.c | 3 +++ parserInternals.c | 30 +++++++++++++++--------------- testparser.c | 29 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/error.c b/error.c index 4f71c036..a68451e4 100644 --- a/error.c +++ b/error.c @@ -1161,6 +1161,9 @@ xmlErrString(xmlParserErrors code) { case XML_ERR_REDECL_PREDEF_ENTITY: errmsg = "Invalid redeclaration of predefined entity"; break; + case XML_ERR_UNSUPPORTED_ENCODING: + errmsg = "Unsupported encoding"; + break; case XML_IO_UNKNOWN: errmsg = "Unknown IO error"; break; diff --git a/parserInternals.c b/parserInternals.c index 8a15fdaa..4e0920d9 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -175,11 +175,9 @@ xmlCtxtErrIO(xmlParserCtxtPtr ctxt, int code, const char *uri) if (ctxt == NULL) return; - if (code == XML_ERR_UNSUPPORTED_ENCODING) { - level = XML_ERR_WARNING; - } else if ((code == XML_IO_ENOENT) || - (code == XML_IO_NETWORK_ATTEMPT) || - (code == XML_IO_UNKNOWN)) { + if ((code == XML_IO_ENOENT) || + (code == XML_IO_NETWORK_ATTEMPT) || + (code == XML_IO_UNKNOWN)) { if (ctxt->validate == 0) level = XML_ERR_WARNING; else @@ -318,17 +316,23 @@ xmlCtxtErr(xmlParserCtxtPtr ctxt, xmlNodePtr node, xmlErrorDomain domain, * Handle a fatal parser error, i.e. violating Well-Formedness constraints */ void -xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors error, const char *info) +xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors code, const char *info) { const char *errmsg; + xmlErrorLevel level; - errmsg = xmlErrString(error); + if (code == XML_ERR_UNSUPPORTED_ENCODING) + level = XML_ERR_WARNING; + else + level = XML_ERR_FATAL; + + errmsg = xmlErrString(code); if (info == NULL) { - xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, + xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level, NULL, NULL, NULL, 0, "%s\n", errmsg); } else { - xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, error, XML_ERR_FATAL, + xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level, (const xmlChar *) info, NULL, NULL, 0, "%s: %s\n", errmsg, info); } @@ -1560,12 +1564,8 @@ xmlNewInputInternal(xmlParserCtxtPtr ctxt, xmlParserInputBufferPtr buf, } } - if (encoding != NULL) { - if (xmlSwitchInputEncodingName(ctxt, input, encoding) < 0) { - xmlFreeInputStream(input); - return(NULL); - } - } + if (encoding != NULL) + xmlSwitchInputEncodingName(ctxt, input, encoding); return(input); } diff --git a/testparser.c b/testparser.c index 287f08b6..35cd27b5 100644 --- a/testparser.c +++ b/testparser.c @@ -9,6 +9,34 @@ #include +static int +testUnsupportedEncoding(void) { + xmlDocPtr doc; + const xmlError *error; + int err = 0; + + xmlResetLastError(); + + doc = xmlReadDoc(BAD_CAST "", NULL, "#unsupported", + XML_PARSE_NOWARNING); + if (doc == NULL) { + fprintf(stderr, "xmlReadDoc failed with unsupported encoding\n"); + err = 1; + } + xmlFreeDoc(doc); + + error = xmlGetLastError(); + if (error->code != XML_ERR_UNSUPPORTED_ENCODING || + error->level != XML_ERR_WARNING || + strcmp(error->message, "Unsupported encoding: #unsupported\n") != 0) + { + fprintf(stderr, "xmlReadDoc failed to raise correct error\n"); + err = 1; + } + + return err; +} + #ifdef LIBXML_SAX1_ENABLED static int testBalancedChunk(void) { @@ -179,6 +207,7 @@ int main(void) { int err = 0; + err |= testUnsupportedEncoding(); #ifdef LIBXML_SAX1_ENABLED err |= testBalancedChunk(); #endif