Skip to content

Make base58 respect query cancellation and limit input size to 10 KB - #106428

Merged
alexey-milovidov merged 8 commits into
masterfrom
base58-respect-cancellation-and-size-limit
Jun 9, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
base58-respect-cancellation-and-size-limit

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 4, 2026 •

Copy link
Copy Markdown
Member

The generic (variable-length) base58 encoder and decoder use a big-integer base conversion whose cost is quadratic in the input length. They ran to completion without ever checking for cancellation, so a single large value could keep a thread busy for many minutes, ignoring max_execution_time (which is only checked between pipeline blocks, not inside one executeImpl call on a single value) and ignoring KILL QUERY.

This was repeatedly hit by the server-side AST fuzzer in the Stress test (experimental, serverfuzz, ...) checks, which runs fuzzed queries synchronously in the connection handler's finish-callback: a fuzzed base58Encode/base58Decode over an enlarged argument blocked the handler, the query stayed in the process list with is_cancelled = 1, and the run failed with Hung check failed, possible deadlock found across many architectures.

Two complementary fixes:

  • The conversion now checks for cancellation periodically inside its inner loop (via the query's QueryStatus::checkTimeLimit, like FunctionSleep), so many in-limit values in a single block still respect the time limit and KILL QUERY.
  • base58 inputs are limited to 10 KB by default. base58 is meant for short data (keys, hashes, addresses), so larger inputs are almost certainly a mistake or fuzzer input. base58Encode/base58Decode throw TOO_LARGE_STRING_SIZE; tryBase58Decode keeps its "empty string on error" contract. base32/base64 are linear and unaffected.

The size limit is configurable through the new setting function_base58_max_input_size (default 10000; 0 disables the limit). Because this rejects inputs that previous releases accepted, the change is registered in SettingsChangesHistory for 26.6 with a compatibility value of 0, so compatibility with versions before 26.6 restores the previous unlimited behavior on upgraded clusters.

CI report: https://play.clickhouse.com/play?user=play (checks table, Stress test (experimental, serverfuzz, ...), test_name = 'Hung check failed, possible deadlock found' on master).

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Functions base58Encode, base58Decode and tryBase58Decode now respect max_execution_time and query cancellation on large inputs, and reject inputs larger than 10 KB instead of running for a very long time. The limit is configurable via the new setting function_base58_max_input_size (0 disables it).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

Version info

  • Merged into: 26.6.1.525

alexey-milovidov and others added 2 commits June 4, 2026 08:55
The generic (variable-length) `base58` encoder and decoder in `Common/Base58.cpp`
use a big-integer base conversion whose cost is quadratic in the input length. They
ran to completion without ever checking for cancellation, so a single large value
could keep a thread busy for many minutes, ignoring `max_execution_time` (which is
only checked between pipeline blocks, not inside one `executeImpl` call on a single
huge value) and ignoring `KILL QUERY`.

The server-side AST fuzzer (the `Stress test (experimental, serverfuzz, ...)` checks)
repeatedly hit this: it runs fuzzed queries synchronously in the connection handler's
finish-callback, and a fuzzed `base58Encode`/`base58Decode` over an enlarged argument
blocked the handler indefinitely. The query stayed in the process list with
`is_cancelled = 1` and the run failed with "Hung check failed, possible deadlock found"
across many architectures.

`encodeBase58` and `decodeBase58` now accept an optional cancellation callback that is
invoked periodically inside their inner loops; `FunctionBaseXXConversion` builds it from
the query's `QueryStatus` (`checkTimeLimit`, like `FunctionSleep`), so a long-running
`base58` conversion now throws `TIMEOUT_EXCEEDED` or `QUERY_WAS_CANCELLED` promptly. The
fixed-size 32/64-byte paths and `base32`/`base64` are linear and unaffected.

CI report: https://play.clickhouse.com/play?user=play#U0VMRUNUIGNoZWNrX25hbWUsIHRlc3RfbmFtZSwgcmVwb3J0X3VybApGUk9NIGNoZWNrcwpXSEVSRSAxCiAgICBBTkQgY2hlY2tfc3RhcnRfdGltZSA+PSBub3coKSAtIElOVEVSVkFMIDI0IEhPVVIKICAgIEFORCAoaGVhZF9yZWYgPSAnbWFzdGVyJyBBTkQgc3RhcnRzV2l0aChoZWFkX3JlcG8sICdDbGlja0hvdXNlLycpKQ==

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
In addition to the cancellation check, reject base58 inputs larger than 10 KB. The generic
base58 conversion is quadratic in the input length, and base58 is meant for short data (keys,
hashes, addresses), so an oversized value is almost certainly a mistake or fuzzer input rather
than a legitimate use. A hard limit gives an immediate, clear error instead of a long-running
conversion.

The limit is enforced in `FunctionBaseXXConversion` via a `Traits::max_input_size` constant
(`base32`/`base64` are linear and set it to 0, i.e. unlimited). `base58Encode`/`base58Decode`
throw `TOO_LARGE_STRING_SIZE`; `tryBase58Decode` keeps its "empty string on error" contract and
returns an empty string for oversized input. The 10 KB limit leaves a large margin over the
32/64-byte values base58 is normally used for.

The cancellation check from the previous commit is still needed: many in-limit values in a single
block (one `executeImpl` call) can still exceed `max_execution_time`, which is otherwise only
checked between pipeline blocks.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b4b24d1]

Summary: ✅


