Skip to content

core(matrix): Negative values checks#17077

Merged
opencv-pushbot merged 1 commit intoopencv:3.4from
i386x:check-negative-values
Nov 26, 2020
Merged

core(matrix): Negative values checks#17077
opencv-pushbot merged 1 commit intoopencv:3.4from
i386x:check-negative-values

Conversation

@i386x
Copy link
Copy Markdown
Contributor

@i386x i386x commented Apr 15, 2020

Add checks that prevents indexing an array by negative values (issues were discovered by static code analysis with coverity).

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 OpenCV (BSD) 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

{
const std::vector<cuda::GpuMat>& vv = *(const std::vector<cuda::GpuMat>*)obj;
if( i < 0 )
return 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is that correct? Why not assertion?

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.

It is used at several similar places, like in conditional that handles STD_ARRAY_MAT. As step (and offset) are declared with -1 to be a default value of i, my guess, based on this, is that if ( i < 0 ) return 1; is preferred over assertion to prevent mat.step() and mat.offset() calls from failing.

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.

maybe, it should be sizeof(cuda::GpuMat>)?

{
const Mat* vv = (const Mat*)obj;
CV_Assert(i < sz.height);
CV_Assert(i > 0 && i < sz.height);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why >= somewhere and > here?

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.

The same assertion is used also in _InputArray::isContinuous for STD_ARRAY_MAT case above. I am not sure whether > is intentional for STD_ARRAY_MAT case or it is a typo. Maybe >= should be used in both places?

@asmorkalov asmorkalov added pr: Discussion Required pr: needs test New functionality requires minimal tests set labels Apr 24, 2020
@asmorkalov
Copy link
Copy Markdown
Contributor

@i386x Do have a chance to review notes?

@i386x
Copy link
Copy Markdown
Contributor Author

i386x commented Apr 28, 2020

Hi @asmorkalov, I am afraid I not fully understand what notes do you have in your mind. Can you please be more specific?

@asmorkalov
Copy link
Copy Markdown
Contributor

@i386x I messed you replies above. Some of cases you touched are not well covered with tests. Could you add some cases for the fixed bug? @alalek could you provide some details on test coverage?

@i386x i386x force-pushed the check-negative-values branch from b77343e to 33cf9f6 Compare May 4, 2020 08:38
@asmorkalov
Copy link
Copy Markdown
Contributor

@386x well done! Thanks for the contributed test.

@asmorkalov asmorkalov added test and removed pr: needs test New functionality requires minimal tests set labels May 4, 2020
@vpisarev vpisarev self-assigned this Jun 30, 2020
@vpisarev
Copy link
Copy Markdown
Contributor

👍

@i386x i386x force-pushed the check-negative-values branch 2 times, most recently from 24decb6 to 479c7b8 Compare July 10, 2020 19:17
@vpisarev
Copy link
Copy Markdown
Contributor

@alalek, the pull request now passes all the checks, I suggest to merge it in

{
const std::vector<Mat>& vv = *(const std::vector<Mat>*)obj;
CV_Assert((size_t)i < vv.size());
CV_Assert(i >= 0 && (size_t)i < vv.size());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Casting to unsigned type is a trick to handle i >= 0 check automatically.

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.

Oh, ok. Should I revert it than and write a note to comment?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe we can keep this as is.

{
const std::vector<UMat>& vv = *(const std::vector<UMat>*)obj;
if( i < 0 )
return 1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is 1 here for offset? What is logic here?

I know that this is a copy-paste, but we should not copy mistakes.

IMHO we should ban this method call with negative argument value (through CV_Assert()).

Copy link
Copy Markdown
Contributor Author

@i386x i386x Aug 10, 2020

Choose a reason for hiding this comment

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

Why is 1 here for offset? What is logic here?

I tried to find some design documents about _InputArray, but without success.

I know that this is a copy-paste, but we should not copy mistakes.

IMHO we should ban this method call with negative argument value (through CV_Assert()).

Agree. The current behavior is a bit inconsistent (some kind of matrices raise an error and some return 1). I will fix the patch and tests.

@asmorkalov
Copy link
Copy Markdown
Contributor

asmorkalov commented Aug 3, 2020

@i386x Friendly reminder.

1 similar comment
@asmorkalov
Copy link
Copy Markdown
Contributor

@i386x Friendly reminder.

@asmorkalov
Copy link
Copy Markdown
Contributor

@i386x Do you have chance to finish the patch in meantime?

@i386x
Copy link
Copy Markdown
Contributor Author

i386x commented Nov 20, 2020

@asmorkalov Sorry, I totally forgot about it. I am now waiting on build and tests to be finished on my machine and then I will submit the updated patch.

@i386x i386x force-pushed the check-negative-values branch from 479c7b8 to a6936ea Compare November 20, 2020 21:47
Add checks that prevents indexing an array by negative values.
@i386x i386x force-pushed the check-negative-values branch from a6936ea to ce31c9c Compare November 20, 2020 21:52
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.

Well done! Thank you 👍

@opencv-pushbot opencv-pushbot merged commit 8c5b3c4 into opencv:3.4 Nov 26, 2020
@alalek alalek mentioned this pull request Nov 27, 2020
@alalek alalek mentioned this pull request Nov 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants