Skip to content

Commit a84ed47

Browse files
Merge pull request ClickHouse#91292 from ClickHouse/backport/25.8/90909
Backport ClickHouse#90909 to 25.8: Fix base58Decode function
2 parents 3eaa565 + 33bea7a commit a84ed47

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/Functions/FunctionBase58Conversion.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ struct Base58DecodeTraits
2929
template <typename Col>
3030
static size_t getBufferSize(Col const & src_column)
3131
{
32-
auto const string_length = src_column.getChars().size();
33-
/// decoded size is at most length of encoded (every 8 bytes becomes at most 6 bytes)
34-
return (string_length * 6 + 7) / 8;
32+
/// According to the RFC https://datatracker.ietf.org/doc/html/draft-msporny-base58-03
33+
/// base58 doesn't have a clean bitsequence-to-character mapping like base32 or base64.
34+
/// Instead, it uses division by 58 and modulo operations on big integers.
35+
/// In addition all the leading zeros are converted to "1"s as is.
36+
/// Thus, if we decode the can have at most same amount of bytes as a result.
37+
/// Example:
38+
/// "11111" (5 chars) -> b'\x00\x00\x00\x00\x00' (5 bytes)
39+
return src_column.getChars().size();
3540
}
3641

3742
static std::optional<size_t> perform(std::string_view src, UInt8 * dst)

tests/queries/0_stateless/03733_base58_decode_bug.reference

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT base58Decode(materialize('11111111111111')) FROM numbers(100000) FORMAT Null;

0 commit comments

Comments
 (0)