1From c14cac8bbabdfbcd23b45dcb0901f1bd951159a4 Mon Sep 17 00:00:00 2001 2From: David Kilzer <ddkilzer@apple.com> 3Date: Wed, 25 May 2022 18:13:07 -0700 4Subject: [PATCH 295/300] xmlBufAvail() should return length without including 5 a byte for NUL terminator 6 7* buf.c: 8(xmlBufAvail): 9- Return the number of bytes available in the buffer, but do not 10 include a byte for the NUL terminator so that it is reserved. 11 12* encoding.c: 13(xmlCharEncFirstLineInput): 14(xmlCharEncInput): 15(xmlCharEncOutput): 16* xmlIO.c: 17(xmlOutputBufferWriteEscape): 18- Remove code that subtracts 1 from the return value of 19 xmlBufAvail(). It was implemented inconsistently anyway. 20 21Reference:https://github.com/GNOME/libxml2/commit/c14cac8bbabdfbcd23b45dcb0901f1bd951159a4 22Conflict:NA 23 24--- 25 buf.c | 9 +++++---- 26 encoding.c | 14 ++++---------- 27 xmlIO.c | 2 +- 28 3 files changed, 10 insertions(+), 15 deletions(-) 29 30diff --git a/buf.c b/buf.c 31index d341750..f896826 100644 32--- a/buf.c 33+++ b/buf.c 34@@ -689,10 +689,11 @@ xmlBufUse(const xmlBufPtr buf) 35 * @buf: the buffer 36 * 37 * Function to find how much free space is allocated but not 38- * used in the buffer. It does not account for the terminating zero 39- * usually needed 40+ * used in the buffer. It reserves one byte for the NUL 41+ * terminator character that is usually needed, so there is 42+ * no need to subtract 1 from the result anymore. 43 * 44- * Returns the amount or 0 if none or an error occurred 45+ * Returns the amount, or 0 if none or if an error occurred. 46 */ 47 48 size_t 49@@ -702,7 +703,7 @@ xmlBufAvail(const xmlBufPtr buf) 50 return 0; 51 CHECK_COMPAT(buf) 52 53- return(buf->size - buf->use); 54+ return((buf->size > buf->use) ? (buf->size - buf->use - 1) : 0); 55 } 56 57 /** 58diff --git a/encoding.c b/encoding.c 59index c14c9ff..8ce407f 100644 60--- a/encoding.c 61+++ b/encoding.c 62@@ -2177,7 +2177,7 @@ xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len) 63 toconv = xmlBufUse(in); 64 if (toconv == 0) 65 return (0); 66- written = xmlBufAvail(out) - 1; /* count '\0' */ 67+ written = xmlBufAvail(out); 68 /* 69 * echo '<?xml version="1.0" encoding="UCS4"?>' | wc -c => 38 70 * 45 chars should be sufficient to reach the end of the encoding 71@@ -2195,7 +2195,7 @@ xmlCharEncFirstLineInput(xmlParserInputBufferPtr input, int len) 72 } 73 if (toconv * 2 >= written) { 74 xmlBufGrow(out, toconv * 2); 75- written = xmlBufAvail(out) - 1; 76+ written = xmlBufAvail(out); 77 } 78 if (written > 360) 79 written = 360; 80@@ -2287,13 +2287,9 @@ xmlCharEncInput(xmlParserInputBufferPtr input, int flush) 81 if ((toconv > 64 * 1024) && (flush == 0)) 82 toconv = 64 * 1024; 83 written = xmlBufAvail(out); 84- if (written > 0) 85- written--; /* count '\0' */ 86 if (toconv * 2 >= written) { 87 xmlBufGrow(out, toconv * 2); 88 written = xmlBufAvail(out); 89- if (written > 0) 90- written--; /* count '\0' */ 91 } 92 if ((written > 128 * 1024) && (flush == 0)) 93 written = 128 * 1024; 94@@ -2475,8 +2471,6 @@ xmlCharEncOutput(xmlOutputBufferPtr output, int init) 95 retry: 96 97 written = xmlBufAvail(out); 98- if (written > 0) 99- written--; /* count '\0' */ 100 101 /* 102 * First specific handling of the initialization call 103@@ -2505,7 +2499,7 @@ retry: 104 toconv = 64 * 1024; 105 if (toconv * 4 >= written) { 106 xmlBufGrow(out, toconv * 4); 107- written = xmlBufAvail(out) - 1; 108+ written = xmlBufAvail(out); 109 } 110 if (written > 256 * 1024) 111 written = 256 * 1024; 112@@ -2580,7 +2574,7 @@ retry: 113 "&#%d;", cur); 114 xmlBufShrink(in, len); 115 xmlBufGrow(out, charrefLen * 4); 116- c_out = xmlBufAvail(out) - 1; 117+ c_out = xmlBufAvail(out); 118 c_in = charrefLen; 119 ret = xmlEncOutputChunk(output->encoder, xmlBufEnd(out), &c_out, 120 charref, &c_in); 121diff --git a/xmlIO.c b/xmlIO.c 122index 007144c..3f5307f 100644 123--- a/xmlIO.c 124+++ b/xmlIO.c 125@@ -3560,7 +3560,7 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str, 126 * how many bytes to consume and how many bytes to store. 127 */ 128 cons = len; 129- chunk = xmlBufAvail(out->buffer) - 1; 130+ chunk = xmlBufAvail(out->buffer); 131 132 /* 133 * make sure we have enough room to save first, if this is 134-- 1352.27.0 136 137