Repository navigation
Conversation
Handle invalid and empty rectangles consistently in both implementations. Prepared with AI assistance.
|
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:
|
|
The following is an English translation and light polish of my own Chinese explanation, prepared with AI assistance for this reply:
|
|
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.
|
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:
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
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:
AI Assistance Disclosure
The following quoted statement was added by the AI assistant to disclose the scope of AI involvement in this PR.