You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudberry.apache.org by re...@apache.org on 2025/01/23 14:13:26 UTC

(cloudberry) 06/16: Fix small overestimation of base64 encoding output length. (#17155)

This is an automated email from the ASF dual-hosted git repository.

reshke pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit f87b1bf6fde6c7e5d652840d8c43283f1c92cabb
Author: Wenkang Zhang <31...@qq.com>
AuthorDate: Thu Mar 7 15:04:42 2024 +0800

    Fix small overestimation of base64 encoding output length. (#17155)
    
    pg_base64_enc_len() and its clones overestimated the output
    length by up to 2 bytes, as a result of sloppy thinking about
    where to divide.  No callers require a precise estimate, so
    this has no consequences worse than palloc'ing a byte or two
    more than necessary.  We might as well get it right though.
    
    This bug is very ancient, dating to commit 79d78bb26 which
    added encode.c.  (The other instances were presumably copied
    from there.)  Still, it doesn't quite seem worth back-patching.
    
    Oleg Tselebrovskiy
    
    Discussion: https://postgr.es/m/f94da55286a63022150bc266afdab754@postgrespro.ru
    (cherry picked from commit d98ed080bb31fd3d46281127871b7886288686d9)
    
    Co-authored-by: Tom Lane <tg...@sss.pgh.pa.us>
---
 contrib/pgcrypto/pgp-armor.c   | 2 +-
 src/backend/utils/adt/encode.c | 2 +-
 src/common/base64.c            | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/contrib/pgcrypto/pgp-armor.c b/contrib/pgcrypto/pgp-armor.c
index 679779a6ac..9128756647 100644
--- a/contrib/pgcrypto/pgp-armor.c
+++ b/contrib/pgcrypto/pgp-armor.c
@@ -165,7 +165,7 @@ pg_base64_enc_len(unsigned srclen)
 	/*
 	 * 3 bytes will be converted to 4, linefeed after 76 chars
 	 */
-	return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
+	return (srclen + 2) / 3 * 4 + srclen / (76 * 3 / 4);
 }
 
 static unsigned
diff --git a/src/backend/utils/adt/encode.c b/src/backend/utils/adt/encode.c
index 6dd93f9a32..5b4e970838 100644
--- a/src/backend/utils/adt/encode.c
+++ b/src/backend/utils/adt/encode.c
@@ -375,7 +375,7 @@ static uint64
 pg_base64_enc_len(const char *src, size_t srclen)
 {
 	/* 3 bytes will be converted to 4, linefeed after 76 chars */
-	return ((uint64) srclen + 2) * 4 / 3 + (uint64) srclen / (76 * 3 / 4);
+	return ((uint64) srclen + 2) / 3 * 4 + (uint64) srclen / (76 * 3 / 4);
 }
 
 static uint64
diff --git a/src/common/base64.c b/src/common/base64.c
index b3ade2a5a5..4068a73ce9 100644
--- a/src/common/base64.c
+++ b/src/common/base64.c
@@ -224,7 +224,7 @@ int
 pg_b64_enc_len(int srclen)
 {
 	/* 3 bytes will be converted to 4 */
-	return (srclen + 2) * 4 / 3;
+	return (srclen + 2) / 3 * 4;
 }
 
 /*


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cloudberry.apache.org
For additional commands, e-mail: commits-help@cloudberry.apache.org