1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-09-08 13:44:13 +03:00

xmlBufAvail() should return length without including a byte for NUL terminator

* buf.c:
(xmlBufAvail):
- Return the number of bytes available in the buffer, but do not
  include a byte for the NUL terminator so that it is reserved.

* encoding.c:
(xmlCharEncFirstLineInput):
(xmlCharEncInput):
(xmlCharEncOutput):
* xmlIO.c:
(xmlOutputBufferWriteEscape):
- Remove code that subtracts 1 from the return value of
  xmlBufAvail().  It was implemented inconsistently anyway.
This commit is contained in:
David Kilzer
2022-05-25 18:13:07 -07:00
parent fe9f76ebb8
commit c14cac8bba
3 changed files with 10 additions and 15 deletions

9
buf.c
View File

@@ -645,10 +645,11 @@ xmlBufUse(const xmlBufPtr buf)
* @buf: the buffer * @buf: the buffer
* *
* Function to find how much free space is allocated but not * Function to find how much free space is allocated but not
* used in the buffer. It does not account for the terminating zero * used in the buffer. It reserves one byte for the NUL
* usually needed * terminator character that is usually needed, so there is
* no need to subtract 1 from the result anymore.
* *
* Returns the amount or 0 if none or an error occurred * Returns the amount, or 0 if none or if an error occurred.
*/ */
size_t size_t
@@ -658,7 +659,7 @@ xmlBufAvail(const xmlBufPtr buf)
return 0; return 0;
CHECK_COMPAT(buf) CHECK_COMPAT(buf)
return(buf->size - buf->use); return((buf->size > buf->use) ? (buf->size - buf->use - 1) : 0);
} }
/** /**

View File

@@ -2197,7 +2197,7 @@ xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len)
toconv = xmlBufUse(in); toconv = xmlBufUse(in);
if (toconv == 0) if (toconv == 0)
return (0); return (0);
written = xmlBufAvail(out) - 1; /* count '\0' */ written = xmlBufAvail(out);
/* /*
* echo '<?xml version="1.0" encoding="UCS4"?>' | wc -c => 38 * echo '<?xml version="1.0" encoding="UCS4"?>' | wc -c => 38
* 45 chars should be sufficient to reach the end of the encoding * 45 chars should be sufficient to reach the end of the encoding
@@ -2215,7 +2215,7 @@ xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len)
} }
if (toconv * 2 >= written) { if (toconv * 2 >= written) {
xmlBufGrow(out, toconv * 2); xmlBufGrow(out, toconv * 2);
written = xmlBufAvail(out) - 1; written = xmlBufAvail(out);
} }
if (written > 360) if (written > 360)
written = 360; written = 360;
@@ -2307,13 +2307,9 @@ xmlCharEncInput(xmlParserInputBufferPtr input, int flush)
if ((toconv > 64 * 1024) && (flush == 0)) if ((toconv > 64 * 1024) && (flush == 0))
toconv = 64 * 1024; toconv = 64 * 1024;
written = xmlBufAvail(out); written = xmlBufAvail(out);
if (written > 0)
written--; /* count '\0' */
if (toconv * 2 >= written) { if (toconv * 2 >= written) {
xmlBufGrow(out, toconv * 2); xmlBufGrow(out, toconv * 2);
written = xmlBufAvail(out); written = xmlBufAvail(out);
if (written > 0)
written--; /* count '\0' */
} }
if ((written > 128 * 1024) && (flush == 0)) if ((written > 128 * 1024) && (flush == 0))
written = 128 * 1024; written = 128 * 1024;
@@ -2495,8 +2491,6 @@ xmlCharEncOutput(xmlOutputBufferPtr output, int init)
retry: retry:
written = xmlBufAvail(out); written = xmlBufAvail(out);
if (written > 0)
written--; /* count '\0' */
/* /*
* First specific handling of the initialization call * First specific handling of the initialization call
@@ -2525,7 +2519,7 @@ retry:
toconv = 64 * 1024; toconv = 64 * 1024;
if (toconv * 4 >= written) { if (toconv * 4 >= written) {
xmlBufGrow(out, toconv * 4); xmlBufGrow(out, toconv * 4);
written = xmlBufAvail(out) - 1; written = xmlBufAvail(out);
} }
if (written > 256 * 1024) if (written > 256 * 1024)
written = 256 * 1024; written = 256 * 1024;
@@ -2600,7 +2594,7 @@ retry:
"&#%d;", cur); "&#%d;", cur);
xmlBufShrink(in, len); xmlBufShrink(in, len);
xmlBufGrow(out, charrefLen * 4); xmlBufGrow(out, charrefLen * 4);
c_out = xmlBufAvail(out) - 1; c_out = xmlBufAvail(out);
c_in = charrefLen; c_in = charrefLen;
ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out, ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
charref, &c_in); charref, &c_in);

View File

@@ -3547,7 +3547,7 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
* how many bytes to consume and how many bytes to store. * how many bytes to consume and how many bytes to store.
*/ */
cons = len; cons = len;
chunk = xmlBufAvail(out->buffer) - 1; chunk = xmlBufAvail(out->buffer);
/* /*
* make sure we have enough room to save first, if this is * make sure we have enough room to save first, if this is