From 377dd04224630e835cce8c7d67e651cae73fd3b3 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Fri, 20 Mar 2020 13:04:04 +0000 Subject: [PATCH] core: fix .begin()/.end() of empty Mat --- modules/core/include/opencv2/core/mat.inl.hpp | 11 +++++++++++ modules/core/test/test_mat.cpp | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index f2efe1c820..55b4e625be 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -1304,6 +1304,8 @@ const _Tp& Mat::at(const Vec& idx) const template inline MatConstIterator_<_Tp> Mat::begin() const { + if (empty()) + return MatConstIterator_<_Tp>(); CV_DbgAssert( elemSize() == sizeof(_Tp) ); return MatConstIterator_<_Tp>((const Mat_<_Tp>*)this); } @@ -1311,6 +1313,8 @@ MatConstIterator_<_Tp> Mat::begin() const template inline MatConstIterator_<_Tp> Mat::end() const { + if (empty()) + return MatConstIterator_<_Tp>(); CV_DbgAssert( elemSize() == sizeof(_Tp) ); MatConstIterator_<_Tp> it((const Mat_<_Tp>*)this); it += total(); @@ -1320,6 +1324,8 @@ MatConstIterator_<_Tp> Mat::end() const template inline MatIterator_<_Tp> Mat::begin() { + if (empty()) + return MatIterator_<_Tp>(); CV_DbgAssert( elemSize() == sizeof(_Tp) ); return MatIterator_<_Tp>((Mat_<_Tp>*)this); } @@ -1327,6 +1333,8 @@ MatIterator_<_Tp> Mat::begin() template inline MatIterator_<_Tp> Mat::end() { + if (empty()) + return MatIterator_<_Tp>(); CV_DbgAssert( elemSize() == sizeof(_Tp) ); MatIterator_<_Tp> it((Mat_<_Tp>*)this); it += total(); @@ -2690,6 +2698,7 @@ MatConstIterator::MatConstIterator(const Mat* _m) { if( m && m->isContinuous() ) { + CV_Assert(!m->empty()); sliceStart = m->ptr(); sliceEnd = sliceStart + m->total()*elemSize; } @@ -2703,6 +2712,7 @@ MatConstIterator::MatConstIterator(const Mat* _m, int _row, int _col) CV_Assert(m && m->dims <= 2); if( m->isContinuous() ) { + CV_Assert(!m->empty()); sliceStart = m->ptr(); sliceEnd = sliceStart + m->total()*elemSize; } @@ -2717,6 +2727,7 @@ MatConstIterator::MatConstIterator(const Mat* _m, Point _pt) CV_Assert(m && m->dims <= 2); if( m->isContinuous() ) { + CV_Assert(!m->empty()); sliceStart = m->ptr(); sliceEnd = sliceStart + m->total()*elemSize; } diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index f4f3597034..efb433dc79 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -2082,4 +2082,12 @@ TEST(Mat, regression_12943) // memory usage: ~4.5 Gb cv::flip(src, dst, 0); } +TEST(Mat, empty_iterator_16855) +{ + cv::Mat m; + EXPECT_NO_THROW(m.begin()); + EXPECT_NO_THROW(m.end()); + EXPECT_TRUE(m.begin() == m.end()); +} + }} // namespace