Skip to content

Commit 65db397

Browse files
committed
parser: Make unsupported encodings an error in declarations
This was changed in 4515726, but in encoding declarations, unsupported encodings should raise a fatal error. Fixes #794.
1 parent 4224a3f commit 65db397

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

parserInternals.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -364,20 +364,14 @@ void
364364
xmlFatalErr(xmlParserCtxtPtr ctxt, xmlParserErrors code, const char *info)
365365
{
366366
const char *errmsg;
367-
xmlErrorLevel level;
368-
369-
if (code == XML_ERR_UNSUPPORTED_ENCODING)
370-
level = XML_ERR_WARNING;
371-
else
372-
level = XML_ERR_FATAL;
373367

374368
errmsg = xmlErrString(code);
375369

376370
if (info == NULL) {
377-
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level,
371+
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, XML_ERR_FATAL,
378372
NULL, NULL, NULL, 0, "%s\n", errmsg);
379373
} else {
380-
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, level,
374+
xmlCtxtErr(ctxt, NULL, XML_FROM_PARSER, code, XML_ERR_FATAL,
381375
(const xmlChar *) info, NULL, NULL, 0,
382376
"%s: %s\n", errmsg, info);
383377
}
@@ -1122,7 +1116,11 @@ xmlSwitchInputEncodingName(xmlParserCtxtPtr ctxt, xmlParserInputPtr input,
11221116
return(-1);
11231117

11241118
res = xmlOpenCharEncodingHandler(encoding, /* output */ 0, &handler);
1125-
if (res != 0) {
1119+
if (res == XML_ERR_UNSUPPORTED_ENCODING) {
1120+
xmlWarningMsg(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
1121+
"Unsupported encoding: %s\n", BAD_CAST encoding, NULL);
1122+
return(-1);
1123+
} else if (res != XML_ERR_OK) {
11261124
xmlFatalErr(ctxt, res, encoding);
11271125
return(-1);
11281126
}
@@ -1383,7 +1381,28 @@ void
13831381
xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) {
13841382
if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) &&
13851383
((ctxt->options & XML_PARSE_IGNORE_ENC) == 0)) {
1386-
xmlSwitchEncodingName(ctxt, (const char *) encoding);
1384+
xmlCharEncodingHandlerPtr handler;
1385+
int res;
1386+
1387+
/*
1388+
* xmlSwitchInputEncodingName treats unsupported encodings as
1389+
* warnings, but we want it to be an error in an encoding
1390+
* declaration.
1391+
*/
1392+
res = xmlOpenCharEncodingHandler((const char *) encoding,
1393+
/* output */ 0, &handler);
1394+
if (res != XML_ERR_OK) {
1395+
xmlFatalErr(ctxt, res, (const char *) encoding);
1396+
xmlFree(encoding);
1397+
return;
1398+
}
1399+
1400+
res = xmlSwitchInputEncoding(ctxt, ctxt->input, handler);
1401+
if (res != XML_ERR_OK) {
1402+
xmlFree(encoding);
1403+
return;
1404+
}
1405+
13871406
ctxt->input->flags |= XML_INPUT_USES_ENC_DECL;
13881407
} else if (ctxt->input->flags & XML_INPUT_AUTO_ENCODING) {
13891408
static const char *allowedUTF8[] = {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
./test/errors/unsupported-encoding.xml:1: parser error : Unsupported encoding: unsupported-encoding
2+
<?xml version="1.0" encoding="unsupported-encoding"?>
3+
^
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
./test/errors/unsupported-encoding.xml:1: parser error : Unsupported encoding: unsupported-encoding
2+
<?xml version="1.0" encoding="unsupported-encoding"?>
3+
^
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
./test/errors/unsupported-encoding.xml:1: parser error : Unsupported encoding: unsupported-encoding
2+
<?xml version="1.0" encoding="unsupported-encoding"?>
3+
^
4+
./test/errors/unsupported-encoding.xml : failed to parse
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="unsupported-encoding"?>
2+
<doc/>

0 commit comments

Comments
 (0)