1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-12-25 23:21:26 +03:00

parser: More fixes to push parser error handling

This commit is contained in:
Nick Wellnhofer 2023-08-29 20:06:43 +02:00
parent bbd918b2e7
commit 53050b1dd8
21 changed files with 73 additions and 20 deletions

View File

@ -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))) {

View File

@ -0,0 +1,3 @@
./test/errors/empty.xml:1: parser error : Document is empty
^

View File

@ -0,0 +1,3 @@
./test/errors/empty.xml:1: parser error : Document is empty
^

View File

@ -0,0 +1,4 @@
./test/errors/empty.xml:1: parser error : Document is empty
^
./test/errors/empty.xml : failed to parse

View File

@ -0,0 +1,3 @@
./test/errors/extra-content.xml:1: parser error : Extra content at the end of the document
<d/>x
^

View File

@ -0,0 +1,3 @@
./test/errors/extra-content.xml:1: parser error : Extra content at the end of the document
<d/>x
^

View File

@ -0,0 +1,4 @@
./test/errors/extra-content.xml:1: parser error : Extra content at the end of the document
<d/>x
^
./test/errors/extra-content.xml : failed to parse

View File

@ -0,0 +1,3 @@
./test/errors/invalid-start-tag-1.xml:1: parser error : Start tag expected, '<' not found
x
^

View File

@ -0,0 +1,3 @@
./test/errors/invalid-start-tag-1.xml:1: parser error : Start tag expected, '<' not found
x
^

View File

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

View File

@ -0,0 +1,3 @@
./test/errors/invalid-start-tag-2.xml:1: parser error : StartTag: invalid element name
<
^

View File

@ -0,0 +1,3 @@
./test/errors/invalid-start-tag-2.xml:1: parser error : StartTag: invalid element name
<
^

View File

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

View File

@ -0,0 +1,3 @@
./test/errors/unclosed-element.xml:2: parser error : Premature end of data in tag d line 1
^

View File

@ -0,0 +1,3 @@
./test/errors/unclosed-element.xml:2: parser error : Premature end of data in tag d line 1
^

View File

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

0
test/errors/empty.xml Normal file
View File

View File

@ -0,0 +1 @@
<d/>x

View File

@ -0,0 +1 @@
x

View File

@ -0,0 +1 @@
<

View File

@ -0,0 +1 @@
<d>