Skip to content

Fix integer overflow in cv::Luv2RGBinteger::process.#21112

Merged
alalek merged 2 commits intoopencv:3.4from
vrabaud:3.4_luv_overflow
Dec 1, 2021
Merged

Fix integer overflow in cv::Luv2RGBinteger::process.#21112
alalek merged 2 commits intoopencv:3.4from
vrabaud:3.4_luv_overflow

Conversation

@vrabaud
Copy link
Copy Markdown
Contributor

@vrabaud vrabaud commented Nov 24, 2021

For LL=49, uu=205, vv=23, we end up with x=7373056 and y=458
which overflows y*x.

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

@vrabaud vrabaud force-pushed the 3.4_luv_overflow branch 2 times, most recently from 4adb648 to 7735216 Compare November 24, 2021 16:20
long long int xv = ((int)up)*(long long)vp;
int x = (int)(xv/BASE);
x = y*x/BASE;
x = ((long long int)y)*x/BASE;
Copy link
Copy Markdown
Member

@alalek alalek Nov 25, 2021

Choose a reason for hiding this comment

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

Could you please add test for the case with overflow?
Existed Imgproc_ColorLuv_Full.bitExactness doesn't cover this code at all.

@savuor Which test should execute this "bit-exact" code (Luv2RGBinteger)?
Update: reproduced with -DCV_DISABLE_OPTIMIZATION=ON

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried to create a test but it is tough: a Luv(49,205,23) gives RGB(0,103,255) without my patch and (255,0,255) with my patch. Then again, converting back to Luv we respectively get (122,80,15) and (153,157,31). We hit the white point limitation as referenced in the doc and here https://answers.opencv.org/question/205330/converting-luv-rgb-luv-results-in-different-values/

If we use https://www.easyrgb.com/en/convert.php, the results are very sensitive at this point and due to rounding, I cannot tell which value should be returned.

I guess we hit the limits of the space and there is no real answer: https://upload.wikimedia.org/wikipedia/commons/8/8d/SRGB_gamut_within_CIELCHuv_color_space_mesh.webm

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, I added a test to ensure it gives the same result as the float version.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@alalek The test you've mentioned should check this code for bit-exactness. If it's not true, then why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@savuor : the bit exactness code has the same overflow bug

(that's why I had to modify it too). The fact that I had to change the hashes shows that there is indeed a bug.

For LL=49, uu=205, vv=23, we end up with x=7373056 and y=458
which overflows y*x.
Copy link
Copy Markdown
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 👍

@vrabaud
Copy link
Copy Markdown
Contributor Author

vrabaud commented Dec 1, 2021

I just need to merge those two commits before it can merged right ?

@alalek
Copy link
Copy Markdown
Member

alalek commented Dec 1, 2021

Not needed - I will squash commits during the merge.
(currently I'm waiting for completion of nightly builds)


TEST(Imgproc_ColorLuv, Overflow_21112)
{
const Size sz(107, 16); // unaligned size to run both SIMD and generil code
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

BTW, generil -> generic

@alalek alalek merged commit 1a1a7bb into opencv:3.4 Dec 1, 2021
@alalek alalek mentioned this pull request Dec 3, 2021
@vrabaud vrabaud deleted the 3.4_luv_overflow branch December 13, 2021 22:06
@vrabaud vrabaud restored the 3.4_luv_overflow branch December 13, 2021 22:06
@vrabaud vrabaud deleted the 3.4_luv_overflow branch December 13, 2021 22:08
@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants