Skip to content

Commit 7d3aece

Browse files
tglsfdcpull[bot]
authored andcommitted
Fix small overestimation of base64 encoding output length.
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 79d78bb 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
1 parent 3eb92a3 commit 7d3aece

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

contrib/pgcrypto/pgp-armor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ pg_base64_enc_len(unsigned srclen)
165165
/*
166166
* 3 bytes will be converted to 4, linefeed after 76 chars
167167
*/
168-
return (srclen + 2) * 4 / 3 + srclen / (76 * 3 / 4);
168+
return (srclen + 2) / 3 * 4 + srclen / (76 * 3 / 4);
169169
}
170170

171171
static unsigned

src/backend/utils/adt/encode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static uint64
385385
pg_base64_enc_len(const char *src, size_t srclen)
386386
{
387387
/* 3 bytes will be converted to 4, linefeed after 76 chars */
388-
return ((uint64) srclen + 2) * 4 / 3 + (uint64) srclen / (76 * 3 / 4);
388+
return ((uint64) srclen + 2) / 3 * 4 + (uint64) srclen / (76 * 3 / 4);
389389
}
390390

391391
static uint64

src/common/base64.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ int
224224
pg_b64_enc_len(int srclen)
225225
{
226226
/* 3 bytes will be converted to 4 */
227-
return (srclen + 2) * 4 / 3;
227+
return (srclen + 2) / 3 * 4;
228228
}
229229

230230
/*

0 commit comments

Comments
 (0)