Error in calculating length of encoded base64 string

From: o(dot)tselebrovskiy(at)postgrespro(dot)ru
To: pgsql-hackers(at)postgresql(dot)org
Cc: Sergey Shinderuk <s(dot)shinderuk(at)postgrespro(dot)ru>
Subject: Error in calculating length of encoded base64 string
Date: 2023-06-08 07:53:28
Message-ID: f94da55286a63022150bc266afdab754@postgrespro.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Greetings, everyone!

While working on an extension I've found an error in how length of
encoded base64 string is calulated;

This error is present in 3 files across all supported versions:

/src/common/base64.c, function pg_b64_enc_len;
/src/backend/utils/adt/encode.c, function pg_base64_enc_len;
/contrib/pgcrypto/pgp-armor.c, function pg_base64_enc_len (copied from
encode.c).

In all three cases the length is calculated as follows:

(srclen + 2) * 4 / 3; (plus linefeed in latter two cases)

There's also a comment /* 3 bytes will be converted to 4 */

This formula is wrong. Let's calculate encoded length for different
starting lengths:

starting length 2: (2 + 2) * 4 / 3 = 5,
starting length 3: (3 + 2) * 4 / 3 = 6,
starting length 4: (4 + 2) * 4 / 3 = 8,
starting length 6: (6 + 2) * 4 / 3 = 10,
starting length 10: (10 + 2) * 4 / 3 = 16,

when it should be 4, 4, 8, 8, 16.

So the suggestion is to change the formula to a right one: (srclen + 2)
/ 3 * 4;

The patch is attached.

Oleg Tselebrovskiy, Postgres Pro

Attachment Content-Type Size
base64_encoded_length.patch text/x-diff 1.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message 쿼리트릭스 2023-06-08 08:51:39 [ psql - review request ] review request for \d+ tablename, \d+ indexname indenting
Previous Message Alexander Pyhalov 2023-06-08 07:39:30 Re: Partial aggregates pushdown