Fix out-of-bounds access in setSize() from cv::Mat() constructor#18473
Fix out-of-bounds access in setSize() from cv::Mat() constructor#18473opencv-pushbot merged 1 commit intoopencv:3.4from
Conversation
|
Please provide a reproducer (simple test is preferable). |
|
Dear @alalek thanks for your consideration. Since the problem is an out-of-bounds access via pointer, it may not actually show an error. It will be easier to understand if you just look into the code. For me it crashed in maybe 1 out of 100 cases. To reproduce, just use the constructor from this link: https://docs.opencv.org/4.4.0/d3/d63/classcv_1_1Mat.html#a5fafc033e089143062fd31015b5d0f40 Example: |
|
Ok, maybe I know a better reproducer. We should be able to force the out-of-bounds to fail if we set the element to something that the element size is not a multiple of. I have not tested this on my side but the following should fail, claiming that steps is not a multiple of the element size. The problem comes from the number |
|
Thank you! OK, documentation says:
So your point is valid about the number of accessed elements in Reproducer above is not fully correct due to: -(2*100) * (2*100)
+100 * 100 * 2Please put this test code into test_mat.cpp file: (adopted to C++98, see below) Problem can be captured by or sporadic failures: As a bugfix this patch should go into 3.4 branch first. So, please:
Note: no needs to re-open PR, apply changes "inplace". |
eeae9b7 to
102d8f6
Compare
Hi @alalek ! I can not find the option to allow edits from maintainers, is it possible that this has been set already? At least the checkbox as discussed in github docs is not available for me. I also did not feel safe to rebase on https://github.com/alalek/opencv/commits/pr18473_r because rebase showed a conflict in Instead I cherry-picked my single change on top of If, one the other hand, you are faster to take my changes yourself onto some branch/PR/... then please accept my permission to take the changes in any way you like for integration into opencv. Thanks! |
alalek
left a comment
There was a problem hiding this comment.
Well done 👍
Will add test in a separate PR after merge.
Pull Request Readiness Checklist
Patch to opencv_extra has the same branch name.
This PR fixes an issue in the
setSize()method ofcv::Mat. According to the documentation (for example https://docs.opencv.org/4.4.0/d3/d63/classcv_1_1Mat.html#a5fafc033e089143062fd31015b5d0f40) the parameterconst size_t* stepsis anArray of ndims-1 steps in case of a multi-dimensional array (the last step is always set to the element size). This is reflected in the implementation, where them.step.p[i]is always set toeszfor the last dimension:opencv/modules/core/src/matrix.cpp
Line 245 in 1913482
However a few lines above, there is a check if
_steps[i]is a multiple ofesz1. This statement does access theith element of_stepsfor the last dimension. This is an out of bounds access according to the documentation:opencv/modules/core/src/matrix.cpp
Line 240 in 1913482
Proposed patch fixes the out-of-bounds access and restores the documented behavior.