Skip to content

Added overflow handling during conversion from float to int for Linea… #20977

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

Merged
merged 1 commit into from
Nov 11, 2021

Conversation

JulieBar
Copy link
Contributor

…rFilter

Related issue #18224

force_builders=Custom
buildworker:Custom=linux-4
build_image:Custom=ubuntu-cuda:18.04

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@JulieBar JulieBar linked an issue Oct 29, 2021 that may be closed by this pull request
4 tasks
@JulieBar JulieBar added the category: gpu/cuda (contrib) OpenCV 4.0+: moved to opencv_contrib label Oct 29, 2021
@JulieBar
Copy link
Contributor Author

@YashasSamaga Could you please take a look at it?

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to prepare regression test first.
Current fix attempt doesn't look valid.

@@ -95,6 +96,12 @@ namespace cv { namespace cuda { namespace device

const int x1 = __float2int_rd(x);
const int y1 = __float2int_rd(y);
if (x1 <= NPP_MIN_32S || x1 >= NPP_MAX_32S || y1 <= NPP_MIN_32S || y1 >= NPP_MAX_32S)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x < 0
y < 0
x >= src.width
y >= src.height

Need to take border value somehow (not sure if it is properly defined for remap).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src is an instance of the class BorderReader which decided how to handle values out of scope by itself depending on the type of border. Values greater than height and width are completely valid, we don't have to clamp them here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note, that condition checks for x1,y1 which have "int" type, instead of the original x,y which can underflow/overflow/NaN/Inf "int" range (return value from __float2int_rd is not something expected).

@@ -95,6 +96,12 @@ namespace cv { namespace cuda { namespace device

const int x1 = __float2int_rd(x);
const int y1 = __float2int_rd(y);
if (x1 <= NPP_MIN_32S || x1 >= NPP_MAX_32S || y1 <= NPP_MIN_32S || y1 >= NPP_MAX_32S)
{
elem_type src_reg = src(y1, x1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src(y1, x1)

Doesn't look as valid access anyway.

Copy link
Contributor Author

@JulieBar JulieBar Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is valid because it would be handled by BorderReader class.

Existed code is totally correct for any values except that it's tried to add something to already MAX_INT value. Here:

const int x2 = x1 + 1;
const int y2 = y1 + 1;

That's the only thing which we have to fix. And that's what I did.

@JulieBar
Copy link
Contributor Author

JulieBar commented Nov 1, 2021

It makes sense to prepare regression test first. Current fix attempt doesn't look valid.

No problem, I'll add a test. The fix is valid, please take a look at the comments above.

Copy link
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 👍

@opencv-pushbot opencv-pushbot merged commit 827ff80 into opencv:4.x Nov 11, 2021
@JulieBar JulieBar deleted the remap_overflow branch November 21, 2021 01:09
@alalek alalek mentioned this pull request Dec 30, 2021
@alalek alalek mentioned this pull request Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: gpu/cuda (contrib) OpenCV 4.0+: moved to opencv_contrib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cuda::remap - illegal memory access
4 participants