Skip to content

Bound decoded-byte growth before reserving a claimed chunk size #2403

Closed

Description

@Mavrick-F

Reproduced allocation before rejection

On main at e0cf82a8, zarr::Array::decode_bytes reserves 64 MiB from a chunk-size claim before discovering that the decoded payload is only eight bytes.

The input is a valid 17-byte Zstandard frame containing eight zero bytes. With a uint8 Zarr array/chunk shape of eight, it decodes successfully. With the same frame and a shape of 67,108,864, it is correctly refused (a chunk holds 8 bytes, not 67108864), but only after unzstd reserves want.min(64 << 20).

In separate native probe processes, /proc/self/status VmPeak stayed at 5,972 KiB for the valid eight-byte case and rose to 71,512 KiB for the mismatched metadata. This measures peak virtual address space, not resident memory. The direct reservation is also visible in raster/zarr.rs::unzstd.

Scope I plan to take

Use one internal bounded decoded-byte reader for Zarr's Zstandard chunks (including its blosc Zstandard streams) and GeoTIFF LERC's Zstandard wrapper. Both already use Read decoders and output-byte limits; they currently accumulate their output separately.

The shared helper would:

  • grow from decoded bytes received, rather than reserve an untrusted expected length;
  • check each growth before allocation, use fallible reservation, and keep capacity within the caller's existing byte limit;
  • detect an extra decoded byte and return an error instead of accepting a truncated result;
  • propagate read failures and Cancel, with checks between bounded reads.

The global format ceilings are unchanged. Zarr codec stages use the final chunk size plus derivable fixed overhead (including four bytes per CRC32C step) where possible. A variable-size intermediate whose length cannot be derived from the final values uses the existing chunk-file cap: 1 GiB natively and 256 MiB in wasm. This raises the former final-size-based allowance for such intermediates; it is an explicit compatibility and memory tradeoff, not a claim that every per-stage ceiling is unchanged. Format-specific validation stays where it belongs: dimension products, codec headers, LZ4 expansion bounds and chunk layout checks are not replaced by a generic byte cap. No dependency, serialized field, command parameter or UI change is planned. This stays clear of #2298's time-window work, #2389's spreadsheet work and #2032's coordinate migration.

Tests will cover the small-frame/large-claim regression, valid decoded output, exact and exceeded limits, read failure and cancellation. Existing Zarr/blosc and LERC tests must keep passing, followed by the normal full and wasm gates. This is part of making allocation limits shared checks, rather than adding another independent reader-specific cap.

Activity

  1. self-assigned this
    on Nov 18, 2026
  2. Mavrick-F commented on Nov 18, 2026

    @Mavrick-F
    MemberAuthor

    Scope correction discovered during #2407 review: the original statement that no memory ceiling changes was too broad. It applies to the global format caps, but not to each Zarr codec stage.

    A compressed intermediate can exceed the final chunk byte count. A native probe decoded a valid 64-byte chunk through a gzip member with a 1 MiB optional filename; the member was 1,048,601 bytes. No fixed framing margin derived from 64 bytes covers such a header. Conversely, an invalid member can grow an intermediate buffer before the next codec rejects it; an 8 MiB probe confirmed that higher peak. This tradeoff is described in the PR and the measured-input reply.

    The reviewed implementation derives tight bounds through size-preserving stages and CRC32C, and uses the existing chunk-file cap (1 GiB native, 256 MiB wasm) only for unknown variable-size intermediates. Zstandard output grows from received bytes with fallible allocation and cancellation checks. Non-Zstandard cancellation granularity is documented. The shared bounded reader remains unchanged. The issue scope is corrected to state this policy explicitly; no dependency, serialized field or command interface changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions