Skip to content

fix(transfer): never shrink BufferedMap buffer to avoid overlap data loss - #5588

Merged
oferchen merged 1 commit into
masterfrom
fix/uts-18-h-buffer-never-shrink
Jun 11, 2026
Merged

oferchen merged 1 commit into
masterfrom
fix/uts-18-h-buffer-never-shrink

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

  • Closes the data-loss path flagged during PR fix(transfer): clamp window_len to remaining file bytes in load_window #5569 review: BufferedMap::load_window calls Vec::resize(window_size, 0) which truncates the buffer when a smaller window is requested, dropping bytes the overlap branch is about to relocate via copy_within and either panicking on the now-out-of-bounds source range or (after PR fix(transfer): turn buffered map_file out-of-range panic into Err #5566's guard) surfacing the corruption as InvalidData.
  • Matches upstream fileio.c:236 where realloc_array only grows the backing buffer and never shrinks; the new window's valid extent is bounded by window_len, so retaining extra trailing capacity is harmless.
  • Surgical 1-line behavior change: buffer.resize(window_size.max(buffer.len()), 0) plus an upstream-citing comment and a positive regression test.

Test plan

  • New positive regression test load_window_overlap_shrink_preserves_data (mirrors PR fix(transfer): clamp window_len to remaining file bytes in load_window #5569's positive-test style):
    • Opens a 5000-byte file with a 4096-byte window cap.
    • Calls map_ptr(0, 100) to fill the buffer to window_len = 4096.
    • Calls map_ptr(3000, 1000) which triggers the overlap-shrink branch (aligned_start=2048, window_size=2952 < buffer.len()=4096) and would panic on the old code's copy_within(2048..4096, 0) against a 2952-byte buffer.
    • Asserts the returned slice matches file bytes 3000..4000.
    • Asserts window_start == 2048 and window_len == 2952 (clamp to remaining file bytes).
    • Asserts the full new window via map_ptr(2048, 2952) matches file bytes 2048..5000 end-to-end, catching any silent truncation of the relocated suffix.
  • Existing window_slide_forward_reuses_overlap test continues to pass (overlap reuse without shrink is unchanged - target_len equals the existing buffer length).
  • cargo fmt --all -- --check (verified by CI fmt+clippy gate).
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings (verified by CI).
  • cargo nextest run -p transfer --all-features covers map_file::tests::load_window_overlap_shrink_preserves_data (verified by CI nextest matrix).

…loss

PR #5569 review flagged a data-loss path in `BufferedMap::load_window`:
`Vec::resize(window_size, 0)` truncates the buffer when a smaller window
is requested. Under the overlap-shrink branch (new window near EOF after
a full-size load), the bytes the overlap branch is about to relocate via
`copy_within` are dropped before the copy runs, leading to either an
out-of-bounds panic on the source range or silent corruption of the
reused suffix once PR #5566's guard converts the panic to InvalidData.

Upstream `fileio.c:236` `realloc_array` only grows the backing buffer.
Mirror that invariant by replacing `buffer.resize(window_size, 0)` with
`buffer.resize(window_size.max(buffer.len()), 0)`. The buffer becomes
monotonically non-decreasing (capped at MAX_MAP_SIZE peak); `window_len`
continues to bound the valid region so callers never see stale tail
bytes.

Picked the never-shrink fix over file-boundary-only shrink or
shrink-and-reload because it is the smallest delta, matches upstream
exactly, and has negligible memory cost in practice.
@github-actions github-actions Bot added the bug Something isn't working label Jun 10, 2026
@oferchen
oferchen merged commit 7464a51 into master Jun 11, 2026
55 of 70 checks passed
@oferchen
oferchen deleted the fix/uts-18-h-buffer-never-shrink branch June 11, 2026 03:11
oferchen added a commit that referenced this pull request Aug 24, 2026
fix(transfer): never shrink BufferedMap buffer to avoid overlap data loss
oferchen added a commit that referenced this pull request Aug 24, 2026
UTS-18.j: extend URV-1 attestation with the basis-window panic chain
discovered after the original codec-only audit cleared the file.
Document the three follow-on regressions (window_len overstatement,
buffer.resize overlap-shrink, bare slice index on misaligned chunk
requests) and the remediation PRs (#5566, #5569, #5588, #5590, #5627).

UTS-18.i: keep `compress-zlib-insert` out of the upstream-testsuite
known-failures list so the workflow's wildcard run surfaces any
regression as an UNEXPECTED FAIL. The cell already iterates
`$suitedir/*.test`, so explicit wiring is the explanatory comment in
`tools/ci/upstream_testsuite_known_failures.conf` plus the
attestation-doc cross-reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant