From 40b762bcebd30b3bbd0b592250a1662d34a7b776 Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Fri, 24 Apr 2015 16:40:14 +0300 Subject: [PATCH] adding test for `polylines()` call with empty Point-s vector and fix for crash in this case --- modules/core/src/drawing.cpp | 3 +++ modules/core/test/test_misc.cpp | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/modules/core/src/drawing.cpp b/modules/core/src/drawing.cpp index 4af39347fd..bd42ac5066 100644 --- a/modules/core/src/drawing.cpp +++ b/modules/core/src/drawing.cpp @@ -2215,7 +2215,10 @@ void cv::polylines(InputOutputArray _img, InputArrayOfArrays pts, { Mat p = pts.getMat(manyContours ? i : -1); if( p.total() == 0 ) + { + npts[i] = 0; continue; + } CV_Assert(p.checkVector(2, CV_32S) >= 0); ptsptr[i] = (Point*)p.data; npts[i] = p.rows*p.cols*p.channels()/2; diff --git a/modules/core/test/test_misc.cpp b/modules/core/test/test_misc.cpp index 5af419c939..79e2cad5c9 100644 --- a/modules/core/test/test_misc.cpp +++ b/modules/core/test/test_misc.cpp @@ -48,3 +48,23 @@ TEST(Core_SaturateCast, NegativeNotClipped) ASSERT_EQ(0xffffffff, val); } + +TEST(Core_Drawing, polylines_empty) +{ + Mat img(100, 100, CV_8UC1, Scalar(0)); + vector pts; // empty + polylines(img, pts, false, Scalar(255)); + int cnt = countNonZero(img); + ASSERT_EQ(cnt, 0); +} + +TEST(Core_Drawing, polylines) +{ + Mat img(100, 100, CV_8UC1, Scalar(0)); + vector pts; + pts.push_back(Point(0, 0)); + pts.push_back(Point(20, 0)); + polylines(img, pts, false, Scalar(255)); + int cnt = countNonZero(img); + ASSERT_EQ(cnt, 21); +} \ No newline at end of file