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
pull/24062/head
Vincent Rabaud 2 years ago committed by GitHub
parent a817813b50
commit 94de7e5d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      modules/imgproc/src/drawing.cpp
  2. 7
      modules/imgproc/test/test_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 // If there are no points, it's a zero-size polygon
CV_Assert( !pts.empty() );
if (pts.size() == 1) { if (pts.size() == 1) {
pts.assign(2, center); 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 // If there are no points, it's a zero-size polygon
CV_Assert( !pts.empty() );
if( pts.size() == 1) { if( pts.size() == 1) {
pts.assign(2,center); pts.assign(2,center);
} }
@ -1021,7 +1023,6 @@ EllipseEx( Mat& img, Point2l center, Size2l axes,
std::vector<Point2l> v; std::vector<Point2l> v;
Point2l prevPt(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF); Point2l prevPt(0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF);
v.resize(0);
for (unsigned int i = 0; i < _v.size(); ++i) for (unsigned int i = 0; i < _v.size(); ++i)
{ {
Point2l pt; 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 there are no points, it's a zero-size polygon
if (v.size() == 1) { if (v.size() <= 1) {
v.assign(2, center); v.assign(2, center);
} }

@ -921,4 +921,11 @@ TEST(Drawing, circle_overflow)
cv::circle(matrix, cv::Point(275, -2147483318), 2147483647, kBlue, 1, 8, 0); 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 }} // namespace

Loading…
Cancel
Save