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

parser: Stop calling xmlParserInputShrink

Introduce xmlParserShrink which takes a parser context to simplify error
handling.
This commit is contained in:
Nick Wellnhofer 2023-03-13 17:51:13 +01:00
parent 483793940c
commit 2099441f32
4 changed files with 62 additions and 12 deletions

View File

@ -295,7 +295,7 @@ htmlNodeInfoPop(htmlParserCtxtPtr ctxt)
#define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
(ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
xmlParserInputShrink(ctxt->input)
xmlParserShrink(ctxt)
#define GROW if ((ctxt->progressive == 0) && \
(ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \

View File

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

View File

@ -2065,16 +2065,7 @@ static int spacePop(xmlParserCtxtPtr ctxt) {
#define SHRINK if ((ctxt->progressive == 0) && \
(ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
(ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
xmlSHRINK (ctxt);
static void xmlSHRINK (xmlParserCtxtPtr ctxt) {
/* Don't shrink memory buffers. */
if ((ctxt->input->buf) &&
((ctxt->input->buf->encoder) || (ctxt->input->buf->readcallback)))
xmlParserInputShrink(ctxt->input);
if (*ctxt->input->cur == 0)
xmlParserGrow(ctxt);
}
xmlParserShrink(ctxt);
#define GROW if ((ctxt->progressive == 0) && \
(ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
@ -11484,7 +11475,7 @@ xmlParseTryOrFinish(xmlParserCtxtPtr ctxt, int terminate) {
if ((ctxt->input != NULL) &&
(ctxt->input->cur - ctxt->input->base > 4096)) {
xmlParserInputShrink(ctxt->input);
xmlParserShrink(ctxt);
}
while (ctxt->instate != XML_PARSER_EOF) {

View File

@ -380,6 +380,63 @@ xmlParserInputGrow(xmlParserInputPtr in, int len) {
return(ret);
}
/**
* xmlParserShrink:
* @ctxt: an XML parser context
*/
int
xmlParserShrink(xmlParserCtxtPtr ctxt) {
xmlParserInputPtr in = ctxt->input;
xmlParserInputBufferPtr buf = in->buf;
size_t used;
int ret = 0;
/* Don't shrink memory buffers. */
if ((buf == NULL) ||
((buf->encoder == NULL) && (buf->readcallback == NULL)))
return(0);
used = in->cur - in->base;
/*
* Do not shrink on large buffers whose only a tiny fraction
* was consumed
*/
if (used > INPUT_CHUNK) {
size_t res = xmlBufShrink(buf->buffer, used - LINE_LEN);
if (res > 0) {
used -= res;
if ((res > ULONG_MAX) ||
(in->consumed > ULONG_MAX - (unsigned long)res))
in->consumed = ULONG_MAX;
else
in->consumed += res;
}
}
if (xmlBufUse(buf->buffer) < INPUT_CHUNK)
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 + used;
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);
}
/**
* xmlParserInputShrink:
* @in: an XML parser input