1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-01-12 09:17:37 +03:00

parser: Fix XML_ERR_UNSUPPORTED_ENCODING errors

Commit 45157261 added the check in the wrong place.

Also allow unsupported encoding in xmlNewInputInternal.

Fixes #654.
This commit is contained in:
Nick Wellnhofer 2023-12-29 18:47:30 +01:00
parent e45a4d7115
commit 16b0dbc1b3
3 changed files with 47 additions and 15 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -9,6 +9,34 @@
#include <string.h>
static int
testUnsupportedEncoding(void) {
xmlDocPtr doc;
const xmlError *error;
int err = 0;
xmlResetLastError();
doc = xmlReadDoc(BAD_CAST "<doc/>", 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