From b9435b9e38dfaa707623879158bcb92952010fe0 Mon Sep 17 00:00:00 2001 From: RAJKIRAN NATARAJAN Date: Mon, 9 Dec 2019 19:24:35 +0000 Subject: [PATCH] Merge pull request #16094 from saskatchewancatch:issue-16053 * Add eps error checking for approxPolyDP to allow sensible values only for epsilon value of Douglas-Peucker algorithm. * Review changes for PR --- modules/imgproc/src/approx.cpp | 7 +++++++ modules/imgproc/test/test_approxpoly.cpp | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/modules/imgproc/src/approx.cpp b/modules/imgproc/src/approx.cpp index 8491e81..195cd16 100644 --- a/modules/imgproc/src/approx.cpp +++ b/modules/imgproc/src/approx.cpp @@ -677,6 +677,13 @@ void cv::approxPolyDP( InputArray _curve, OutputArray _approxCurve, { CV_INSTRUMENT_REGION(); + //Prevent unreasonable error values (Douglas-Peucker algorithm) + //from being used. + if (epsilon < 0.0 || !(epsilon < 1e30)) + { + CV_Error(CV_StsOutOfRange, "Epsilon not valid."); + } + Mat curve = _curve.getMat(); int npoints = curve.checkVector(2), depth = curve.depth(); CV_Assert( npoints >= 0 && (depth == CV_32S || depth == CV_32F)); diff --git a/modules/imgproc/test/test_approxpoly.cpp b/modules/imgproc/test/test_approxpoly.cpp index 69511a6..845d7bb 100644 --- a/modules/imgproc/test/test_approxpoly.cpp +++ b/modules/imgproc/test/test_approxpoly.cpp @@ -355,4 +355,25 @@ _exit_: TEST(Imgproc_ApproxPoly, accuracy) { CV_ApproxPolyTest test; test.safe_run(); } +//Tests to make sure that unreasonable epsilon (error) +//values never get passed to the Douglas-Peucker algorithm. +TEST(Imgproc_ApproxPoly, bad_epsilon) +{ + std::vector inputPoints; + inputPoints.push_back(Point2f(0.0f, 0.0f)); + std::vector outputPoints; + + double eps = std::numeric_limits::infinity(); + ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false)); + + eps = 9e99; + ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false)); + + eps = -1e-6; + ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false)); + + eps = NAN; + ASSERT_ANY_THROW(approxPolyDP(inputPoints, outputPoints, eps, false)); +} + }} // namespace -- 2.7.4