AI Review

Summary

This PR makes the generic base58 conversions respect cancellation/time limits and adds a compatibility-gated per-value input size limit via function_base58_max_input_size. I found no unresolved correctness, compatibility, performance, or documentation issues in the current code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 4, 2026
alexey-milovidov and others added 2 commits June 5, 2026 14:47
The hard 10 KB cap on `base58Encode`, `base58Decode` and `tryBase58Decode`
rejected inputs that previous releases accepted, applied unconditionally on
upgraded clusters. Gate it behind the new setting `function_base58_max_input_size`
(default 10000, `0` disables the limit) so the protective behavior is on by
default while `compatibility` with versions before 26.6 restores the previous
unlimited behavior.

The limit is only enforced for the quadratic `base58` conversion (the only
function whose compile-time `Traits::max_input_size` is non-zero); the linear
`base32`/`base64` functions ignore the setting. The error message now reports
the effective runtime limit.

Also renumber the test to 04319 to avoid a number collision introduced by the
merge with master.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Updated:

  • Merged master (was 403 behind) — clean, no conflicts.
  • Addressed the backward-compatibility review: the 10 KB base58 input limit is now configurable via the new setting function_base58_max_input_size (default 10000, 0 disables it) instead of a hard compile-time cap. Registered in SettingsChangesHistory for 26.6 with compatibility value 0, so compatibility with pre-26.6 versions restores the previous unlimited behavior. The linear base32/base64 functions ignore the setting.
  • Renumbered the test 04306 → 04319 (collided with two master tests at 04306) and extended it to cover the configurable limit, the disabled-limit (compatibility) path, and that base64 ignores the setting.

Built clean and verified locally: default cap rejects >10 KB with TOO_LARGE_STRING_SIZE; function_base58_max_input_size = 0 removes the limit; a smaller value (10) rejects 100-byte inputs; base64 round-trips a 100 KB string regardless of the setting; the in-block cancellation still throws TIMEOUT_EXCEEDED under max_execution_time = 1. Test 04319 passes with and without random settings.

The remaining 16 red checks are unrelated arm performance-comparison noise (re2_regex_caching, rand, array_reduce, coalesce, logical_functions_small, parallel_hash_join_various, set_index, entropy) — none touch base58.

Comment thread src/Common/Base58.cpp Resolved
alexey-milovidov and others added 2 commits June 7, 2026 10:28
…tion size check

Use `base32` instead of `base64` in the test, since `base64` is not built in
the Fast test image (the test only needs a linear, unaffected function).

Two review fixes in the conversion itself:
- The cancellation check in `encodeBase58`/`decodeBase58` is now driven by the
  accumulated inner-loop work, not the number of outer iterations. The inner
  loop is `idx` iterations and `idx` grows with the input, so checking once per
  fixed number of source bytes could leave billions of inner-loop iterations
  between checks when the size limit is disabled or raised. Worst-case latency
  is now one inner-loop pass.
- `QueryStatus::checkTimeLimit` returns `false` (instead of throwing) for the
  `break` overflow mode. A single scalar value has no meaningful partial
  result, so the callback now turns a `break` request into a hard
  `TIMEOUT_EXCEEDED` stop.

And one in the function layer: the input-size limit is now checked before
allocating the result buffer. `getBufferSize` reserves roughly the whole input
(about twice for encode), so an oversized block could surface as
`MEMORY_LIMIT_EXCEEDED` before `TOO_LARGE_STRING_SIZE`. Input lengths are
validated up front; the throwing decoder rejects oversized rows before any
allocation, and the empty-string decoder sizes the buffer from the in-limit
rows alone.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Pushed d27565a (after merging master):

  • Fast test failure (04319_base58_respects_time_limit): the test used base64Encode/base64Decode, which are not built in the Fast test image. Switched that case to base32, which is also linear and unaffected by the limit and is available in all builds.
  • Review (cancellation granularity): encodeBase58/decodeBase58 now drive the cancellation check by accumulated inner-loop work instead of the number of outer iterations, so the time limit and KILL QUERY stay responsive even when the size limit is disabled.
  • Review (break overflow mode): a false from checkTimeLimit (break-mode timeout) is now turned into a hard TIMEOUT_EXCEEDED, since a scalar value has no meaningful partial result.
  • Review (allocation before rejection): input sizes are validated before getBufferSize/resize, so an oversized block is rejected with TOO_LARGE_STRING_SIZE before the large allocation (the empty-string decoder sizes its buffer from the in-limit rows only).

Built locally and verified all 04319 cases (size limit, configurable/disabled limit, TIMEOUT_EXCEEDED inside a single block, base32 round-trip), plus the existing 02337_base58 / 04027_base58_optimized / 03714_base32_base58_short_string regression tests pass.

alexey-milovidov and others added 2 commits June 8, 2026 18:13
The arm_tidy build failed with `cppcoreguidelines-init-variables`:
`variable 'max_result_size' is not initialized` in
`FunctionBaseXXConversion.h`. Initialize it to 0.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106428&sha=d27565af2eb16335626e0d89804858a1685f7bb8&name_0=PR&name_1=Build%20%28arm_tidy%29

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 166/188 (88.30%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Jun 9, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 9, 2026
Merged via the queue into master with commit 5f285cd Jun 9, 2026
166 checks passed
@alexey-milovidov
alexey-milovidov deleted the base58-respect-cancellation-and-size-limit branch June 9, 2026 01:10
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants