Skip to content

Commit 14451f3

Browse files
committed
core: fix adjustROI behavior on indexes overflow
1 parent 526220a commit 14451f3

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

modules/core/src/matrix.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,8 +971,13 @@ Mat& Mat::adjustROI( int dtop, int dbottom, int dleft, int dright )
971971
Size wholeSize; Point ofs;
972972
size_t esz = elemSize();
973973
locateROI( wholeSize, ofs );
974-
int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
975-
int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
974+
int row1 = std::min(std::max(ofs.y - dtop, 0), wholeSize.height), row2 = std::max(0, std::min(ofs.y + rows + dbottom, wholeSize.height));
975+
int col1 = std::min(std::max(ofs.x - dleft, 0), wholeSize.width), col2 = std::max(0, std::min(ofs.x + cols + dright, wholeSize.width));
976+
if(row1 > row2)
977+
std::swap(row1, row2);
978+
if(col1 > col2)
979+
std::swap(col1, col2);
980+
976981
data += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
977982
rows = row2 - row1; cols = col2 - col1;
978983
size.p[0] = rows; size.p[1] = cols;

modules/core/src/umatrix.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,13 @@ UMat& UMat::adjustROI( int dtop, int dbottom, int dleft, int dright )
599599
Size wholeSize; Point ofs;
600600
size_t esz = elemSize();
601601
locateROI( wholeSize, ofs );
602-
int row1 = std::max(ofs.y - dtop, 0), row2 = std::min(ofs.y + rows + dbottom, wholeSize.height);
603-
int col1 = std::max(ofs.x - dleft, 0), col2 = std::min(ofs.x + cols + dright, wholeSize.width);
602+
int row1 = std::min(std::max(ofs.y - dtop, 0), wholeSize.height), row2 = std::max(0, std::min(ofs.y + rows + dbottom, wholeSize.height));
603+
int col1 = std::min(std::max(ofs.x - dleft, 0), wholeSize.width), col2 = std::max(0, std::min(ofs.x + cols + dright, wholeSize.width));
604+
if(row1 > row2)
605+
std::swap(row1, row2);
606+
if(col1 > col2)
607+
std::swap(col1, col2);
608+
604609
offset += (row1 - ofs.y)*step + (col1 - ofs.x)*esz;
605610
rows = row2 - row1; cols = col2 - col1;
606611
size.p[0] = rows; size.p[1] = cols;

modules/core/test/test_operations.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,3 +1240,17 @@ class CV_SparseMatTest : public cvtest::BaseTest
12401240
};
12411241

12421242
TEST(Core_SparseMat, iterations) { CV_SparseMatTest test; test.safe_run(); }
1243+
1244+
TEST(MatTestRoi, adjustRoiOverflow)
1245+
{
1246+
Mat m(15, 10, CV_32S);
1247+
Mat roi(m, cv::Range(2, 10), cv::Range(3,6));
1248+
int rowsInROI = roi.rows;
1249+
roi.adjustROI(1, 0, 0, 0);
1250+
1251+
ASSERT_EQ(roi.rows, rowsInROI + 1);
1252+
1253+
roi.adjustROI(-m.rows, -m.rows, 0, 0);
1254+
1255+
ASSERT_EQ(roi.rows, m.rows);
1256+
}

modules/core/test/test_umat.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,20 @@ TEST_P(UMatTestRoi, adjustRoi)
496496

497497
INSTANTIATE_TEST_CASE_P(UMat, UMatTestRoi, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, UMAT_TEST_SIZES ));
498498

499+
TEST(UMatTestRoi, adjustRoiOverflow)
500+
{
501+
UMat m(15, 10, CV_32S);
502+
UMat roi(m, cv::Range(2, 10), cv::Range(3,6));
503+
int rowsInROI = roi.rows;
504+
roi.adjustROI(1, 0, 0, 0);
505+
506+
ASSERT_EQ(roi.rows, rowsInROI + 1);
507+
508+
roi.adjustROI(-m.rows, -m.rows, 0, 0);
509+
510+
ASSERT_EQ(roi.rows, m.rows);
511+
}
512+
499513
/////////////////////////////////////////////////////////////// Size ////////////////////////////////////////////////////////////////////
500514

501515
PARAM_TEST_CASE(UMatTestSizeOperations, int, int, Size, bool)

0 commit comments

Comments
 (0)