From 94de7e5d2146b53138a293bb792953748bf6a3b7 Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Wed, 26 Jul 2023 19:00:22 +0200 Subject: [PATCH] Merge pull request #24042 from vrabaud:circle Fix harmless ASAN error. #24042 For an empty radius, &v[0] would be accessed (though the called functions would not use it due to v.size() being 0). Also add checks for emptyness and fix the first element checks, in case we get INT_MAX to compare to. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/imgproc/src/drawing.cpp | 5 +++-- modules/imgproc/test/test_drawing.cpp | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/modules/imgproc/src/drawing.cpp b/modules/imgproc/src/drawing.cpp index cbd238aca9..40ba22430f 100644 --- a/modules/imgproc/src/drawing.cpp +++ b/modules/imgproc/src/drawing.cpp @@ -939,6 +939,7 @@ void ellipse2Poly( Point center, Size axes, int angle, } // If there are no points, it's a zero-size polygon + CV_Assert( !pts.empty() ); if (pts.size() == 1) { pts.assign(2, center); } @@ -1001,6 +1002,7 @@ void ellipse2Poly( Point2d center, Size2d axes, int angle, } // If there are no points, it's a zero-size polygon + CV_Assert( !pts.empty() ); if( pts.size() == 1) { pts.assign(2,center); } @@ -1021,7 +1023,6 @@ EllipseEx( Mat& img, Point2l center, Size2l axes, std::vector v; Point2l prevPt(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); - v.resize(0); for (unsigned int i = 0; i < _v.size(); ++i) { Point2l pt; @@ -1036,7 +1037,7 @@ EllipseEx( Mat& img, Point2l center, Size2l axes, } // If there are no points, it's a zero-size polygon - if (v.size() == 1) { + if (v.size() <= 1) { v.assign(2, center); } diff --git a/modules/imgproc/test/test_drawing.cpp b/modules/imgproc/test/test_drawing.cpp index 23208ab881..02095cd6b7 100644 --- a/modules/imgproc/test/test_drawing.cpp +++ b/modules/imgproc/test/test_drawing.cpp @@ -921,4 +921,11 @@ TEST(Drawing, circle_overflow) cv::circle(matrix, cv::Point(275, -2147483318), 2147483647, kBlue, 1, 8, 0); } +TEST(Drawing, circle_memory_access) +{ + cv::Mat1b matrix = cv::Mat1b::zeros(10, 10); + cv::Scalar kBlue = cv::Scalar(0, 0, 255); + cv::circle(matrix, cv::Point(-1, -1), 0, kBlue, 2, 8, 16); +} + }} // namespace