1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 07:43:03 +04:00

Merge pull request #24260 from vrabaud:ubsan

Fix undefined behavior arithmetic in copyMakeBorder and adjustROI. #24260

This is due to the undefined: negative int multiplied by size_t pointer increment.

To test, compile with:
```
mkdir build
cd build
cmake ../ -DCMAKE_C_FLAGS_INIT="-fsanitize=undefined" -DCMAKE_CXX_FLAGS_INIT="-fsanitize=undefined" -DCMAKE_C_COMPILER="/usr/bin/clang" -DCMAKE_CXX_COMPILER="/usr/bin/clang++" -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=undefined -lubsan"
```
And run:
```
make -j opencv_test_core && ./bin/opencv_test_core --gtest_filter=*UndefinedBehavior*
```

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Vincent Rabaud
2023-09-14 14:16:28 +02:00
committed by GitHub
parent ec1c0608bc
commit 3880d059b3
4 changed files with 24 additions and 3 deletions
+9
View File
@@ -1379,6 +1379,15 @@ TEST(MatTestRoi, adjustRoiOverflow)
ASSERT_EQ(roi.rows, m.rows);
}
TEST(MatTestRoi, adjustRoiUndefinedBehavior)
{
Mat m(6, 6, CV_8U);
Mat roi(m, cv::Range(2, 4), cv::Range(2, 4));
// This could trigger a (negative int)*size_t when updating data,
// which is undefined behavior.
roi.adjustROI(2, 2, 2, 2);
EXPECT_EQ(m.data, roi.data);
}
CV_ENUM(SortRowCol, SORT_EVERY_COLUMN, SORT_EVERY_ROW)
CV_ENUM(SortOrder, SORT_ASCENDING, SORT_DESCENDING)