checks and fixes from static code analyzers results
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Tue, 1 Sep 2020 22:18:20 +0000 (22:18 +0000)
committerAlexander Alekhin <alexander.alekhin@intel.com>
Wed, 2 Sep 2020 18:59:34 +0000 (21:59 +0300)
13 files changed:
modules/calib3d/src/calibinit.cpp
modules/calib3d/src/calibration.cpp
modules/calib3d/src/fundam.cpp
modules/calib3d/src/usac/estimator.cpp
modules/calib3d/src/usac/fundamental_solver.cpp
modules/calib3d/src/usac/local_optimization.cpp
modules/calib3d/src/usac/sampler.cpp
modules/dnn/src/layers/convolution_layer.cpp
modules/dnn/src/ocl4dnn/include/ocl4dnn.hpp
modules/flann/include/opencv2/flann/result_set.h
modules/flann/include/opencv2/flann/timer.h
modules/highgui/src/window_w32.cpp
modules/objdetect/src/cascadedetect_convert.cpp

index 3ba631e..d763013 100644 (file)
@@ -791,6 +791,7 @@ int ChessBoardDetector::orderFoundConnectedQuads(std::vector<ChessBoardQuad*>& q
 
         for (int i = 0; i < 4; i++)
         {
+            CV_DbgAssert(q);
             ChessBoardQuad *neighbor = q->neighbors[i];
             switch(i)   // adjust col, row for this quad
             {           // start at top left, go clockwise
@@ -1271,6 +1272,7 @@ int ChessBoardDetector::cleanFoundConnectedQuads(std::vector<ChessBoardQuad*>& q
         for (int i = 0; i < quad_count; ++i)
         {
             ChessBoardQuad *q = quad_group[i];
+            CV_DbgAssert(q);
             for (int j = 0; j < 4; ++j)
             {
                 if (q->neighbors[j] == q0)
@@ -1328,6 +1330,7 @@ void ChessBoardDetector::findConnectedQuads(std::vector<ChessBoardQuad*>& out_gr
             stack.pop();
             for (int k = 0; k < 4; k++ )
             {
+                CV_DbgAssert(q);
                 ChessBoardQuad *neighbor = q->neighbors[k];
                 if (neighbor && neighbor->count > 0 && neighbor->group_idx < 0 )
                 {
@@ -1716,6 +1719,7 @@ void ChessBoardDetector::findQuadNeighbors()
                     int k = 0;
                     for (; k < 4; k++ )
                     {
+                        CV_DbgAssert(q);
                         if (!q->neighbors[k])
                         {
                             if (normL2Sqr<float>(closest_corner.pt - q->corners[k]->pt) < min_dist)
@@ -2090,6 +2094,7 @@ void drawChessboardCorners( InputOutputArray image, Size patternSize,
         return;
     Mat corners = _corners.getMat();
     const Point2f* corners_data = corners.ptr<Point2f>(0);
+    CV_DbgAssert(corners_data);
     int nelems = corners.checkVector(2, CV_32F, true);
     CV_Assert(nelems >= 0);
 
index d8ca4ca..a84e49b 100644 (file)
@@ -1052,9 +1052,9 @@ CV_IMPL void cvFindExtrinsicCameraParams2( const CvMat* objectPoints,
 
     int i, count;
     double a[9], ar[9]={1,0,0,0,1,0,0,0,1}, R[9];
-    double MM[9], U[9], V[9], W[3];
+    double MM[9] = { 0 }, U[9] = { 0 }, V[9] = { 0 }, W[3] = { 0 };
     cv::Scalar Mc;
-    double param[6];
+    double param[6] = { 0 };
     CvMat matA = cvMat( 3, 3, CV_64F, a );
     CvMat _Ar = cvMat( 3, 3, CV_64F, ar );
     CvMat matR = cvMat( 3, 3, CV_64F, R );
@@ -1274,8 +1274,9 @@ CV_IMPL void cvInitIntrinsicParams2D( const CvMat* objectPoints,
     CvMat matH = cvMat( 3, 3, CV_64F, H );
     CvMat _f = cvMat( 2, 1, CV_64F, f );
 
-    assert( CV_MAT_TYPE(npoints->type) == CV_32SC1 &&
-            CV_IS_MAT_CONT(npoints->type) );
+    CV_Assert(npoints);
+    CV_Assert(CV_MAT_TYPE(npoints->type) == CV_32SC1);
+    CV_Assert(CV_IS_MAT_CONT(npoints->type));
     nimages = npoints->rows + npoints->cols - 1;
 
     if( (CV_MAT_TYPE(objectPoints->type) != CV_32FC3 &&
@@ -1296,6 +1297,9 @@ CV_IMPL void cvInitIntrinsicParams2D( const CvMat* objectPoints,
     // extract vanishing points in order to obtain initial value for the focal length
     for( i = 0, pos = 0; i < nimages; i++, pos += ni )
     {
+        CV_DbgAssert(npoints->data.i);
+        CV_DbgAssert(matA && matA->data.db);
+        CV_DbgAssert(_b && _b->data.db);
         double* Ap = matA->data.db + i*4;
         double* bp = _b->data.db + i*2;
         ni = npoints->data.i[i];
@@ -1306,6 +1310,7 @@ CV_IMPL void cvInitIntrinsicParams2D( const CvMat* objectPoints,
         cvGetCols( imagePoints, &_m, pos, pos + ni );
 
         cvFindHomography( &matM, &_m, &matH );
+        CV_DbgAssert(_allH && _allH->data.db);
         memcpy( _allH->data.db + i*9, H, sizeof(H) );
 
         H[0] -= H[6]*a[2]; H[1] -= H[7]*a[2]; H[2] -= H[8]*a[2];
@@ -4145,6 +4150,7 @@ static void adjust3rdMatrix(InputArrayOfArrays _imgpt1_0,
 
     double y1_ = 0, y2_ = 0, y1y1_ = 0, y1y2_ = 0;
     size_t n = imgpt1.size();
+    CV_DbgAssert(n > 0);
 
     for( size_t i = 0; i < n; i++ )
     {
index 0e38eb9..4151ca4 100644 (file)
@@ -911,6 +911,7 @@ cv::Mat cv::findFundamentalMat( InputArray points1, InputArray points2,
                         OutputArray mask, const UsacParams &params) {
     Ptr<usac::Model> model;
     setParameters(model, usac::EstimationMethod::Fundamental, params, mask.needed());
+    CV_Assert(model);
     Ptr<usac::RansacOutput> ransac_output;
     if (usac::run(model, points1, points2, model->getRandomGeneratorState(),
             ransac_output, noArray(), noArray(), noArray(), noArray())) {
index f7af9ee..46c9d4c 100644 (file)
@@ -239,8 +239,14 @@ private:
     float minv11, minv12, minv13, minv21, minv22, minv23, minv31, minv32, minv33;
     std::vector<float> errors;
 public:
-    explicit ReprojectedErrorSymmetricImpl (const Mat &points_) :
-            points_mat(&points_), points ((float *) points_.data), errors(points_.rows) {}
+    explicit ReprojectedErrorSymmetricImpl (const Mat &points_)
+        : points_mat(&points_), points ((float *) points_.data)
+        , m11(0), m12(0), m13(0), m21(0), m22(0), m23(0), m31(0), m32(0), m33(0)
+        , minv11(0), minv12(0), minv13(0), minv21(0), minv22(0), minv23(0), minv31(0), minv32(0), minv33(0)
+        , errors(points_.rows)
+    {
+        CV_DbgAssert(points);
+    }
 
     inline void setModelParameters (const Mat &model) override {
         const auto * const m = (double *) model.data;
@@ -298,7 +304,12 @@ private:
     std::vector<float> errors;
 public:
     explicit ReprojectedErrorForwardImpl (const Mat &points_)
-        : points_mat(&points_), points ((float *)points_.data), errors(points_.rows) {}
+        : points_mat(&points_), points ((float *)points_.data)
+        , m11(0), m12(0), m13(0), m21(0), m22(0), m23(0), m31(0), m32(0), m33(0)
+        , errors(points_.rows)
+    {
+        CV_DbgAssert(points);
+    }
 
     inline void setModelParameters (const Mat &model) override {
         const auto * const m = (double *) model.data;
@@ -342,8 +353,13 @@ private:
     float m11, m12, m13, m21, m22, m23, m31, m32, m33;
     std::vector<float> errors;
 public:
-    explicit SampsonErrorImpl (const Mat &points_) :
-            points_mat(&points_), points ((float *) points_.data), errors(points_.rows) {}
+    explicit SampsonErrorImpl (const Mat &points_)
+        : points_mat(&points_), points ((float *) points_.data)
+        , m11(0), m12(0), m13(0), m21(0), m22(0), m23(0), m31(0), m32(0), m33(0)
+        , errors(points_.rows)
+    {
+        CV_DbgAssert(points);
+    }
 
     inline void setModelParameters (const Mat &model) override {
         const auto * const m = (double *) model.data;
@@ -404,8 +420,13 @@ private:
     float m11, m12, m13, m21, m22, m23, m31, m32, m33;
     std::vector<float> errors;
 public:
-    explicit SymmetricGeometricDistanceImpl (const Mat &points_) :
-            points_mat(&points_), points ((float *) points_.data), errors(points_.rows) {}
+    explicit SymmetricGeometricDistanceImpl (const Mat &points_)
+        : points_mat(&points_), points ((float *) points_.data)
+        , m11(0), m12(0), m13(0), m21(0), m22(0), m23(0), m31(0), m32(0), m33(0)
+        , errors(points_.rows)
+    {
+        CV_DbgAssert(points);
+    }
 
     inline void setModelParameters (const Mat &model) override {
         const auto * const m = (double *) model.data;
@@ -458,8 +479,14 @@ private:
     float p11, p12, p13, p14, p21, p22, p23, p24, p31, p32, p33, p34;
     std::vector<float> errors;
 public:
-    explicit ReprojectionErrorPmatrixImpl (const Mat &points_) :
-        points_mat(&points_), points ((float *) points_.data), errors(points_.rows) {}
+    explicit ReprojectionErrorPmatrixImpl (const Mat &points_)
+        : points_mat(&points_), points ((float *) points_.data)
+        , p11(0), p12(0), p13(0), p14(0), p21(0), p22(0), p23(0), p24(0), p31(0), p32(0), p33(0), p34(0)
+        , errors(points_.rows)
+    {
+        CV_DbgAssert(points);
+    }
+
 
     inline void setModelParameters (const Mat &model) override {
         const auto * const p = (double *) model.data;
@@ -512,8 +539,13 @@ private:
     float m11, m12, m13, m21, m22, m23;
     std::vector<float> errors;
 public:
-    explicit ReprojectedDistanceAffineImpl (const Mat &points_) :
-            points_mat(&points_), points ((float*)points_.data), errors(points_.rows) {}
+    explicit ReprojectedDistanceAffineImpl (const Mat &points_)
+        : points_mat(&points_), points ((float *) points_.data)
+        , m11(0), m12(0), m13(0), m21(0), m22(0), m23(0)
+        , errors(points_.rows)
+    {
+        CV_DbgAssert(points);
+    }
 
     inline void setModelParameters (const Mat &model) override {
         const auto * const m = (double *) model.data;
index 5641350..5048aa0 100644 (file)
@@ -79,8 +79,8 @@ public:
         }
 
         // OpenCV:
-        double c[4], r[3];
-        double t0, t1, t2;
+        double c[4] = { 0 }, r[3] = { 0 };
+        double t0 = 0, t1 = 0, t2 = 0;
         Mat_<double> coeffs (1, 4, c);
         Mat_<double> roots (1, 3, r);
 
@@ -158,7 +158,10 @@ private:
     const float * const points;
 public:
     explicit FundamentalMinimalSolver8ptsImpl (const Mat &points_) :
-            points_mat (&points_), points ((float*) points_.data) {}
+            points_mat (&points_), points ((float*) points_.data)
+    {
+        CV_DbgAssert(points);
+    }
 
     int estimate (const std::vector<int> &sample, std::vector<Mat> &models) const override {
         const int m = 8, n = 9; // rows, cols
index f680337..c55aa37 100644 (file)
@@ -217,9 +217,11 @@ public:
          const Ptr<RandomGenerator> &lo_sampler_, int pts_size,
          double threshold_, bool is_iterative_, int lo_iter_sample_size_,
          int lo_inner_iterations_=10, int lo_iter_max_iterations_=5,
-         double threshold_multiplier_=4) : estimator (estimator_), quality (quality_),
-                           lo_sampler (lo_sampler_) {
-
+         double threshold_multiplier_=4)
+        : estimator (estimator_), quality (quality_), lo_sampler (lo_sampler_)
+        , lo_iter_sample_size(0)
+        , new_threshold(0), threshold_step(0)
+    {
         lo_inner_max_iterations = lo_inner_iterations_;
         lo_iter_max_iterations = lo_iter_max_iterations_;
 
index 42fb1e7..d045e1f 100644 (file)
@@ -14,11 +14,13 @@ namespace cv { namespace usac {
 class UniformSamplerImpl : public UniformSampler {
 private:
     std::vector<int> points_random_pool;
-    int sample_size, random_pool_size, points_size = 0;
+    int sample_size, points_size = 0;
     RNG rng;
 public:
 
-    UniformSamplerImpl (int state, int sample_size_, int points_size_) : rng(state) {
+    UniformSamplerImpl (int state, int sample_size_, int points_size_)
+        : rng(state)
+    {
         sample_size = sample_size_;
         setPointsSize (points_size_);
     }
@@ -26,7 +28,7 @@ public:
         setPointsSize(points_size_);
     }
     void generateSample (std::vector<int> &sample) override {
-        random_pool_size = points_size; // random points of entire range
+        int random_pool_size = points_size; // random points of entire range
         for (int i = 0; i < sample_size; i++) {
             // get random point index
             const int array_random_index = rng.uniform(0, random_pool_size);
index ae7501e..d758457 100644 (file)
@@ -752,6 +752,8 @@ public:
         std::vector<size_t> dims = ieInpNode->get_shape();
         CV_Assert(dims.size() == 4 || dims.size() == 5);
         std::shared_ptr<ngraph::Node> ieWeights = nodes.size() > 1 ? nodes[1].dynamicCast<InfEngineNgraphNode>()->node : nullptr;
+        if (nodes.size() > 1)
+            CV_Assert(ieWeights);  // dynamic_cast should not fail
         const int inpCn = dims[1];
         const int inpGroupCn = nodes.size() > 1 ? ieWeights->get_shape()[1] : blobs[0].size[1];
         const int group = inpCn / inpGroupCn;
@@ -859,6 +861,7 @@ public:
         ParallelConv()
             : input_(0), weights_(0), output_(0), ngroups_(0), nstripes_(0),
               biasvec_(0), reluslope_(0), activ_(0), is1x1_(false), useAVX(false), useAVX2(false), useAVX512(false)
+            , blk_size_cn(0)
         {}
 
         static void run( const Mat& input, Mat& output, const Mat& weights,
index eda2e83..8de7ba2 100644 (file)
@@ -433,7 +433,7 @@ class OCL4DNNInnerProduct
                      UMat& top_data);
     private:
         OCL4DNNInnerProductConfig config_;
-        int32_t axis_;
+        //int32_t axis_;
         int32_t num_output_;
         int32_t M_;
         int32_t N_;
index 02f827a..47ad231 100644 (file)
@@ -160,7 +160,8 @@ class KNNResultSet : public ResultSet<DistanceType>
     DistanceType worst_distance_;
 
 public:
-    KNNResultSet(int capacity_) : capacity(capacity_), count(0)
+    KNNResultSet(int capacity_)
+        : indices(NULL), dists(NULL), capacity(capacity_), count(0), worst_distance_(0)
     {
     }
 
@@ -186,6 +187,8 @@ public:
 
     void addPoint(DistanceType dist, int index) CV_OVERRIDE
     {
+        CV_DbgAssert(indices);
+        CV_DbgAssert(dists);
         if (dist >= worst_distance_) return;
         int i;
         for (i = count; i > 0; --i) {
index 73795aa..7dc50a4 100644 (file)
@@ -60,6 +60,7 @@ public:
      * Constructor.
      */
     StartStopTimer()
+        : startTime(0)
     {
         reset();
     }
index e9a2116..267d9eb 100644 (file)
@@ -442,6 +442,7 @@ icvSaveWindowPos( const char* name, CvRect rect )
 
 CvRect cvGetWindowRect_W32(const char* name)
 {
+    RECT rect = { 0 };
     CvRect result = cvRect(-1, -1, -1, -1);
 
     CV_FUNCNAME( "cvGetWindowRect_W32" );
@@ -456,7 +457,6 @@ CvRect cvGetWindowRect_W32(const char* name)
     if (!window)
         EXIT; // keep silence here
 
-    RECT rect;
     GetClientRect(window->hwnd, &rect);
     {
     POINT pt = {rect.left, rect.top};
@@ -526,7 +526,7 @@ void cvSetModeWindow_W32( const char* name, double prop_value)//Yannick Verdie
         if (window->status==CV_WINDOW_NORMAL && prop_value==CV_WINDOW_FULLSCREEN)
         {
             //save dimension
-            RECT rect;
+            RECT rect = { 0 };
             GetWindowRect(window->frame, &rect);
             CvRect RectCV = cvRect(rect.left, rect.top,rect.right - rect.left, rect.bottom - rect.top);
             icvSaveWindowPos(window->name,RectCV );
@@ -1119,7 +1119,7 @@ static void icvScreenToClient( HWND hwnd, RECT* rect )
 static RECT icvCalcWindowRect( CvWindow* window )
 {
     const int gutter = 1;
-    RECT crect, trect, rect;
+    RECT crect = { 0 }, trect = { 0 } , rect = { 0 };
 
     assert(window);
 
@@ -1175,7 +1175,7 @@ static bool icvGetBitmapData( CvWindow* window, SIZE* size, int* channels, void*
 
 static void icvUpdateWindowPos( CvWindow* window )
 {
-    RECT rect;
+    RECT rect = { 0 };
     assert(window);
 
     if( (window->flags & CV_WINDOW_AUTOSIZE) && window->image )
@@ -1188,7 +1188,7 @@ static void icvUpdateWindowPos( CvWindow* window )
         // toolbar may resize too
         for(i = 0; i < (window->toolbar.toolbar ? 2 : 1); i++)
         {
-            RECT rmw, rw = icvCalcWindowRect(window );
+            RECT rmw = { 0 }, rw = icvCalcWindowRect(window );
             MoveWindow(window->hwnd, rw.left, rw.top,
                 rw.right - rw.left, rw.bottom - rw.top, FALSE);
             GetClientRect(window->hwnd, &rw);
@@ -1294,7 +1294,7 @@ CV_IMPL void cvResizeWindow(const char* name, int width, int height )
 
     int i;
     CvWindow* window;
-    RECT rmw, rw, rect;
+    RECT rmw = { 0 }, rw = { 0 }, rect = { 0 };
 
     if( !name )
         CV_ERROR( CV_StsNullPtr, "NULL name" );
@@ -1333,7 +1333,7 @@ CV_IMPL void cvMoveWindow( const char* name, int x, int y )
     __BEGIN__;
 
     CvWindow* window;
-    RECT rect;
+    RECT rect = { 0 };
 
     if( !name )
         CV_ERROR( CV_StsNullPtr, "NULL name" );
@@ -1373,7 +1373,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
         if( !(window->flags & CV_WINDOW_AUTOSIZE) )
         {
             MINMAXINFO* minmax = (MINMAXINFO*)lParam;
-            RECT rect;
+            RECT rect = { 0 };
             LRESULT retval = DefWindowProc(hwnd, uMsg, wParam, lParam);
 
             minmax->ptMinTrackSize.y = 100;
@@ -1396,7 +1396,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
             // Update the toolbar pos/size
             if(window->toolbar.toolbar)
             {
-                RECT rect;
+                RECT rect = { 0 };
                 GetWindowRect(window->toolbar.toolbar, &rect);
                 MoveWindow(window->toolbar.toolbar, 0, 0, pos->cx, rect.bottom - rect.top, TRUE);
             }
@@ -1412,7 +1412,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
           // Snap window to screen edges with multi-monitor support. // Adi Shavit
           LPWINDOWPOS pos = (LPWINDOWPOS)lParam;
 
-          RECT rect;
+          RECT rect = { 0 };
           GetWindowRect(window->frame, &rect);
 
           HMONITOR hMonitor;
@@ -1463,7 +1463,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
           pt.y = GET_Y_LPARAM( lParam );
           ::ScreenToClient(hwnd, &pt); // Convert screen coordinates to client coordinates.
 
-          RECT rect;
+          RECT rect = { 0 };
           GetClientRect( window->hwnd, &rect );
 
           SIZE size = {0,0};
@@ -1490,7 +1490,7 @@ MainWindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
 
     case WM_ERASEBKGND:
         {
-            RECT cr, tr, wrc;
+            RECT cr = { 0 }, tr = { 0 }, wrc = { 0 };
             HRGN rgn, rgn1, rgn2;
             int ret;
             HDC hdc = (HDC)wParam;
@@ -1670,7 +1670,7 @@ static LRESULT CALLBACK HighGUIProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
                 window->on_mouse( event, pt.x, pt.y, flags, window->on_mouse_param );
             } else {
                 // Full window is displayed using different size. Scale coordinates to match underlying positions.
-                RECT rect;
+                RECT rect = { 0 };
                 SIZE size = {0, 0};
 
                 GetClientRect( window->hwnd, &rect );
@@ -1730,7 +1730,7 @@ static LRESULT CALLBACK HighGUIProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM
             }
             else
             {
-                RECT rect;
+                RECT rect = { 0 };
                 GetClientRect(window->hwnd, &rect);
                 StretchBlt( hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top,
                             window->dc, 0, 0, size.cx, size.cy, SRCCOPY );
@@ -1887,7 +1887,7 @@ static LRESULT CALLBACK HGToolbarProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPAR
 
                 for( ; trackbar != 0; trackbar = trackbar->next )
                 {
-                    RECT rect;
+                    RECT rect = { 0 };
                     SendMessage(window->toolbar.toolbar, TB_GETITEMRECT,
                                (WPARAM)trackbar->id, (LPARAM)&rect);
                     MoveWindow(trackbar->hwnd, rect.left + HG_BUDDY_WIDTH, rect.top,
@@ -2112,7 +2112,7 @@ icvCreateTrackbar( const char* trackbar_name, const char* window_name,
     {
         TBBUTTON tbs = {};
         TBBUTTONINFO tbis = {};
-        RECT rect;
+        RECT rect = { 0 };
         int bcount;
         int len = (int)strlen( trackbar_name );
 
index 2ca0d0d..2118aab 100644 (file)
@@ -108,6 +108,8 @@ struct HaarClassifier
 
 struct HaarStageClassifier
 {
+    HaarStageClassifier() : threshold(0) {}
+
     double threshold;
     std::vector<HaarClassifier> weaks;
 };