1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-04-24 18:50:07 +03:00

parser: Stop calling xmlParserInputGrow

Introduce xmlParserGrow which takes a parser context to simplify error
handling.
This commit is contained in:
Nick Wellnhofer 2023-03-12 16:47:15 +01:00
parent f6fddb78a5
commit 3eb6bf0386
4 changed files with 79 additions and 74 deletions

View File

@ -299,7 +299,7 @@ htmlNodeInfoPop(htmlParserCtxtPtr ctxt)
#define GROW if ((ctxt->progressive == 0) && \
(ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
xmlParserInputGrow(ctxt->input, INPUT_CHUNK)
xmlParserGrow(ctxt)
#define SKIP_BLANKS htmlSkipBlankChars(ctxt)
@ -473,7 +473,7 @@ htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
if ((c & 0x40) == 0)
goto encoding_error;
if (cur[1] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if ((cur[1] & 0xc0) != 0x80)
@ -481,14 +481,14 @@ htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
if ((c & 0xe0) == 0xe0) {
if (cur[2] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if ((cur[2] & 0xc0) != 0x80)
goto encoding_error;
if ((c & 0xf0) == 0xf0) {
if (cur[3] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if (((c & 0xf8) != 0xf0) ||
@ -588,17 +588,12 @@ htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
int res = 0;
while (IS_BLANK_CH(*(ctxt->input->cur))) {
if ((*ctxt->input->cur == 0) &&
(xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
xmlPopInput(ctxt);
} else {
if (*(ctxt->input->cur) == '\n') {
ctxt->input->line++; ctxt->input->col = 1;
} else ctxt->input->col++;
ctxt->input->cur++;
if (*ctxt->input->cur == 0)
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
}
xmlParserGrow(ctxt);
if (res < INT_MAX)
res++;
}

View File

@ -23,5 +23,7 @@ XML_HIDDEN void
__xmlErrEncoding(xmlParserCtxtPtr ctxt, xmlParserErrors xmlerr,
const char *msg, const xmlChar *str1,
const xmlChar *str2) LIBXML_ATTR_FORMAT(3,0);
XML_HIDDEN int
xmlParserGrow(xmlParserCtxtPtr ctxt);
#endif /* XML_PARSER_H_PRIVATE__ */

View File

@ -2049,7 +2049,7 @@ static int spacePop(xmlParserCtxtPtr ctxt) {
#define SKIP(val) do { \
ctxt->input->cur += (val),ctxt->input->col+=(val); \
if (*ctxt->input->cur == 0) \
xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
xmlParserGrow(ctxt); \
} while (0)
#define SKIPL(val) do { \
@ -2061,7 +2061,7 @@ static int spacePop(xmlParserCtxtPtr ctxt) {
ctxt->input->cur++; \
} \
if (*ctxt->input->cur == 0) \
xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
xmlParserGrow(ctxt); \
} while (0)
#define SHRINK if ((ctxt->progressive == 0) && \
@ -2075,54 +2075,12 @@ static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
((ctxt->input->buf->encoder) || (ctxt->input->buf->readcallback)))
xmlParserInputShrink(ctxt->input);
if (*ctxt->input->cur == 0)
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
}
#define GROW if ((ctxt->progressive == 0) && \
(ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
xmlGROW (ctxt);
static void xmlGROW (xmlParserCtxtPtr ctxt) {
xmlParserInputPtr in = ctxt->input;
xmlParserInputBufferPtr buf = in->buf;
ptrdiff_t curEnd = in->end - in->cur;
ptrdiff_t curBase = in->cur - in->base;
int ret;
if (buf == NULL)
return;
/* Don't grow memory buffers. */
if ((buf->encoder == NULL) && (buf->readcallback == NULL))
return;
if (((curEnd > XML_MAX_LOOKUP_LIMIT) ||
(curBase > XML_MAX_LOOKUP_LIMIT)) &&
((ctxt->options & XML_PARSE_HUGE) == 0)) {
xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup");
xmlHaltParser(ctxt);
return;
}
if (xmlBufUse(buf->buffer) > (unsigned int) curBase + INPUT_CHUNK)
return;
ret = xmlParserInputBufferGrow(buf, INPUT_CHUNK);
in->base = xmlBufContent(buf->buffer);
if (in->base == NULL) {
in->base = BAD_CAST "";
in->cur = in->base;
in->end = in->base;
xmlErrMemory(ctxt, NULL);
return;
}
in->cur = in->base + curBase;
in->end = xmlBufEnd(buf->buffer);
/* TODO: Get error code from xmlParserInputBufferGrow */
if (ret < 0)
xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Growing input buffer");
}
xmlParserGrow(ctxt);
#define SKIP_BLANKS xmlSkipBlankChars(ctxt)
@ -2132,7 +2090,7 @@ static void xmlGROW (xmlParserCtxtPtr ctxt) {
ctxt->input->col++; \
ctxt->input->cur++; \
if (*ctxt->input->cur == 0) \
xmlParserInputGrow(ctxt->input, INPUT_CHUNK); \
xmlParserGrow(ctxt); \
}
#define NEXTL(l) do { \
@ -2185,7 +2143,7 @@ xmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
res++;
if (*cur == 0) {
ctxt->input->cur = cur;
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
}
@ -2279,7 +2237,7 @@ xmlPopInput(xmlParserCtxtPtr ctxt) {
input->entity->flags &= ~XML_ENT_EXPANDING;
xmlFreeInputStream(input);
if (*ctxt->input->cur == 0)
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
return(CUR);
}

View File

@ -272,6 +272,57 @@ xmlParserInputRead(xmlParserInputPtr in ATTRIBUTE_UNUSED, int len ATTRIBUTE_UNUS
return(-1);
}
/**
* xmlParserGrow:
* @ctxt: an XML parser context
*/
int
xmlParserGrow(xmlParserCtxtPtr ctxt) {
xmlParserInputPtr in = ctxt->input;
xmlParserInputBufferPtr buf = in->buf;
ptrdiff_t curEnd = in->end - in->cur;
ptrdiff_t curBase = in->cur - in->base;
int ret;
if (buf == NULL)
return(0);
/* Don't grow memory buffers. */
if ((buf->encoder == NULL) && (buf->readcallback == NULL))
return(0);
if (((curEnd > XML_MAX_LOOKUP_LIMIT) ||
(curBase > XML_MAX_LOOKUP_LIMIT)) &&
((ctxt->options & XML_PARSE_HUGE) == 0)) {
xmlErrInternal(ctxt, "Huge input lookup", NULL);
ctxt->instate = XML_PARSER_EOF;
return(-1);
}
if (curEnd >= INPUT_CHUNK)
return(0);
ret = xmlParserInputBufferGrow(buf, INPUT_CHUNK);
in->base = xmlBufContent(buf->buffer);
if (in->base == NULL) {
in->base = BAD_CAST "";
in->cur = in->base;
in->end = in->base;
xmlErrMemory(ctxt, NULL);
return(-1);
}
in->cur = in->base + curBase;
in->end = xmlBufEnd(buf->buffer);
/* TODO: Get error code from xmlParserInputBufferGrow */
if (ret < 0) {
xmlErrInternal(ctxt, "Growing input buffer", NULL);
ctxt->instate = XML_PARSER_EOF;
}
return(ret);
}
/**
* xmlParserInputGrow:
* @in: an XML parser input
@ -406,8 +457,7 @@ xmlNextChar(xmlParserCtxtPtr ctxt)
return;
}
if ((ctxt->input->cur >= ctxt->input->end) &&
(xmlParserInputGrow(ctxt->input, INPUT_CHUNK) <= 0)) {
if ((ctxt->input->cur >= ctxt->input->end) && (xmlParserGrow(ctxt) <= 0)) {
return;
}
@ -444,7 +494,7 @@ xmlNextChar(xmlParserCtxtPtr ctxt)
if (c == 0xC0)
goto encoding_error;
if (cur[1] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if ((cur[1] & 0xc0) != 0x80)
@ -453,14 +503,14 @@ xmlNextChar(xmlParserCtxtPtr ctxt)
unsigned int val;
if (cur[2] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if ((cur[2] & 0xc0) != 0x80)
goto encoding_error;
if ((c & 0xf0) == 0xf0) {
if (cur[3] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if (((c & 0xf8) != 0xf0) ||
@ -506,7 +556,7 @@ xmlNextChar(xmlParserCtxtPtr ctxt)
ctxt->input->cur++;
}
if (*ctxt->input->cur == 0)
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
return;
encoding_error:
/*
@ -585,21 +635,21 @@ xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
if (((c & 0x40) == 0) || (c == 0xC0))
goto encoding_error;
if (cur[1] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if ((cur[1] & 0xc0) != 0x80)
goto encoding_error;
if ((c & 0xe0) == 0xe0) {
if (cur[2] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if ((cur[2] & 0xc0) != 0x80)
goto encoding_error;
if ((c & 0xf0) == 0xf0) {
if (cur[3] == 0) {
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
cur = ctxt->input->cur;
}
if (((c & 0xf8) != 0xf0) ||
@ -639,7 +689,7 @@ xmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
/* 1-byte code */
*len = 1;
if (*ctxt->input->cur == 0)
xmlParserInputGrow(ctxt->input, INPUT_CHUNK);
xmlParserGrow(ctxt);
if ((*ctxt->input->cur == 0) &&
(ctxt->input->end > ctxt->input->cur)) {
xmlErrEncodingInt(ctxt, XML_ERR_INVALID_CHAR,