From c6044e6a109aa3a01eb9daadb13c6256b80e853c Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Sun, 4 Oct 2026 11:47:59 +0200 Subject: [PATCH] base64: properly compute the decoded size upper bound First, the correct length for decoding should be computed with /4*3 , not the other way round as when encoding. But also, without padding, a string such as "Zg" is valid base64 (here, it's the character 'f', padded base64 would be "Zg=="). decodedLenUpperBound() returned 0 instead of 1 in that example. Correctly fix the computation of the upper bound. --- lib/std/crypto/codecs/base64_hex_ct.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/crypto/codecs/base64_hex_ct.zig b/lib/std/crypto/codecs/base64_hex_ct.zig index 9e639d84bd09cb3cd8852b34d2c1e7667ec698ea..3da08ceb25989747d6984d2f4017b8009543a865 100644 --- a/lib/std/crypto/codecs/base64_hex_ct.zig +++ b/lib/std/crypto/codecs/base64_hex_ct.zig @@ -261,7 +261,7 @@ pub const base64 = struct { /// Returns the maximum possible decoded size for a given input length after skipping ignored characters. pub fn decodedLenUpperBound(b64_len: usize) usize { - return b64_len / 3 * 4; + return b64_len / 4 * 3 + (b64_len % 4) * 3 / 4; } }; -- 2.54.0