From 435c97c7a2ca7d88dd1e54b2ce360a754002c130 Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Tue, 10 Dec 2019 15:13:38 +0300 Subject: [PATCH] imgproc: add parameter checks in calcHist and calcBackProj --- modules/imgproc/src/histogram.cpp | 14 +++++++-- modules/imgproc/test/test_histograms.cpp | 37 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/modules/imgproc/src/histogram.cpp b/modules/imgproc/src/histogram.cpp index 64a5c61afe..e66e2e2d23 100644 --- a/modules/imgproc/src/histogram.cpp +++ b/modules/imgproc/src/histogram.cpp @@ -75,8 +75,8 @@ calcHistLookupTables_8u( const Mat& hist, const SparseMat& shist, int sz = !issparse ? hist.size[i] : shist.size(i); size_t step = !issparse ? hist.step[i] : 1; - double v_lo = ranges[i][0]; - double v_hi = ranges[i][1]; + double v_lo = ranges ? ranges[i][0] : 0; + double v_hi = ranges ? ranges[i][1] : 256; for( j = low; j < high; j++ ) { @@ -183,7 +183,7 @@ static void histPrepareImages( const Mat* images, int nimages, const int* channe imsize.height = 1; } - if( !ranges ) + if( !ranges ) // implicit uniform ranges for 8U { CV_Assert( depth == CV_8U ); @@ -951,6 +951,8 @@ void cv::calcHist( const Mat* images, int nimages, const int* channels, { CV_INSTRUMENT_REGION(); + CV_Assert(images && nimages > 0); + CV_OVX_RUN( images && histSize && nimages == 1 && images[0].type() == CV_8UC1 && dims == 1 && _mask.getMat().empty() && @@ -1261,6 +1263,8 @@ void cv::calcHist( const Mat* images, int nimages, const int* channels, { CV_INSTRUMENT_REGION(); + CV_Assert(images && nimages > 0); + Mat mask = _mask.getMat(); calcHist( images, nimages, channels, mask, hist, dims, histSize, ranges, uniform, accumulate, false ); @@ -1608,6 +1612,8 @@ void cv::calcBackProject( const Mat* images, int nimages, const int* channels, { CV_INSTRUMENT_REGION(); + CV_Assert(images && nimages > 0); + Mat hist = _hist.getMat(); std::vector ptrs; std::vector deltas; @@ -1777,6 +1783,8 @@ void cv::calcBackProject( const Mat* images, int nimages, const int* channels, { CV_INSTRUMENT_REGION(); + CV_Assert(images && nimages > 0); + std::vector ptrs; std::vector deltas; std::vector uniranges; diff --git a/modules/imgproc/test/test_histograms.cpp b/modules/imgproc/test/test_histograms.cpp index e9c7575ca4..fdf31fe771 100644 --- a/modules/imgproc/test/test_histograms.cpp +++ b/modules/imgproc/test/test_histograms.cpp @@ -1957,5 +1957,42 @@ TEST(Imgproc_Hist_Calc, calcHist_regression_11544) } } +TEST(Imgproc_Hist_Calc, badarg) +{ + const int channels[] = {0}; + float range1[] = {0, 10}; + float range2[] = {10, 20}; + const float * ranges[] = {range1, range2}; + Mat img = cv::Mat::zeros(10, 10, CV_8UC1); + Mat imgInt = cv::Mat::zeros(10, 10, CV_32SC1); + Mat hist; + const int hist_size[] = { 100 }; + // base run + EXPECT_NO_THROW(cv::calcHist(&img, 1, channels, noArray(), hist, 1, hist_size, ranges, true)); + // bad parameters + EXPECT_THROW(cv::calcHist(NULL, 1, channels, noArray(), hist, 1, hist_size, ranges, true), cv::Exception); + EXPECT_THROW(cv::calcHist(&img, 0, channels, noArray(), hist, 1, hist_size, ranges, true), cv::Exception); + EXPECT_THROW(cv::calcHist(&img, 1, NULL, noArray(), hist, 2, hist_size, ranges, true), cv::Exception); + EXPECT_THROW(cv::calcHist(&img, 1, channels, noArray(), noArray(), 1, hist_size, ranges, true), cv::Exception); + EXPECT_THROW(cv::calcHist(&img, 1, channels, noArray(), hist, -1, hist_size, ranges, true), cv::Exception); + EXPECT_THROW(cv::calcHist(&img, 1, channels, noArray(), hist, 1, NULL, ranges, true), cv::Exception); + EXPECT_THROW(cv::calcHist(&imgInt, 1, channels, noArray(), hist, 1, hist_size, NULL, true), cv::Exception); + // special case + EXPECT_NO_THROW(cv::calcHist(&img, 1, channels, noArray(), hist, 1, hist_size, NULL, true)); + + Mat backProj; + // base run + EXPECT_NO_THROW(cv::calcBackProject(&img, 1, channels, hist, backProj, ranges, 1, true)); + // bad parameters + EXPECT_THROW(cv::calcBackProject(NULL, 1, channels, hist, backProj, ranges, 1, true), cv::Exception); + EXPECT_THROW(cv::calcBackProject(&img, 0, channels, hist, backProj, ranges, 1, true), cv::Exception); + EXPECT_THROW(cv::calcBackProject(&img, 1, channels, noArray(), backProj, ranges, 1, true), cv::Exception); + EXPECT_THROW(cv::calcBackProject(&img, 1, channels, hist, noArray(), ranges, 1, true), cv::Exception); + EXPECT_THROW(cv::calcBackProject(&imgInt, 1, channels, hist, backProj, NULL, 1, true), cv::Exception); + // special case + EXPECT_NO_THROW(cv::calcBackProject(&img, 1, channels, hist, backProj, NULL, 1, true)); +} + + }} // namespace /* End Of File */