Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d98ed08

Browse files
committed
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 378d73e commit d98ed08

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

contrib/pgcrypto/pgp-armor.c

+1-1
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

+1-1
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

+1-1
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)