diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 8f904559d9..d11d5051e9 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -107,7 +107,10 @@ inline _InputArray::_InputArray(const std::vector& vec) { init(+STD_VECTOR template inline _InputArray::_InputArray(const std::vector<_Tp>& vec) -{ init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); } +{ + CV_CheckLE(vec.size(), static_cast(std::numeric_limits::max()), "Must not be larger than INT_MAX"); + init(FIXED_TYPE + STD_VECTOR + traits::Type<_Tp>::value + ACCESS_READ, &vec); +} template inline _InputArray::_InputArray(const std::array<_Tp, _Nm>& arr) diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index ea6370cfb0..ae8d5ddfda 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -1471,6 +1471,14 @@ TEST(Core_InputArray, empty) ASSERT_TRUE( _InputArray(data).empty() ); } +TEST(Core_InputArray, convert_from_vector_over2GB) +{ + applyTestTag(CV_TEST_TAG_MEMORY_6GB); + // empty buffer more than 2GB size + std::vector buf(size_t(INT_MAX) + 4096); + EXPECT_ANY_THROW(auto work = _InputArray(buf)); +} + TEST(Core_CopyMask, bug1918) { Mat_ tmpSrc(100,100); diff --git a/modules/imgcodecs/test/test_read_write.cpp b/modules/imgcodecs/test/test_read_write.cpp index 1e171d9e8d..2508ef2dbd 100644 --- a/modules/imgcodecs/test/test_read_write.cpp +++ b/modules/imgcodecs/test/test_read_write.cpp @@ -619,4 +619,13 @@ TEST(Imgcodecs_Params, imencode_regression_22752) EXPECT_ANY_THROW(cv::imencode("test.jpg", img, buf, params)); // parameters size or missing JPEG codec } +TEST(Imgcodecs, decode_over2GB) +{ + applyTestTag(CV_TEST_TAG_MEMORY_6GB); + // empty buffer more than 2GB size + std::vector buf(size_t(INT_MAX) + 4096); + cv::Mat dst; + EXPECT_THROW(dst = cv::imdecode(buf, cv::IMREAD_COLOR), cv::Exception); +} + }} // namespace