From 53050b1dd8277b7990f98482638d8f5dbf30a11b Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Tue, 29 Aug 2023 20:06:43 +0200 Subject: [PATCH] parser: More fixes to push parser error handling --- parser.c | 39 +++++++++++------------ result/errors/empty.xml.ent | 3 ++ result/errors/empty.xml.err | 3 ++ result/errors/empty.xml.str | 4 +++ result/errors/extra-content.xml.ent | 3 ++ result/errors/extra-content.xml.err | 3 ++ result/errors/extra-content.xml.str | 4 +++ result/errors/invalid-start-tag-1.xml.ent | 3 ++ result/errors/invalid-start-tag-1.xml.err | 3 ++ result/errors/invalid-start-tag-1.xml.str | 4 +++ result/errors/invalid-start-tag-2.xml.ent | 3 ++ result/errors/invalid-start-tag-2.xml.err | 3 ++ result/errors/invalid-start-tag-2.xml.str | 4 +++ result/errors/unclosed-element.xml.ent | 3 ++ result/errors/unclosed-element.xml.err | 3 ++ result/errors/unclosed-element.xml.str | 4 +++ test/errors/empty.xml | 0 test/errors/extra-content.xml | 1 + test/errors/invalid-start-tag-1.xml | 1 + test/errors/invalid-start-tag-2.xml | 1 + test/errors/unclosed-element.xml | 1 + 21 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 result/errors/empty.xml.ent create mode 100644 result/errors/empty.xml.err create mode 100644 result/errors/empty.xml.str create mode 100644 result/errors/extra-content.xml.ent create mode 100644 result/errors/extra-content.xml.err create mode 100644 result/errors/extra-content.xml.str create mode 100644 result/errors/invalid-start-tag-1.xml.ent create mode 100644 result/errors/invalid-start-tag-1.xml.err create mode 100644 result/errors/invalid-start-tag-1.xml.str create mode 100644 result/errors/invalid-start-tag-2.xml.ent create mode 100644 result/errors/invalid-start-tag-2.xml.err create mode 100644 result/errors/invalid-start-tag-2.xml.str create mode 100644 result/errors/unclosed-element.xml.ent create mode 100644 result/errors/unclosed-element.xml.err create mode 100644 result/errors/unclosed-element.xml.str create mode 100644 test/errors/empty.xml create mode 100644 test/errors/extra-content.xml create mode 100644 test/errors/invalid-start-tag-1.xml create mode 100644 test/errors/invalid-start-tag-2.xml create mode 100644 test/errors/unclosed-element.xml diff --git a/parser.c b/parser.c index 57e03fc0..d7635482 100644 --- a/parser.c +++ b/parser.c @@ -10955,7 +10955,7 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { /* * Very first chars read from the document flow. */ - if (avail < 4) + if ((!terminate) && (avail < 4)) goto done; /* @@ -10973,23 +10973,12 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { break; case XML_PARSER_XML_DECL: - if (avail < 2) + if ((!terminate) && (avail < 2)) goto done; cur = ctxt->input->cur[0]; next = ctxt->input->cur[1]; - if (cur == 0) { - if ((ctxt->sax) && (ctxt->sax->setDocumentLocator)) - ctxt->sax->setDocumentLocator(ctxt->userData, - &xmlDefaultSAXLocator); - xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); - xmlHaltParser(ctxt); - if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) - ctxt->sax->endDocument(ctxt->userData); - goto done; - } if ((cur == '<') && (next == '?')) { /* PI or XML decl */ - if (avail < 5) goto done; if ((!terminate) && (!xmlParseLookupString(ctxt, 2, "?>", 2))) goto done; @@ -11034,11 +11023,12 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { int line = ctxt->input->line; int nsNr = ctxt->nsNr; - if (avail < 2) + if ((!terminate) && (avail < 2)) goto done; cur = ctxt->input->cur[0]; if (cur != '<') { - xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); + xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, + "Start tag expected, '<' not found"); xmlHaltParser(ctxt); if ((ctxt->sax) && (ctxt->sax->endDocument != NULL)) ctxt->sax->endDocument(ctxt->userData); @@ -11213,8 +11203,6 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) { break; } case XML_PARSER_END_TAG: - if (avail < 2) - goto done; if ((!terminate) && (!xmlParseLookupChar(ctxt, '>'))) goto done; if (ctxt->sax2) { @@ -11532,9 +11520,20 @@ xmlParseChunk(xmlParserCtxtPtr ctxt, const char *chunk, int size, /* * Check for termination */ - if (ctxt->input->cur < ctxt->input->end) { - if (ctxt->errNo == XML_ERR_OK) - xmlFatalErr(ctxt, XML_ERR_DOCUMENT_END, NULL); + if ((ctxt->instate != XML_PARSER_EOF) && + (ctxt->instate != XML_PARSER_EPILOG)) { + if (ctxt->nameNr > 0) { + const xmlChar *name = ctxt->nameTab[ctxt->nameNr - 1]; + int line = ctxt->pushTab[ctxt->nameNr - 1].line; + xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_TAG_NOT_FINISHED, + "Premature end of data in tag %s line %d\n", + name, line, NULL); + } else if (ctxt->instate == XML_PARSER_START) { + xmlFatalErr(ctxt, XML_ERR_DOCUMENT_EMPTY, NULL); + } else { + xmlFatalErrMsg(ctxt, XML_ERR_DOCUMENT_EMPTY, + "Start tag expected, '<' not found\n"); + } } else if ((ctxt->input->buf != NULL) && (ctxt->input->buf->encoder != NULL) && (!xmlBufIsEmpty(ctxt->input->buf->raw))) { diff --git a/result/errors/empty.xml.ent b/result/errors/empty.xml.ent new file mode 100644 index 00000000..922ca6ff --- /dev/null +++ b/result/errors/empty.xml.ent @@ -0,0 +1,3 @@ +./test/errors/empty.xml:1: parser error : Document is empty + +^ diff --git a/result/errors/empty.xml.err b/result/errors/empty.xml.err new file mode 100644 index 00000000..922ca6ff --- /dev/null +++ b/result/errors/empty.xml.err @@ -0,0 +1,3 @@ +./test/errors/empty.xml:1: parser error : Document is empty + +^ diff --git a/result/errors/empty.xml.str b/result/errors/empty.xml.str new file mode 100644 index 00000000..37ef1772 --- /dev/null +++ b/result/errors/empty.xml.str @@ -0,0 +1,4 @@ +./test/errors/empty.xml:1: parser error : Document is empty + +^ +./test/errors/empty.xml : failed to parse diff --git a/result/errors/extra-content.xml.ent b/result/errors/extra-content.xml.ent new file mode 100644 index 00000000..7fedc442 --- /dev/null +++ b/result/errors/extra-content.xml.ent @@ -0,0 +1,3 @@ +./test/errors/extra-content.xml:1: parser error : Extra content at the end of the document +x + ^ diff --git a/result/errors/extra-content.xml.err b/result/errors/extra-content.xml.err new file mode 100644 index 00000000..7fedc442 --- /dev/null +++ b/result/errors/extra-content.xml.err @@ -0,0 +1,3 @@ +./test/errors/extra-content.xml:1: parser error : Extra content at the end of the document +x + ^ diff --git a/result/errors/extra-content.xml.str b/result/errors/extra-content.xml.str new file mode 100644 index 00000000..50d8b48e --- /dev/null +++ b/result/errors/extra-content.xml.str @@ -0,0 +1,4 @@ +./test/errors/extra-content.xml:1: parser error : Extra content at the end of the document +x + ^ +./test/errors/extra-content.xml : failed to parse diff --git a/result/errors/invalid-start-tag-1.xml.ent b/result/errors/invalid-start-tag-1.xml.ent new file mode 100644 index 00000000..e48002f2 --- /dev/null +++ b/result/errors/invalid-start-tag-1.xml.ent @@ -0,0 +1,3 @@ +./test/errors/invalid-start-tag-1.xml:1: parser error : Start tag expected, '<' not found +x +^ diff --git a/result/errors/invalid-start-tag-1.xml.err b/result/errors/invalid-start-tag-1.xml.err new file mode 100644 index 00000000..e48002f2 --- /dev/null +++ b/result/errors/invalid-start-tag-1.xml.err @@ -0,0 +1,3 @@ +./test/errors/invalid-start-tag-1.xml:1: parser error : Start tag expected, '<' not found +x +^ diff --git a/result/errors/invalid-start-tag-1.xml.str b/result/errors/invalid-start-tag-1.xml.str new file mode 100644 index 00000000..bbb49cb6 --- /dev/null +++ b/result/errors/invalid-start-tag-1.xml.str @@ -0,0 +1,4 @@ +./test/errors/invalid-start-tag-1.xml:1: parser error : Start tag expected, '<' not found +x +^ +./test/errors/invalid-start-tag-1.xml : failed to parse diff --git a/result/errors/invalid-start-tag-2.xml.ent b/result/errors/invalid-start-tag-2.xml.ent new file mode 100644 index 00000000..a71398d3 --- /dev/null +++ b/result/errors/invalid-start-tag-2.xml.ent @@ -0,0 +1,3 @@ +./test/errors/invalid-start-tag-2.xml:1: parser error : StartTag: invalid element name +< + ^ diff --git a/result/errors/invalid-start-tag-2.xml.err b/result/errors/invalid-start-tag-2.xml.err new file mode 100644 index 00000000..a71398d3 --- /dev/null +++ b/result/errors/invalid-start-tag-2.xml.err @@ -0,0 +1,3 @@ +./test/errors/invalid-start-tag-2.xml:1: parser error : StartTag: invalid element name +< + ^ diff --git a/result/errors/invalid-start-tag-2.xml.str b/result/errors/invalid-start-tag-2.xml.str new file mode 100644 index 00000000..da03e41c --- /dev/null +++ b/result/errors/invalid-start-tag-2.xml.str @@ -0,0 +1,4 @@ +./test/errors/invalid-start-tag-2.xml:1: parser error : StartTag: invalid element name +< + ^ +./test/errors/invalid-start-tag-2.xml : failed to parse diff --git a/result/errors/unclosed-element.xml.ent b/result/errors/unclosed-element.xml.ent new file mode 100644 index 00000000..14020556 --- /dev/null +++ b/result/errors/unclosed-element.xml.ent @@ -0,0 +1,3 @@ +./test/errors/unclosed-element.xml:2: parser error : Premature end of data in tag d line 1 + +^ diff --git a/result/errors/unclosed-element.xml.err b/result/errors/unclosed-element.xml.err new file mode 100644 index 00000000..14020556 --- /dev/null +++ b/result/errors/unclosed-element.xml.err @@ -0,0 +1,3 @@ +./test/errors/unclosed-element.xml:2: parser error : Premature end of data in tag d line 1 + +^ diff --git a/result/errors/unclosed-element.xml.str b/result/errors/unclosed-element.xml.str new file mode 100644 index 00000000..9d7f2e74 --- /dev/null +++ b/result/errors/unclosed-element.xml.str @@ -0,0 +1,4 @@ +./test/errors/unclosed-element.xml:2: parser error : Premature end of data in tag d line 1 + +^ +./test/errors/unclosed-element.xml : failed to parse diff --git a/test/errors/empty.xml b/test/errors/empty.xml new file mode 100644 index 00000000..e69de29b diff --git a/test/errors/extra-content.xml b/test/errors/extra-content.xml new file mode 100644 index 00000000..e2ad673c --- /dev/null +++ b/test/errors/extra-content.xml @@ -0,0 +1 @@ +x diff --git a/test/errors/invalid-start-tag-1.xml b/test/errors/invalid-start-tag-1.xml new file mode 100644 index 00000000..587be6b4 --- /dev/null +++ b/test/errors/invalid-start-tag-1.xml @@ -0,0 +1 @@ +x diff --git a/test/errors/invalid-start-tag-2.xml b/test/errors/invalid-start-tag-2.xml new file mode 100644 index 00000000..93184183 --- /dev/null +++ b/test/errors/invalid-start-tag-2.xml @@ -0,0 +1 @@ +< diff --git a/test/errors/unclosed-element.xml b/test/errors/unclosed-element.xml new file mode 100644 index 00000000..9ba700df --- /dev/null +++ b/test/errors/unclosed-element.xml @@ -0,0 +1 @@ +