Blame SOURCES/apr-util-1.6.1-CVE-2022-25147.patch

c608ed
diff --git a/encoding/apr_base64.c b/encoding/apr_base64.c
c608ed
index 1eed153..2803106 100644
c608ed
--- a/encoding/apr_base64.c
c608ed
+++ b/encoding/apr_base64.c
c608ed
@@ -20,11 +20,20 @@
c608ed
  * ugly 'len' functions, which is quite a nasty cost.
c608ed
  */
c608ed
 
c608ed
+#undef NDEBUG /* always abort() on assert()ion failure */
c608ed
+#include <assert.h>
c608ed
+
c608ed
 #include "apr_base64.h"
c608ed
 #if APR_CHARSET_EBCDIC
c608ed
 #include "apr_xlate.h"
c608ed
 #endif				/* APR_CHARSET_EBCDIC */
c608ed
 
c608ed
+/* Above APR_BASE64_ENCODE_MAX length the encoding can't fit in an int >= 0 */
c608ed
+#define APR_BASE64_ENCODE_MAX 1610612733
c608ed
+
c608ed
+/* Above APR_BASE64_DECODE_MAX length the decoding can't fit in an int >= 0 */
c608ed
+#define APR_BASE64_DECODE_MAX 2863311524u
c608ed
+
c608ed
 /* aaaack but it's fast and const should make it shared text page. */
c608ed
 static const unsigned char pr2six[256] =
c608ed
 {
c608ed
@@ -109,7 +118,6 @@ APU_DECLARE(apr_status_t) apr_base64init_ebcdic(apr_xlate_t *to_ascii,
c608ed
 
c608ed
 APU_DECLARE(int) apr_base64_decode_len(const char *bufcoded)
c608ed
 {
c608ed
-    int nbytesdecoded;
c608ed
     register const unsigned char *bufin;
c608ed
     register apr_size_t nprbytes;
c608ed
 
c608ed
@@ -117,16 +125,16 @@ APU_DECLARE(int) apr_base64_decode_len(const char *bufcoded)
c608ed
     while (pr2six[*(bufin++)] <= 63);
c608ed
 
c608ed
     nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
c608ed
-    nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
c608ed
+    assert(nprbytes <= APR_BASE64_DECODE_MAX);
c608ed
 
c608ed
-    return nbytesdecoded + 1;
c608ed
+    return (int)(((nprbytes + 3u) / 4u) * 3u + 1u);
c608ed
 }
c608ed
 
c608ed
 APU_DECLARE(int) apr_base64_decode(char *bufplain, const char *bufcoded)
c608ed
 {
c608ed
 #if APR_CHARSET_EBCDIC
c608ed
     apr_size_t inbytes_left, outbytes_left;
c608ed
-#endif				/* APR_CHARSET_EBCDIC */
c608ed
+#endif	/* APR_CHARSET_EBCDIC */
c608ed
     int len;
c608ed
     
c608ed
     len = apr_base64_decode_binary((unsigned char *) bufplain, bufcoded);
c608ed
@@ -153,12 +161,13 @@ APU_DECLARE(int) apr_base64_decode_binary(unsigned char *bufplain,
c608ed
     bufin = (const unsigned char *) bufcoded;
c608ed
     while (pr2six[*(bufin++)] <= 63);
c608ed
     nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
c608ed
-    nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
c608ed
+    assert(nprbytes <= APR_BASE64_DECODE_MAX);
c608ed
+    nbytesdecoded = (int)(((nprbytes + 3u) / 4u) * 3u);
c608ed
 
c608ed
     bufout = (unsigned char *) bufplain;
c608ed
     bufin = (const unsigned char *) bufcoded;
c608ed
 
c608ed
-    while (nprbytes > 4) {
c608ed
+    while (nprbytes >= 4) {
c608ed
 	*(bufout++) =
c608ed
 	    (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
c608ed
 	*(bufout++) =
c608ed
@@ -178,13 +187,8 @@ APU_DECLARE(int) apr_base64_decode_binary(unsigned char *bufplain,
c608ed
 	*(bufout++) =
c608ed
 	    (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
c608ed
     }
c608ed
-    if (nprbytes > 3) {
c608ed
-	*(bufout++) =
c608ed
-	    (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
c608ed
-    }
c608ed
 
c608ed
-    nbytesdecoded -= (4 - (int)nprbytes) & 3;
c608ed
-    return nbytesdecoded;
c608ed
+    return nbytesdecoded - (int)((4u - nprbytes) & 3u);
c608ed
 }
c608ed
 
c608ed
 static const char basis_64[] =
c608ed
@@ -192,6 +196,8 @@ static const char basis_64[] =
c608ed
 
c608ed
 APU_DECLARE(int) apr_base64_encode_len(int len)
c608ed
 {
c608ed
+    assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
c608ed
+
c608ed
     return ((len + 2) / 3 * 4) + 1;
c608ed
 }
c608ed
 
c608ed
@@ -203,6 +209,8 @@ APU_DECLARE(int) apr_base64_encode(char *encoded, const char *string, int len)
c608ed
     int i;
c608ed
     char *p;
c608ed
 
c608ed
+    assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
c608ed
+
c608ed
     p = encoded;
c608ed
     for (i = 0; i < len - 2; i += 3) {
c608ed
 	*p++ = basis_64[(os_toascii[string[i]] >> 2) & 0x3F];
c608ed
@@ -227,7 +235,7 @@ APU_DECLARE(int) apr_base64_encode(char *encoded, const char *string, int len)
c608ed
     }
c608ed
 
c608ed
     *p++ = '\0';
c608ed
-    return p - encoded;
c608ed
+    return (unsigned int)(p - encoded);
c608ed
 #endif				/* APR_CHARSET_EBCDIC */
c608ed
 }
c608ed
 
c608ed
@@ -240,6 +248,8 @@ APU_DECLARE(int) apr_base64_encode_binary(char *encoded,
c608ed
     int i;
c608ed
     char *p;
c608ed
 
c608ed
+    assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
c608ed
+
c608ed
     p = encoded;
c608ed
     for (i = 0; i < len - 2; i += 3) {
c608ed
 	*p++ = basis_64[(string[i] >> 2) & 0x3F];
c608ed
@@ -264,5 +274,5 @@ APU_DECLARE(int) apr_base64_encode_binary(char *encoded,
c608ed
     }
c608ed
 
c608ed
     *p++ = '\0';
c608ed
-    return (int)(p - encoded);
c608ed
+    return (unsigned int)(p - encoded);
c608ed
 }