-
Notifications
You must be signed in to change notification settings - Fork 624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cropping API doesn't check bounds, easy to misuse #2296
Comments
SubImages seem to be kinda broken in general: #1412 |
@Shnatsel I was thinking of a simple solution, like Lines 49 to 60 in 40940d2
We can only return a SubImage if the width and height is > 0, else we return None let (x, y, width, height) = crop_dimms(image, x, y, width, height);
if width > 0 && height > 0 { return Some(SubImage::new(image, x, y, width, height);) }
return None; |
This will require changing the public API, so it's not a change that will be easy to get merged. I suggest looking at issues that do not have the API tag for your first contribution. |
Since the result is useless anyway, I don't think it would be a breaking change to return a slightly less broken result? Crop could try to stay within bounds of the image: clamp left & top coordinates to width-1 & height-1, and then clamp size to whatever is left. |
If we assign the (x, y) to (width-1, height-1), and the size left is > 0, then we will always return a single pixel as a Subimage. Isn't that wrong? Because the result is a single pixel, while there should not be any pixels in the output Subimago (i.e. the Subimage should not exist/ be defined). |
It is wrong, but the wrongness started with invalid input. A 1x1 image may be better than a crashy empty one. Alternatively, the function could itself panic on out-of-bounds coordinates. The docs currently don't say anything about error handling. |
True... Lines 62 to 78 in 40940d2
Looking at the code, we can then replace let x = cmp::min(x, iwidth-1);
let y = cmp::min(y, iheight-1); |
In
image
v0.25.2, thecrop()
andcrop_imm()
functions return an image view or an image unconditionally, even if the area to be cropped is completely out of bounds. This makes the API very easy to misuse:Demo in playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0898257582886870218d41c94506e3cb
The text was updated successfully, but these errors were encountered: