From bd9eed4694fe56e8b26405cefb3d22637b21a4d4 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 2 Sep 2024 18:37:41 +0200 Subject: [PATCH] parser: Make unsupported encodings an error in declarations This was changed in 45157261, but in encoding declarations, unsupported encodings should raise a fatal error. Fixes #794. --- parserInternals.c | 40 ++++++++++++++++------ result/errors/unsupported-encoding.xml.ent | 3 ++ result/errors/unsupported-encoding.xml.err | 3 ++ result/errors/unsupported-encoding.xml.str | 4 +++ test/errors/unsupported-encoding.xml | 2 ++ 5 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 result/errors/unsupported-encoding.xml.ent create mode 100644 result/errors/unsupported-encoding.xml.err create mode 100644 result/errors/unsupported-encoding.xml.str create mode 100644 test/errors/unsupported-encoding.xml diff --git a/parserInternals.c b/parserInternals.c index eb13be7e..a6ddf502 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -475,20 +475,14 @@ void xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors code, const char *info) { const char *errmsg; - xmlErrorLevel level; - - 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, code, level, + xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, XML_ERR_FATAL, NULL, NULL, NULL, 0, "%s\n", errmsg); } else { - xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level, + xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, XML_ERR_FATAL, (const xmlChar *) info, NULL, NULL, 0, "%s: %s\n", errmsg, info); } @@ -1223,7 +1217,11 @@ xmlSwitchInputEncodingName(xmlParserCtxtPtr ctxt, xmlParserInputPtr input, res = xmlCreateCharEncodingHandler(encoding, /* output */ 0, ctxt->convImpl, ctxt->convCtxt, &handler); - if (res != XML_ERR_OK) { + if (res == XML_ERR_UNSUPPORTED_ENCODING) { + xmlWarningMsg(ctxt, XML_ERR_UNSUPPORTED_ENCODING, + "Unsupported encoding: %s\n", BAD_CAST encoding, NULL); + return(-1); + } else if (res != XML_ERR_OK) { xmlFatalErr(ctxt, res, encoding); return(-1); } @@ -1535,7 +1533,29 @@ void xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) { if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) && ((ctxt->options & XML_PARSE_IGNORE_ENC) == 0)) { - xmlSwitchEncodingName(ctxt, (const char *) encoding); + xmlCharEncodingHandlerPtr handler; + int res; + + /* + * xmlSwitchEncodingName treats unsupported encodings as + * warnings, but we want it to be an error in an encoding + * declaration. + */ + res = xmlCreateCharEncodingHandler((const char *) encoding, + /* output */ 0, ctxt->convImpl, ctxt->convCtxt, &handler); + if (res != XML_ERR_OK) { + xmlFatalErr(ctxt, res, (const char *) encoding); + xmlFree(encoding); + return; + } + + res = xmlInputSetEncodingHandler(ctxt->input, handler); + if (res != XML_ERR_OK) { + xmlCtxtErrIO(ctxt, res, NULL); + xmlFree(encoding); + return; + } + ctxt->input->flags |= XML_INPUT_USES_ENC_DECL; } else if (ctxt->input->flags & XML_INPUT_AUTO_ENCODING) { static const char *allowedUTF8[] = { diff --git a/result/errors/unsupported-encoding.xml.ent b/result/errors/unsupported-encoding.xml.ent new file mode 100644 index 00000000..a275ec19 --- /dev/null +++ b/result/errors/unsupported-encoding.xml.ent @@ -0,0 +1,3 @@ +./test/errors/unsupported-encoding.xml:1: parser error : Unsupported encoding: unsupported-encoding + + ^ diff --git a/result/errors/unsupported-encoding.xml.err b/result/errors/unsupported-encoding.xml.err new file mode 100644 index 00000000..a275ec19 --- /dev/null +++ b/result/errors/unsupported-encoding.xml.err @@ -0,0 +1,3 @@ +./test/errors/unsupported-encoding.xml:1: parser error : Unsupported encoding: unsupported-encoding + + ^ diff --git a/result/errors/unsupported-encoding.xml.str b/result/errors/unsupported-encoding.xml.str new file mode 100644 index 00000000..1f06d608 --- /dev/null +++ b/result/errors/unsupported-encoding.xml.str @@ -0,0 +1,4 @@ +./test/errors/unsupported-encoding.xml:1: parser error : Unsupported encoding: unsupported-encoding + + ^ +./test/errors/unsupported-encoding.xml : failed to parse diff --git a/test/errors/unsupported-encoding.xml b/test/errors/unsupported-encoding.xml new file mode 100644 index 00000000..8aba00e4 --- /dev/null +++ b/test/errors/unsupported-encoding.xml @@ -0,0 +1,2 @@ + +