Skip to content

Pull Request: Fix copy_within Bounds Validation Logic - #3129

Closed
liuyinchu wants to merge 1 commit into
image-rs:mainfrom
liuyinchu:fix/copy-within-bounds-3006
Closed

liuyinchu wants to merge 1 commit into
image-rs:mainfrom
liuyinchu:fix/copy-within-bounds-3006

Conversation

@liuyinchu

Copy link
Copy Markdown

Pull Request: Fix copy_within Bounds Validation Logic

Overview

This commit (b463e54) optimizes and unifies the bounds validation and empty rectangle handling logic for the copy_within method in the image crate, while significantly expanding unit test coverage.

The Problem

Previously, the copy_within method in both buffer.rs and generic_image.rs used custom inline math logic (e.g., based on dx.max(sx)) to verify whether the source and destination rectangles were out of bounds. This implementation had several limitations:

  1. Boundary Vulnerabilities and Inconsistencies: When faced with extreme dimensions (such as u32::MAX), unexpected offsets, or special width/height combinations, hand-written inline checks lacked rigor, potentially leading to out-of-bounds access or inconsistent behavior.
  2. Imperfect Handling of Empty Rectangles: There was a lack of clear, uniform handling for empty rectangles with a width or height of 0.

The Fix

This change unifies and hardens the validation logic through the following approaches:

  • Reusing Standard Bounds Validation: Switched to the test_in_bounds_of method to perform strict bounds checks on both the source rectangle (source) and the destination rectangle (constructed via Rect { x, y, ..source }). If either returns an error (is_err()), false is returned immediately.

  • Explicit Empty Rectangle Handling: After passing the bounds check, rectangle dimensions are checked first:

    Rust

    if width == 0 || height == 0 {
        return true;
    }
    
    

    This ensures that empty rectangles return successfully (without modifying the image) when they are within the image boundaries, making the behavior more consistent and expected.

Verification & Testing

To ensure correctness and prevent future regressions, this commit introduces a new, comprehensive test suite in tests/copy_within.rs (adding 107 lines of test code) covering the following validation scenarios:

  1. Out-of-Bounds and Limit Tests (check_copy_within_bounds):
    • Performs boundary stress tests across various offsets and ultra-large width/height combinations up to u32::MAX to ensure out-of-bounds requests are correctly intercepted.
  2. Empty Rectangle and Zero-Dimension Tests:
    • Validates the behavior of rectangles with a width or height of 0 (such as (0, 0), (0, 2), (2, 0), or even empty rectangles containing u32::MAX) under both valid and invalid offsets.
  3. Multi-Type Implementation Verification:
    • Conducts dual testing on both standard buffers (GrayImage / buffer_copy_within_bounds) and generic image implementations wrapped via sub-images (sub_image / generic_copy_within_bounds), ensuring completely consistent behavior between the primary and generic implementations.

AI Assistance Disclosure

The following quoted statement was added by the AI assistant to disclose the scope of AI involvement in this PR.

This PR includes substantial AI-generated contributions: the code changes and added regression tests were generated and prepared with AI assistance. The review description above was supplied by the contributor and is reproduced unchanged at their explicit request. The AI assistant also helped inspect the relevant source code and contribution policy and prepare this disclosure and draft PR.

Handle invalid and empty rectangles consistently in both implementations.

Prepared with AI assistance.
@liuyinchu
liuyinchu marked this pull request as ready for review October 19, 2026 15:01
@fintelia

Copy link
Copy Markdown
Contributor

Are you sure you wrote that PR description yourself as claimed? It is both way longer than necessary but also really hard to pin down exactly what the main purpose of the PR is. It says "optimize and unify" in one place, alleges "vulnerabilities and inconsistencies" in another, and the goes back to "unifies and hardens".

Also this paragraph exactly the way weaker LLMs responded when asked to find problems with some code. They make a bunch of vague allegations (what does "unexpected offsets" or "special width/height combinations" mean?) and then hedge by saying that could "potentially" lead to one of several bad things:

Boundary Vulnerabilities and Inconsistencies: When faced with extreme dimensions (such as u32::MAX), unexpected offsets, or special width/height combinations, hand-written inline checks lacked rigor, potentially leading to out-of-bounds access or inconsistent behavior.

@liuyinchu

Copy link
Copy Markdown
Author

@fintelia

The following is an English translation and light polish of my own Chinese explanation, prepared with AI assistance for this reply:

Thank you for the feedback. I wrote the original PR description myself in Chinese and used translation software to produce the English version. My technical writing is limited, and the resulting description was awkward and imprecise. My point was that I did not have a model such as Codex compose that original explanation for me; I would have expected its wording to be more polished than mine. I understand that this does not address your substantive criticism of the description, and that the vague security claims need clarification.

@liuyinchu

Copy link
Copy Markdown
Author

Additional context: I asked an LLM to produce the following example of a more precise explanation, based on my description and the actual patch. This is deliberately LLM-written supplementary material, not a claim that I personally wrote the text below. It is included to clarify the concrete behavior changes rather than leave the discussion at a comparison of writing style.

This patch changes copy_within boundary and empty-rectangle behavior in ImageBuffer and the default GenericImage implementation. Previously, both implementations asserted that source and destination origins were strictly inside the image. For example, on a 4×4 image, attempting to copy a 1×1 source rectangle starting at x = 4 triggers that assertion. The patch instead validates both complete rectangles with Rect::test_in_bounds_of and returns false for invalid bounds.

After both bounds checks succeed, a rectangle with zero width or height returns true without modifying pixels. Empty rectangles still have to satisfy the bounds checks; zero area does not make an out-of-bounds request valid. Regression tests cover the buffer and subimage/default implementations, including invalid origins, extreme dimensions, and empty rectangles or images.

This is a correction to panic/return-value behavior and empty-copy handling, using shared bounds validation. The diff does not establish out-of-bounds memory access in the old implementation or a performance improvement. The original description's vulnerability and optimization wording was therefore too strong.

@liuyinchu liuyinchu closed this Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants