153aa9179Sopenharmony_ciFrom c14cac8bbabdfbcd23b45dcb0901f1bd951159a4 Mon Sep 17 00:00:00 2001
253aa9179Sopenharmony_ciFrom: David Kilzer <ddkilzer@apple.com>
353aa9179Sopenharmony_ciDate: Wed, 25 May 2022 18:13:07 -0700
453aa9179Sopenharmony_ciSubject: [PATCH 295/300] xmlBufAvail() should return length without including
553aa9179Sopenharmony_ci a byte for NUL terminator
653aa9179Sopenharmony_ci
753aa9179Sopenharmony_ci* buf.c:
853aa9179Sopenharmony_ci(xmlBufAvail):
953aa9179Sopenharmony_ci- Return the number of bytes available in the buffer, but do not
1053aa9179Sopenharmony_ci  include a byte for the NUL terminator so that it is reserved.
1153aa9179Sopenharmony_ci
1253aa9179Sopenharmony_ci* encoding.c:
1353aa9179Sopenharmony_ci(xmlCharEncFirstLineInput):
1453aa9179Sopenharmony_ci(xmlCharEncInput):
1553aa9179Sopenharmony_ci(xmlCharEncOutput):
1653aa9179Sopenharmony_ci* xmlIO.c:
1753aa9179Sopenharmony_ci(xmlOutputBufferWriteEscape):
1853aa9179Sopenharmony_ci- Remove code that subtracts 1 from the return value of
1953aa9179Sopenharmony_ci  xmlBufAvail().  It was implemented inconsistently anyway.
2053aa9179Sopenharmony_ci
2153aa9179Sopenharmony_ciReference:https://github.com/GNOME/libxml2/commit/c14cac8bbabdfbcd23b45dcb0901f1bd951159a4
2253aa9179Sopenharmony_ciConflict:NA
2353aa9179Sopenharmony_ci
2453aa9179Sopenharmony_ci---
2553aa9179Sopenharmony_ci buf.c      |  9 +++++----
2653aa9179Sopenharmony_ci encoding.c | 14 ++++----------
2753aa9179Sopenharmony_ci xmlIO.c    |  2 +-
2853aa9179Sopenharmony_ci 3 files changed, 10 insertions(+), 15 deletions(-)
2953aa9179Sopenharmony_ci
3053aa9179Sopenharmony_cidiff --git a/buf.c b/buf.c
3153aa9179Sopenharmony_ciindex d341750..f896826 100644
3253aa9179Sopenharmony_ci--- a/buf.c
3353aa9179Sopenharmony_ci+++ b/buf.c
3453aa9179Sopenharmony_ci@@ -689,10 +689,11 @@ xmlBufUse(const xmlBufPtr buf)
3553aa9179Sopenharmony_ci  * @buf:  the buffer
3653aa9179Sopenharmony_ci  *
3753aa9179Sopenharmony_ci  * Function to find how much free space is allocated but not
3853aa9179Sopenharmony_ci- * used in the buffer. It does not account for the terminating zero
3953aa9179Sopenharmony_ci- * usually needed
4053aa9179Sopenharmony_ci+ * used in the buffer. It reserves one byte for the NUL
4153aa9179Sopenharmony_ci+ * terminator character that is usually needed, so there is
4253aa9179Sopenharmony_ci+ * no need to subtract 1 from the result anymore.
4353aa9179Sopenharmony_ci  *
4453aa9179Sopenharmony_ci- * Returns the amount or 0 if none or an error occurred
4553aa9179Sopenharmony_ci+ * Returns the amount, or 0 if none or if an error occurred.
4653aa9179Sopenharmony_ci  */
4753aa9179Sopenharmony_ci 
4853aa9179Sopenharmony_ci size_t
4953aa9179Sopenharmony_ci@@ -702,7 +703,7 @@ xmlBufAvail(const xmlBufPtr buf)
5053aa9179Sopenharmony_ci         return 0;
5153aa9179Sopenharmony_ci     CHECK_COMPAT(buf)
5253aa9179Sopenharmony_ci 
5353aa9179Sopenharmony_ci-    return(buf->size - buf->use);
5453aa9179Sopenharmony_ci+    return((buf->size > buf->use) ? (buf->size - buf->use - 1) : 0);
5553aa9179Sopenharmony_ci }
5653aa9179Sopenharmony_ci 
5753aa9179Sopenharmony_ci /**
5853aa9179Sopenharmony_cidiff --git a/encoding.c b/encoding.c
5953aa9179Sopenharmony_ciindex c14c9ff..8ce407f 100644
6053aa9179Sopenharmony_ci--- a/encoding.c
6153aa9179Sopenharmony_ci+++ b/encoding.c
6253aa9179Sopenharmony_ci@@ -2177,7 +2177,7 @@ xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len)
6353aa9179Sopenharmony_ci     toconv = xmlBufUse(in);
6453aa9179Sopenharmony_ci     if (toconv == 0)
6553aa9179Sopenharmony_ci         return (0);
6653aa9179Sopenharmony_ci-    written = xmlBufAvail(out) - 1; /* count '\0' */
6753aa9179Sopenharmony_ci+    written = xmlBufAvail(out);
6853aa9179Sopenharmony_ci     /*
6953aa9179Sopenharmony_ci      * echo '<?xml version="1.0" encoding="UCS4"?>' | wc -c => 38
7053aa9179Sopenharmony_ci      * 45 chars should be sufficient to reach the end of the encoding
7153aa9179Sopenharmony_ci@@ -2195,7 +2195,7 @@ xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len)
7253aa9179Sopenharmony_ci     }
7353aa9179Sopenharmony_ci     if (toconv * 2 >= written) {
7453aa9179Sopenharmony_ci         xmlBufGrow(out, toconv * 2);
7553aa9179Sopenharmony_ci-        written = xmlBufAvail(out) - 1;
7653aa9179Sopenharmony_ci+        written = xmlBufAvail(out);
7753aa9179Sopenharmony_ci     }
7853aa9179Sopenharmony_ci     if (written > 360)
7953aa9179Sopenharmony_ci         written = 360;
8053aa9179Sopenharmony_ci@@ -2287,13 +2287,9 @@ xmlCharEncInput(xmlParserInputBufferPtr input, int flush)
8153aa9179Sopenharmony_ci     if ((toconv > 64 * 1024) && (flush == 0))
8253aa9179Sopenharmony_ci         toconv = 64 * 1024;
8353aa9179Sopenharmony_ci     written = xmlBufAvail(out);
8453aa9179Sopenharmony_ci-    if (written > 0)
8553aa9179Sopenharmony_ci-        written--; /* count '\0' */
8653aa9179Sopenharmony_ci     if (toconv * 2 >= written) {
8753aa9179Sopenharmony_ci         xmlBufGrow(out, toconv * 2);
8853aa9179Sopenharmony_ci         written = xmlBufAvail(out);
8953aa9179Sopenharmony_ci-        if (written > 0)
9053aa9179Sopenharmony_ci-            written--; /* count '\0' */
9153aa9179Sopenharmony_ci     }
9253aa9179Sopenharmony_ci     if ((written > 128 * 1024) && (flush == 0))
9353aa9179Sopenharmony_ci         written = 128 * 1024;
9453aa9179Sopenharmony_ci@@ -2475,8 +2471,6 @@ xmlCharEncOutput(xmlOutputBufferPtr output, int init)
9553aa9179Sopenharmony_ci retry:
9653aa9179Sopenharmony_ci 
9753aa9179Sopenharmony_ci     written = xmlBufAvail(out);
9853aa9179Sopenharmony_ci-    if (written > 0)
9953aa9179Sopenharmony_ci-        written--; /* count '\0' */
10053aa9179Sopenharmony_ci 
10153aa9179Sopenharmony_ci     /*
10253aa9179Sopenharmony_ci      * First specific handling of the initialization call
10353aa9179Sopenharmony_ci@@ -2505,7 +2499,7 @@ retry:
10453aa9179Sopenharmony_ci         toconv = 64 * 1024;
10553aa9179Sopenharmony_ci     if (toconv * 4 >= written) {
10653aa9179Sopenharmony_ci         xmlBufGrow(out, toconv * 4);
10753aa9179Sopenharmony_ci-        written = xmlBufAvail(out) - 1;
10853aa9179Sopenharmony_ci+        written = xmlBufAvail(out);
10953aa9179Sopenharmony_ci     }
11053aa9179Sopenharmony_ci     if (written > 256 * 1024)
11153aa9179Sopenharmony_ci         written = 256 * 1024;
11253aa9179Sopenharmony_ci@@ -2580,7 +2574,7 @@ retry:
11353aa9179Sopenharmony_ci                              "&#%d;", cur);
11453aa9179Sopenharmony_ci             xmlBufShrink(in, len);
11553aa9179Sopenharmony_ci             xmlBufGrow(out, charrefLen * 4);
11653aa9179Sopenharmony_ci-            c_out = xmlBufAvail(out) - 1;
11753aa9179Sopenharmony_ci+            c_out = xmlBufAvail(out);
11853aa9179Sopenharmony_ci             c_in = charrefLen;
11953aa9179Sopenharmony_ci             ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out,
12053aa9179Sopenharmony_ci                                     charref, &c_in);
12153aa9179Sopenharmony_cidiff --git a/xmlIO.c b/xmlIO.c
12253aa9179Sopenharmony_ciindex 007144c..3f5307f 100644
12353aa9179Sopenharmony_ci--- a/xmlIO.c
12453aa9179Sopenharmony_ci+++ b/xmlIO.c
12553aa9179Sopenharmony_ci@@ -3560,7 +3560,7 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
12653aa9179Sopenharmony_ci 	 * how many bytes to consume and how many bytes to store.
12753aa9179Sopenharmony_ci 	 */
12853aa9179Sopenharmony_ci 	cons = len;
12953aa9179Sopenharmony_ci-	chunk = xmlBufAvail(out->buffer) - 1;
13053aa9179Sopenharmony_ci+	chunk = xmlBufAvail(out->buffer);
13153aa9179Sopenharmony_ci 
13253aa9179Sopenharmony_ci         /*
13353aa9179Sopenharmony_ci 	 * make sure we have enough room to save first, if this is
13453aa9179Sopenharmony_ci-- 
13553aa9179Sopenharmony_ci2.27.0
13653aa9179Sopenharmony_ci
137