Repository navigation
Fix logical error and memory tracking in PNG image output - #107537
Merged
alexey-milovidov merged 9 commits intoJun 16, 2026
Merged
alexey-milovidov merged 9 commits into
alexey-milovidov merged 9 commits into
Conversation
libpng caps each image dimension at its per-dimension user limit (1000000 by default) and reports a larger value through an error callback that we surfaced as a `LOGICAL_ERROR`, which aborts in debug builds. Our own pre-validation only rejected values above the 31-bit PNG range, so any dimension between the libpng limit and that range reached `png_set_IHDR` and tripped it. Validate width and height against the libpng limit up front and throw `BAD_ARGUMENTS` naming the `output_format_image_width` / `output_format_image_height` settings, before allocating the buffer or invoking libpng. The check at serializer construction covers every output mode (raw PNG, iTerm, Kitty, Sixel). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The Sixel, iTerm, and Kitty paths allocated their scratch buffers (the per-band color map, the encoded PNG, and its base64) through standard containers. Those use the non-throwing memory tracker, so a single large allocation overshoots `max_memory_usage` and completes instead of being rejected. Switch the Sixel scratch buffers and the encoded-PNG buffer to memory-tracked containers that throw when the limit is reached, and stream the base64 for iTerm/Kitty directly into the output buffer rather than materializing the whole encoded string plus a redundant copy of the PNG. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 of 21 tasks
Contributor
|
Workflow [PR], commit [bb1d4b2] Summary: ✅
AI ReviewSummaryThis PR fixes oversized Findings
Final VerdictStatus: Minimum required action: restore |
Replace the hand-rolled base64 routine with `Base64WriteBuffer`, a new streaming buffer in `src/IO` built on libbase64. It encodes into the underlying buffer without materializing the whole base64 string, and unlike the compression write buffers it does not finalize the underlying buffer, so callers can keep writing raw bytes after it (the terminal control sequences that wrap the payload). The iTerm path streams the whole payload through it; the Kitty path encodes each protocol chunk with libbase64 directly, since its framing interleaves control sequences between fixed-size base64 chunks. This lowers the iTerm/Kitty peak memory by ~40% on large images, removing a redundant copy of the PNG and the fully materialized base64 string. `ch_contrib::base64` is now linked to `clickhouse_common_io`, and the PNG output format is gated behind `USE_LIBPNG && USE_BASE64`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The native protocol formats the result on the client, so the test did not exercise the server-side PNG encoder. Drive the queries over HTTP so the server produces the image. The reference output is unchanged. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The PNG output format requires libpng and base64. The fast-test build enables libpng but not base64, so the format is unavailable there and the tests produce empty output. Tag them `no-fasttest`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The Sixel encoder's per-band scratch buffer must be charged to the memory tracker so it respects `max_memory_usage`. Over HTTP (server-side encoding) a 1000000-wide one-row image makes that ~24 MB buffer the dominant allocation; under a 10 MB limit it must be rejected with `MEMORY_LIMIT_EXCEEDED`, and a generous limit still produces a Sixel stream. Before the fix the allocation silently exceeded the limit. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Encode through a fixed local buffer and forward with `out.write` instead of writing base64 straight into the wrapped buffer. The previous code assumed the wrapped `WriteBuffer` could expose at least four contiguous bytes; a buffer that never does (for example a `WriteBufferFromPointer` over one to three bytes) would have spun forever. `out.write` copes with any destination buffer size. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
clang-tidy's member-init check flags `state` as uninitialized in the constructor, even though `base64_stream_encode_init` sets it in the body. Zero-initialize the member to satisfy the check. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Extend the test with an iTerm case: the iTerm and Kitty modes build the whole encoded PNG in a memory-tracked buffer, so an incompressible image under a tight limit (with the response streamed, so HTTP buffering does not mask it) must be rejected. The existing case only covered the Sixel per-band scratch buffer. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Contributor
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 68/73 (93.15%) | Lost baseline coverage: none · Uncovered code |
Merged
via the queue into
ClickHouse:master
with commit Jun 16, 2026
3926f40
327 of 328 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two fixes to the PNG image output format, both found from a query that aborted in debug builds.
Oversized image dimensions raised a
LOGICAL_ERROR. libpng caps each dimension at 1000000 (its per-dimension user limit) and reports a larger value through an error callback we surfaced as a logical error, which aborts in debug builds. Our pre-validation only rejected values above the 31-bit PNG range, so a dimension between the libpng limit and that range slipped through topng_set_IHDR. Width and height are now validated up front against the libpng limit and rejected with a clearBAD_ARGUMENTSnaming theoutput_format_image_width/output_format_image_heightsettings. The check covers every output mode (raw PNG, iTerm, Kitty, Sixel).The terminal-output paths allocated scratch buffers (the per-band color map, the encoded PNG, and its base64) through standard containers, which use the non-throwing memory tracker and so overshoot
max_memory_usageinstead of failing. Those buffers now use memory-tracked containers, and the base64 for iTerm/Kitty is streamed straight into the output buffer rather than materializing the whole encoded string plus a redundant copy of the PNG.Changelog category (leave one):
🤖 Generated with Claude Code
Version info
26.6.1.896