From b8e4b2a49b366b48d98c041a206ef4376358b584 Mon Sep 17 00:00:00 2001 From: Erik Boasson Date: Sat, 4 May 2019 15:21:52 +0800 Subject: [PATCH] don't modify input string in XML parser The XML parser has two modes: it can parse a file or parse a caller-owned string. In the former case, it owns its buffer and shifts the contents to make room for more data read in from the file. This shifting may not happen when parsing a string. Signed-off-by: Erik Boasson --- src/ddsrt/src/xmlparser.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/ddsrt/src/xmlparser.c b/src/ddsrt/src/xmlparser.c index 7a7e0cb..e22bf09 100644 --- a/src/ddsrt/src/xmlparser.c +++ b/src/ddsrt/src/xmlparser.c @@ -179,22 +179,22 @@ static int make_chars_available (struct ddsrt_xmlp_state *st, size_t nmin) return 1; } /* ensure buffer space is available */ - if (pos + nmin > st->cbufmax) { - memmove (st->cbuf, st->cbuf + pos, st->cbufn - pos); - st->cbufn -= pos; - st->cbufp -= pos; - if (st->cbufmark != NOMARKER) { - st->cbufmark -= pos; - } - } - /* buffer is owned by caller if fp = NULL, and by us if fp != NULL */ - if (st->cbufp + st->cbufmax < nmin && st->fp != NULL) { - st->cbufmax = st->cbufp + nmin; - st->cbuf = ddsrt_realloc (st->cbuf, st->cbufmax); - } - /* try to refill buffer if a backing file is present; eof (or end-of-string) is - reached when this doesn't add any bytes to the buffer */ if (st->fp != NULL) { + if (pos + nmin > st->cbufmax) { + memmove (st->cbuf, st->cbuf + pos, st->cbufn - pos); + st->cbufn -= pos; + st->cbufp -= pos; + if (st->cbufmark != NOMARKER) { + st->cbufmark -= pos; + } + } + /* buffer is owned by caller if fp = NULL, and by us if fp != NULL */ + if (st->cbufp + st->cbufmax < nmin) { + st->cbufmax = st->cbufp + nmin; + st->cbuf = ddsrt_realloc (st->cbuf, st->cbufmax); + } + /* try to refill buffer if a backing file is present; eof (or end-of-string) is + reached when this doesn't add any bytes to the buffer */ n = fread (st->cbuf + st->cbufn, 1, st->cbufmax - st->cbufn, st->fp); st->cbufn += n; }