From 1da46fe6fba30f8e8930d3a15ef3889026a3d3da Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Tue, 17 Jul 2018 16:14:54 +0300 Subject: [PATCH 1/2] Fixed issues found by static analysis (mostly DBZ) --- modules/calib3d/src/calibration.cpp | 5 +++++ modules/calib3d/src/dls.cpp | 1 + modules/calib3d/src/fisheye.cpp | 12 +++++++++--- modules/calib3d/src/homography_decomp.cpp | 1 + modules/core/include/opencv2/core/mat.inl.hpp | 8 ++++++-- modules/core/src/batch_distance.cpp | 1 + modules/dnn/src/layers/resize_layer.cpp | 2 +- modules/features2d/src/bagofwords.cpp | 1 + modules/features2d/src/blobdetector.cpp | 3 +++ modules/features2d/src/brisk.cpp | 2 ++ modules/imgproc/src/deriv.cpp | 2 +- modules/imgproc/src/filter.cpp | 12 ++++++++---- modules/imgproc/src/grabcut.cpp | 1 + modules/imgproc/src/imgwarp.cpp | 2 ++ modules/imgproc/src/linefit.cpp | 2 ++ modules/imgproc/src/lsd.cpp | 1 + modules/imgproc/src/moments.cpp | 11 +++++++---- modules/imgproc/src/smooth.cpp | 6 ++++++ modules/ml/src/em.cpp | 1 + modules/ml/src/rtrees.cpp | 1 + modules/ml/src/tree.cpp | 2 +- modules/objdetect/src/haar.cpp | 2 +- modules/objdetect/src/qrcode.cpp | 2 ++ modules/photo/src/tonemap.cpp | 6 +++++- modules/shape/src/sc_dis.cpp | 1 + modules/videostab/src/inpainting.cpp | 2 +- 26 files changed, 71 insertions(+), 19 deletions(-) diff --git a/modules/calib3d/src/calibration.cpp b/modules/calib3d/src/calibration.cpp index 5de4db9959..8c136b5b58 100644 --- a/modules/calib3d/src/calibration.cpp +++ b/modules/calib3d/src/calibration.cpp @@ -2336,10 +2336,13 @@ void cvStereoRectify( const CvMat* _cameraMatrix1, const CvMat* _cameraMatrix2, _uu[2] = 1; cvCrossProduct(&uu, &t, &ww); nt = cvNorm(&t, 0, CV_L2); + CV_Assert(fabs(nt) > 0); nw = cvNorm(&ww, 0, CV_L2); + CV_Assert(fabs(nw) > 0); cvConvertScale(&ww, &ww, 1 / nw); cvCrossProduct(&t, &ww, &w3); nw = cvNorm(&w3, 0, CV_L2); + CV_Assert(fabs(nw) > 0); cvConvertScale(&w3, &w3, 1 / nw); _uu[2] = 0; @@ -3870,12 +3873,14 @@ float cv::rectify3Collinear( InputArray _cameraMatrix1, InputArray _distCoeffs1, int idx = fabs(t12(0,0)) > fabs(t12(1,0)) ? 0 : 1; double c = t12(idx,0), nt = norm(t12, CV_L2); + CV_Assert(fabs(nt) > 0); Mat_ uu = Mat_::zeros(3,1); uu(idx, 0) = c > 0 ? 1 : -1; // calculate global Z rotation Mat_ ww = t12.cross(uu), wR; double nw = norm(ww, CV_L2); + CV_Assert(fabs(nw) > 0); ww *= acos(fabs(c)/nt)/nw; Rodrigues(ww, wR); diff --git a/modules/calib3d/src/dls.cpp b/modules/calib3d/src/dls.cpp index d44c364b49..b0334c4268 100644 --- a/modules/calib3d/src/dls.cpp +++ b/modules/calib3d/src/dls.cpp @@ -206,6 +206,7 @@ void dls::run_kernel(const cv::Mat& pp) void dls::build_coeff_matrix(const cv::Mat& pp, cv::Mat& Mtilde, cv::Mat& D) { + CV_Assert(!pp.empty()); cv::Mat eye = cv::Mat::eye(3, 3, CV_64F); // build coeff matrix diff --git a/modules/calib3d/src/fisheye.cpp b/modules/calib3d/src/fisheye.cpp index 4ca6b71ca1..83a5a88c5f 100644 --- a/modules/calib3d/src/fisheye.cpp +++ b/modules/calib3d/src/fisheye.cpp @@ -126,7 +126,8 @@ void cv::fisheye::projectPoints(InputArray objectPoints, OutputArray imagePoints { Vec3d Xi = objectPoints.depth() == CV_32F ? (Vec3d)Xf[i] : Xd[i]; Vec3d Y = aff*Xi; - + if (fabs(Y[2]) < DBL_MIN) + Y[2] = 1; Vec2d x(Y[0]/Y[2], Y[1]/Y[2]); double r2 = x.dot(x); @@ -1186,6 +1187,7 @@ void cv::internal::ComputeExtrinsicRefine(const Mat& imagePoints, const Mat& obj { CV_Assert(!objectPoints.empty() && objectPoints.type() == CV_64FC3); CV_Assert(!imagePoints.empty() && imagePoints.type() == CV_64FC2); + CV_Assert(rvec.total() > 2 && tvec.total() > 2); Vec6d extrinsics(rvec.at(0), rvec.at(1), rvec.at(2), tvec.at(0), tvec.at(1), tvec.at(2)); double change = 1; @@ -1365,9 +1367,13 @@ void cv::internal::InitExtrinsics(const Mat& _imagePoints, const Mat& _objectPoi double sc = .5 * (norm(H.col(0)) + norm(H.col(1))); H = H / sc; Mat u1 = H.col(0).clone(); - u1 = u1 / norm(u1); + double norm_u1 = norm(u1); + CV_Assert(fabs(norm_u1) > 0); + u1 = u1 / norm_u1; Mat u2 = H.col(1).clone() - u1.dot(H.col(1).clone()) * u1; - u2 = u2 / norm(u2); + double norm_u2 = norm(u2); + CV_Assert(fabs(norm_u2) > 0); + u2 = u2 / norm_u2; Mat u3 = u1.cross(u2); Mat RRR; hconcat(u1, u2, RRR); diff --git a/modules/calib3d/src/homography_decomp.cpp b/modules/calib3d/src/homography_decomp.cpp index 6975a7ef11..fea8882c5a 100644 --- a/modules/calib3d/src/homography_decomp.cpp +++ b/modules/calib3d/src/homography_decomp.cpp @@ -194,6 +194,7 @@ void HomographyDecompZhang::decompose(std::vector& camMotions) { Mat W, U, Vt; SVD::compute(getHnorm(), W, U, Vt); + CV_Assert(W.total() > 2 && Vt.total() > 7); double lambda1=W.at(0); double lambda3=W.at(2); double lambda1m3 = (lambda1-lambda3); diff --git a/modules/core/include/opencv2/core/mat.inl.hpp b/modules/core/include/opencv2/core/mat.inl.hpp index 98346f1bc0..2e9b57ecce 100644 --- a/modules/core/include/opencv2/core/mat.inl.hpp +++ b/modules/core/include/opencv2/core/mat.inl.hpp @@ -861,7 +861,9 @@ bool Mat::isSubmatrix() const inline size_t Mat::elemSize() const { - return dims > 0 ? step.p[dims - 1] : 0; + size_t res = dims > 0 ? step.p[dims - 1] : 0; + CV_DbgAssert(res != 0); + return res; } inline @@ -3789,7 +3791,9 @@ bool UMat::isSubmatrix() const inline size_t UMat::elemSize() const { - return dims > 0 ? step.p[dims - 1] : 0; + size_t res = dims > 0 ? step.p[dims - 1] : 0; + CV_DbgAssert(res != 0); + return res; } inline diff --git a/modules/core/src/batch_distance.cpp b/modules/core/src/batch_distance.cpp index 1fd088dd5e..4c90db7ec4 100644 --- a/modules/core/src/batch_distance.cpp +++ b/modules/core/src/batch_distance.cpp @@ -263,6 +263,7 @@ void cv::batchDistance( InputArray _src1, InputArray _src2, if( crosscheck ) { CV_Assert( K == 1 && update == 0 && mask.empty() ); + CV_Assert(!nidx.empty()); Mat tdist, tidx; batchDistance(src2, src1, tdist, dtype, tidx, normType, K, mask, 0, false); diff --git a/modules/dnn/src/layers/resize_layer.cpp b/modules/dnn/src/layers/resize_layer.cpp index 4bf7b506a5..b26206694d 100644 --- a/modules/dnn/src/layers/resize_layer.cpp +++ b/modules/dnn/src/layers/resize_layer.cpp @@ -14,7 +14,7 @@ namespace cv { namespace dnn { class ResizeLayerImpl : public ResizeLayer { public: - ResizeLayerImpl(const LayerParams& params) + ResizeLayerImpl(const LayerParams& params) : scaleWidth(0), scaleHeight(0) { setParamsFrom(params); outWidth = params.get("width", 0); diff --git a/modules/features2d/src/bagofwords.cpp b/modules/features2d/src/bagofwords.cpp index 31fb19d837..65eef9a0ef 100644 --- a/modules/features2d/src/bagofwords.cpp +++ b/modules/features2d/src/bagofwords.cpp @@ -177,6 +177,7 @@ void BOWImgDescriptorExtractor::compute( InputArray keypointDescriptors, OutputA CV_INSTRUMENT_REGION() CV_Assert( !vocabulary.empty() ); + CV_Assert(!keypointDescriptors.empty()); int clusterCount = descriptorSize(); // = vocabulary.rows diff --git a/modules/features2d/src/blobdetector.cpp b/modules/features2d/src/blobdetector.cpp index ffcc431b58..b8a2e95977 100644 --- a/modules/features2d/src/blobdetector.cpp +++ b/modules/features2d/src/blobdetector.cpp @@ -264,6 +264,8 @@ void SimpleBlobDetectorImpl::findBlobs(InputArray _image, InputArray _binaryImag convexHull(Mat(contours[contourIdx]), hull); double area = contourArea(Mat(contours[contourIdx])); double hullArea = contourArea(Mat(hull)); + if (fabs(hullArea) < DBL_EPSILON) + continue; double ratio = area / hullArea; if (ratio < params.minConvexity || ratio >= params.maxConvexity) continue; @@ -309,6 +311,7 @@ void SimpleBlobDetectorImpl::detect(InputArray image, std::vector& CV_INSTRUMENT_REGION() keypoints.clear(); + CV_Assert(params.minRepeatability != 0); Mat grayscaleImage; if (image.channels() == 3 || image.channels() == 4) cvtColor(image, grayscaleImage, COLOR_BGR2GRAY); diff --git a/modules/features2d/src/brisk.cpp b/modules/features2d/src/brisk.cpp index bacea2b5a4..5e233d0d8f 100644 --- a/modules/features2d/src/brisk.cpp +++ b/modules/features2d/src/brisk.cpp @@ -506,6 +506,7 @@ BRISK_Impl::smoothedIntensity(const cv::Mat& image, const cv::Mat& integral, con // scaling: const int scaling = (int)(4194304.0 / area); const int scaling2 = int(float(scaling) * area / 1024.0); + CV_Assert(scaling2 != 0); // the integral image is larger: const int integralcols = imagecols + 1; @@ -2238,6 +2239,7 @@ BriskLayer::value(const cv::Mat& mat, float xf, float yf, float scale_in) const // scaling: const int scaling = (int)(4194304.0f / area); const int scaling2 = (int)(float(scaling) * area / 1024.0f); + CV_Assert(scaling2 != 0); // calculate borders const float x_1 = xf - sigma_half; diff --git a/modules/imgproc/src/deriv.cpp b/modules/imgproc/src/deriv.cpp index cc37180fdd..2a1a73d7aa 100644 --- a/modules/imgproc/src/deriv.cpp +++ b/modules/imgproc/src/deriv.cpp @@ -546,10 +546,10 @@ static bool ocl_Laplacian5(InputArray _src, OutputArray _dst, size_t lmsz = dev.localMemSize(); size_t src_step = _src.step(), src_offset = _src.offset(); const size_t tileSizeYmax = wgs / tileSizeX; + CV_Assert(src_step != 0 && esz != 0); // workaround for NVIDIA: 3 channel vector type takes 4*elem_size in local memory int loc_mem_cn = dev.vendorID() == ocl::Device::VENDOR_NVIDIA && cn == 3 ? 4 : cn; - if (((src_offset % src_step) % esz == 0) && ( (borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE) || diff --git a/modules/imgproc/src/filter.cpp b/modules/imgproc/src/filter.cpp index 23c560736a..a0866156a1 100644 --- a/modules/imgproc/src/filter.cpp +++ b/modules/imgproc/src/filter.cpp @@ -4284,10 +4284,14 @@ static bool ocl_sepFilter2D_SinglePass(InputArray _src, OutputArray _dst, size_t src_step = _src.step(), src_offset = _src.offset(); bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; - if ((src_offset % src_step) % esz != 0 || (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F)) || - !(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || - borderType == BORDER_REFLECT || borderType == BORDER_WRAP || - borderType == BORDER_REFLECT_101)) + if (esz == 0 + || (src_offset % src_step) % esz != 0 + || (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F)) + || !(borderType == BORDER_CONSTANT + || borderType == BORDER_REPLICATE + || borderType == BORDER_REFLECT + || borderType == BORDER_WRAP + || borderType == BORDER_REFLECT_101)) return false; size_t lt2[2] = { optimizedSepFilterLocalWidth, optimizedSepFilterLocalHeight }; diff --git a/modules/imgproc/src/grabcut.cpp b/modules/imgproc/src/grabcut.cpp index 6edce405cb..ff3c601548 100644 --- a/modules/imgproc/src/grabcut.cpp +++ b/modules/imgproc/src/grabcut.cpp @@ -174,6 +174,7 @@ void GMM::addSample( int ci, const Vec3d color ) void GMM::endLearning() { + CV_Assert(totalSampleCount > 0); const double variance = 0.01; for( int ci = 0; ci < componentsCount; ci++ ) { diff --git a/modules/imgproc/src/imgwarp.cpp b/modules/imgproc/src/imgwarp.cpp index 66981e8f3d..ad090fd247 100644 --- a/modules/imgproc/src/imgwarp.cpp +++ b/modules/imgproc/src/imgwarp.cpp @@ -3286,6 +3286,7 @@ void cv::warpPolar(InputArray _src, OutputArray _dst, Size dsize, if (!(flags & CV_WARP_INVERSE_MAP)) { + CV_Assert(!dsize.empty()); double Kangle = CV_2PI / dsize.height; int phi, rho; @@ -3332,6 +3333,7 @@ void cv::warpPolar(InputArray _src, OutputArray _dst, Size dsize, Mat src = _dst.getMat(); Size ssize = _dst.size(); ssize.height -= 2 * ANGLE_BORDER; + CV_Assert(!ssize.empty()); const double Kangle = CV_2PI / ssize.height; double Kmag; if (semiLog) diff --git a/modules/imgproc/src/linefit.cpp b/modules/imgproc/src/linefit.cpp index 246d693586..103fa55950 100644 --- a/modules/imgproc/src/linefit.cpp +++ b/modules/imgproc/src/linefit.cpp @@ -47,6 +47,7 @@ static const double eps = 1e-6; static void fitLine2D_wods( const Point2f* points, int count, float *weights, float *line ) { + CV_Assert(count > 0); double x = 0, y = 0, x2 = 0, y2 = 0, xy = 0, w = 0; double dx2, dy2, dxy; int i; @@ -98,6 +99,7 @@ static void fitLine2D_wods( const Point2f* points, int count, float *weights, fl static void fitLine3D_wods( const Point3f * points, int count, float *weights, float *line ) { + CV_Assert(count > 0); int i; float w0 = 0; float x0 = 0, y0 = 0, z0 = 0; diff --git a/modules/imgproc/src/lsd.cpp b/modules/imgproc/src/lsd.cpp index d73787407e..370d76955d 100644 --- a/modules/imgproc/src/lsd.cpp +++ b/modules/imgproc/src/lsd.cpp @@ -772,6 +772,7 @@ bool LineSegmentDetectorImpl::refine(std::vector& reg, double reg_a ++n; } } + CV_Assert(n > 0); double mean_angle = sum / double(n); // 2 * standard deviation double tau = 2.0 * sqrt((s_sum - 2.0 * mean_angle * sum) / double(n) + mean_angle * mean_angle); diff --git a/modules/imgproc/src/moments.cpp b/modules/imgproc/src/moments.cpp index 7e52f1f2fa..fc986aff0b 100644 --- a/modules/imgproc/src/moments.cpp +++ b/modules/imgproc/src/moments.cpp @@ -495,6 +495,13 @@ static bool ocl_moments( InputArray _src, Moments& m, bool binary) const int TILE_SIZE = 32; const int K = 10; + Size sz = _src.getSz(); + int xtiles = divUp(sz.width, TILE_SIZE); + int ytiles = divUp(sz.height, TILE_SIZE); + int ntiles = xtiles*ytiles; + if (ntiles == 0) + return false; + ocl::Kernel k = ocl::Kernel("moments", ocl::imgproc::moments_oclsrc, format("-D TILE_SIZE=%d%s", TILE_SIZE, @@ -504,10 +511,6 @@ static bool ocl_moments( InputArray _src, Moments& m, bool binary) return false; UMat src = _src.getUMat(); - Size sz = src.size(); - int xtiles = (sz.width + TILE_SIZE-1)/TILE_SIZE; - int ytiles = (sz.height + TILE_SIZE-1)/TILE_SIZE; - int ntiles = xtiles*ytiles; UMat umbuf(1, ntiles*K, CV_32S); size_t globalsize[] = {(size_t)xtiles, std::max((size_t)TILE_SIZE, (size_t)sz.height)}; diff --git a/modules/imgproc/src/smooth.cpp b/modules/imgproc/src/smooth.cpp index 2bcc4b2789..d54065f801 100644 --- a/modules/imgproc/src/smooth.cpp +++ b/modules/imgproc/src/smooth.cpp @@ -1709,6 +1709,7 @@ void cv::sqrBoxFilter( InputArray _src, OutputArray _dst, int ddepth, cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype ) { + CV_Assert(n > 0); const int SMALL_GAUSSIAN_SIZE = 7; static const float small_gaussian_tab[][SMALL_GAUSSIAN_SIZE] = { @@ -1747,6 +1748,7 @@ cv::Mat cv::getGaussianKernel( int n, double sigma, int ktype ) } } + CV_DbgAssert(fabs(sum) > 0); sum = 1./sum; for( i = 0; i < n; i++ ) { @@ -5334,6 +5336,7 @@ public: wsum += w; } // overflow is not possible here => there is no need to use cv::saturate_cast + CV_DbgAssert(fabs(wsum) > 0); dptr[j] = (uchar)cvRound(sum/wsum); } } @@ -5419,6 +5422,7 @@ public: sum_b += b*w; sum_g += g*w; sum_r += r*w; wsum += w; } + CV_DbgAssert(fabs(wsum) > 0); wsum = 1.f/wsum; b0 = cvRound(sum_b*wsum); g0 = cvRound(sum_g*wsum); @@ -5678,6 +5682,7 @@ public: sum += val*w; wsum += w; } + CV_DbgAssert(fabs(wsum) > 0); dptr[j] = (float)(sum/wsum); } } @@ -5768,6 +5773,7 @@ public: sum_b += b*w; sum_g += g*w; sum_r += r*w; wsum += w; } + CV_DbgAssert(fabs(wsum) > 0); wsum = 1.f/wsum; b0 = sum_b*wsum; g0 = sum_g*wsum; diff --git a/modules/ml/src/em.cpp b/modules/ml/src/em.cpp index 8a1020dbd9..c2dfc9c523 100644 --- a/modules/ml/src/em.cpp +++ b/modules/ml/src/em.cpp @@ -616,6 +616,7 @@ public: expDiffSum += v; // sum_j(exp(L_ij - L_iq)) } + CV_Assert(expDiffSum > 0); if(probs) L.convertTo(*probs, ptype, 1./expDiffSum); diff --git a/modules/ml/src/rtrees.cpp b/modules/ml/src/rtrees.cpp index 0751e37b91..e10ef9bc42 100644 --- a/modules/ml/src/rtrees.cpp +++ b/modules/ml/src/rtrees.cpp @@ -170,6 +170,7 @@ public: double val = std::abs(w->ord_responses[w->sidx[i]]); max_response = std::max(max_response, val); } + CV_Assert(fabs(max_response) > 0); } if( rparams.calcVarImportance ) diff --git a/modules/ml/src/tree.cpp b/modules/ml/src/tree.cpp index da76a81f87..2f9dc049e1 100644 --- a/modules/ml/src/tree.cpp +++ b/modules/ml/src/tree.cpp @@ -630,7 +630,7 @@ void DTreesImpl::calcValue( int nidx, const vector& _sidx ) w->cv_Tn[nidx*cv_n + j] = INT_MAX; } } - + CV_Assert(fabs(sumw) > 0); node->node_risk = sum2 - (sum/sumw)*sum; node->node_risk /= sumw; node->value = sum/sumw; diff --git a/modules/objdetect/src/haar.cpp b/modules/objdetect/src/haar.cpp index 33110116b8..f988d01848 100644 --- a/modules/objdetect/src/haar.cpp +++ b/modules/objdetect/src/haar.cpp @@ -599,7 +599,7 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade, else sum0 += hidfeature->rect[k].weight * tr.width * tr.height; } - + CV_Assert(area0 > 0); hidfeature->rect[0].weight = (float)(-sum0/area0); } /* l */ } /* j */ diff --git a/modules/objdetect/src/qrcode.cpp b/modules/objdetect/src/qrcode.cpp index 731e74fea2..311181317e 100644 --- a/modules/objdetect/src/qrcode.cpp +++ b/modules/objdetect/src/qrcode.cpp @@ -111,6 +111,7 @@ vector QRDecode::searchVerticalLines() for (size_t i = 0; i < test_lines.size(); i++) { length += test_lines[i]; } + CV_Assert(length > 0); for (size_t i = 0; i < test_lines.size(); i++) { if (i == 2) { weight += abs((test_lines[i] / length) - 3.0/7.0); } @@ -182,6 +183,7 @@ vector QRDecode::separateHorizontalLines(vector list_lines) for (size_t i = 0; i < test_lines.size(); i++) { length += test_lines[i]; } + CV_Assert(length > 0); for (size_t i = 0; i < test_lines.size(); i++) { if (i % 3 == 0) { weight += abs((test_lines[i] / length) - 3.0/14.0); } diff --git a/modules/photo/src/tonemap.cpp b/modules/photo/src/tonemap.cpp index 2911fc55c8..053360f4c2 100644 --- a/modules/photo/src/tonemap.cpp +++ b/modules/photo/src/tonemap.cpp @@ -140,6 +140,7 @@ public: double max; minMaxLoc(gray_img, NULL, &max); + CV_Assert(max > 0); Mat map; log(gray_img + 1.0f, map); @@ -429,12 +430,15 @@ public: for(int i = 0; i < max_iterations; i++) { calculateProduct(p, product); - float alpha = rr / static_cast(p.dot(product)); + double dprod = p.dot(product); + CV_Assert(fabs(dprod) > 0); + float alpha = rr / static_cast(dprod); r -= alpha * product; x += alpha * p; float new_rr = static_cast(r.dot(r)); + CV_Assert(fabs(rr) > 0); p = r + (new_rr / rr) * p; rr = new_rr; diff --git a/modules/shape/src/sc_dis.cpp b/modules/shape/src/sc_dis.cpp index 26dd459378..cf4f9fe3a0 100644 --- a/modules/shape/src/sc_dis.cpp +++ b/modules/shape/src/sc_dis.cpp @@ -743,6 +743,7 @@ void SCDMatcher::hungarian(cv::Mat &costMatrix, std::vector &outMatc // calculate symmetric shape context cost cv::Mat trueCostMatrix(costMatrix, cv::Rect(0,0,sizeScd1, sizeScd2)); + CV_Assert(!trueCostMatrix.empty()); float leftcost = 0; for (int nrow=0; nrow 0) ? (1.f / wSum) : 0; // if wSum is 0, c1-c3 will be 0 too frame(y,x) = Point3_( static_cast(c1*wSumInv), static_cast(c2*wSumInv), From c473718bc24e1b6197696cb22a2ca46f2c6a1c3b Mon Sep 17 00:00:00 2001 From: Maksim Shabunin Date: Tue, 17 Jul 2018 17:50:50 +0300 Subject: [PATCH 2/2] Check for empty Mat in compare, operator= and RNG::fill, fixed related tests --- modules/core/src/arithm.cpp | 2 +- modules/core/src/copy.cpp | 2 ++ modules/core/src/rand.cpp | 2 ++ modules/core/test/test_rand.cpp | 5 +++-- modules/imgproc/test/test_grabcut.cpp | 1 - 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/core/src/arithm.cpp b/modules/core/src/arithm.cpp index 5ea596ffc6..e3ded859b6 100644 --- a/modules/core/src/arithm.cpp +++ b/modules/core/src/arithm.cpp @@ -1233,7 +1233,7 @@ void cv::compare(InputArray _src1, InputArray _src2, OutputArray _dst, int op) CV_Assert( op == CMP_LT || op == CMP_LE || op == CMP_EQ || op == CMP_NE || op == CMP_GE || op == CMP_GT ); - if(_src1.empty() && _src2.empty()) + if(_src1.empty() || _src2.empty()) { _dst.release(); return; diff --git a/modules/core/src/copy.cpp b/modules/core/src/copy.cpp index b8a52f2f5a..8775bff4aa 100644 --- a/modules/core/src/copy.cpp +++ b/modules/core/src/copy.cpp @@ -411,6 +411,8 @@ Mat& Mat::operator = (const Scalar& s) { CV_INSTRUMENT_REGION() + if (empty()) return *this; + const Mat* arrays[] = { this }; uchar* dptr; NAryMatIterator it(arrays, &dptr, 1); diff --git a/modules/core/src/rand.cpp b/modules/core/src/rand.cpp index 7b7f8ed9b6..a456c72633 100644 --- a/modules/core/src/rand.cpp +++ b/modules/core/src/rand.cpp @@ -511,6 +511,8 @@ static RandnScaleFunc randnScaleTab[] = void RNG::fill( InputOutputArray _mat, int disttype, InputArray _param1arg, InputArray _param2arg, bool saturateRange ) { + if (_mat.empty()) + return; Mat mat = _mat.getMat(), _param1 = _param1arg.getMat(), _param2 = _param2arg.getMat(); int depth = mat.depth(), cn = mat.channels(); AutoBuffer _parambuf; diff --git a/modules/core/test/test_rand.cpp b/modules/core/test/test_rand.cpp index 82bb6104a9..8677aa0c31 100644 --- a/modules/core/test/test_rand.cpp +++ b/modules/core/test/test_rand.cpp @@ -168,11 +168,12 @@ void Core_RandTest::run( int ) { tested_rng = saved_rng; int sz = 0, dsz = 0, slice; - for( slice = 0; slice < maxSlice; slice++, sz += dsz ) + for( slice = 0; slice < maxSlice && sz < SZ; slice++, sz += dsz ) { - dsz = slice+1 < maxSlice ? (int)(cvtest::randInt(rng) % (SZ - sz + 1)) : SZ - sz; + dsz = slice+1 < maxSlice ? (int)(cvtest::randInt(rng) % (SZ - sz) + 1) : SZ - sz; Mat aslice = arr[k].colRange(sz, sz + dsz); tested_rng.fill(aslice, dist_type, A, B); + printf("%d - %d\n", sz, sz + dsz); } } diff --git a/modules/imgproc/test/test_grabcut.cpp b/modules/imgproc/test/test_grabcut.cpp index eae8b3e482..7bf6555a0c 100644 --- a/modules/imgproc/test/test_grabcut.cpp +++ b/modules/imgproc/test/test_grabcut.cpp @@ -89,7 +89,6 @@ void CV_GrabcutTest::run( int /* start_from */) Mat exp_bgdModel, exp_fgdModel; Mat mask; - mask = Scalar(0); Mat bgdModel, fgdModel; grabCut( img, mask, rect, bgdModel, fgdModel, 0, GC_INIT_WITH_RECT ); bgdModel.copyTo(exp_bgdModel);