Fixed several issues found by static analysis tools
authorMaksim Shabunin <maksim.shabunin@gmail.com>
Mon, 23 Jul 2018 14:22:47 +0000 (17:22 +0300)
committerMaksim Shabunin <maksim.shabunin@gmail.com>
Mon, 23 Jul 2018 14:22:47 +0000 (17:22 +0300)
modules/core/include/opencv2/core/types.hpp
modules/core/include/opencv2/core/types_c.h
modules/core/src/array.cpp
modules/core/src/persistence_types.cpp
modules/dnn/src/layers/resize_layer.cpp
modules/imgproc/src/filter.cpp
modules/imgproc/src/undistort.cpp
modules/videoio/src/cap_mjpeg_encoder.cpp

index 5037434..63232e3 100644 (file)
@@ -859,6 +859,13 @@ public:
     */
     TermCriteria(int type, int maxCount, double epsilon);
 
+    inline bool isValid() const
+    {
+        const bool isCount = (type & COUNT) && maxCount > 0;
+        const bool isEps = (type & EPS) && !cvIsNaN(epsilon);
+        return isCount || isEps;
+    }
+
     int type; //!< the type of termination criteria: COUNT, EPS or COUNT + EPS
     int maxCount; //!< the maximum number of iterations/elements
     double epsilon; //!< the desired accuracy
index 81b24f0..7e384a5 100644 (file)
@@ -629,7 +629,6 @@ CV_INLINE int cvIplDepth( int type )
 #define CV_TYPE_NAME_MATND    "opencv-nd-matrix"
 
 #define CV_MAX_DIM            32
-#define CV_MAX_DIM_HEAP       1024
 
 /**
   @deprecated consider using cv::Mat instead
index 11e9868..45e6ee8 100644 (file)
@@ -1725,8 +1725,8 @@ cvPtr1D( const CvArr* arr, int idx, int* _type )
         else
         {
             int i, n = m->dims;
-            CV_DbgAssert( n <= CV_MAX_DIM_HEAP );
-            int _idx[CV_MAX_DIM_HEAP];
+            CV_DbgAssert( n <= CV_MAX_DIM );
+            int _idx[CV_MAX_DIM];
 
             for( i = n - 1; i >= 0; i-- )
             {
index d5732a7..7ef115b 100644 (file)
@@ -302,7 +302,7 @@ static void* icvReadSparseMat( CvFileStorage* fs, CvFileNode* node )
     CvFileNode* sizes_node;
     CvSeqReader reader;
     CvSeq* elements;
-    int sizes[CV_MAX_DIM_HEAP], dims, elem_type, cn;
+    int sizes[CV_MAX_DIM], dims, elem_type, cn;
     int i;
 
     sizes_node = cvGetFileNodeByName( fs, node, "sizes" );
@@ -327,7 +327,7 @@ static void* icvReadSparseMat( CvFileStorage* fs, CvFileNode* node )
     mat = cvCreateSparseMat( dims, sizes, elem_type );
 
     cn = CV_MAT_CN(elem_type);
-    int idx[CV_MAX_DIM_HEAP];
+    int idx[CV_MAX_DIM];
     elements = data->data.seq;
     cvStartReadRawData( fs, data, &reader );
 
index b262066..78362da 100644 (file)
@@ -14,7 +14,7 @@ namespace cv { namespace dnn {
 class ResizeLayerImpl : public ResizeLayer
 {
 public:
-    ResizeLayerImpl(const LayerParams& params) : scaleWidth(0), scaleHeight(0)
+    ResizeLayerImpl(const LayerParams& params) : zoomFactorWidth(0), zoomFactorHeight(0), scaleWidth(0), scaleHeight(0)
     {
         setParamsFrom(params);
         outWidth = params.get<float>("width", 0);
index a086615..79c752b 100644 (file)
@@ -4284,7 +4284,7 @@ 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 (esz == 0
+    if (esz == 0 || src_step == 0
         || (src_offset % src_step) % esz != 0
         || (!doubleSupport && (sdepth == CV_64F || ddepth == CV_64F))
         || !(borderType == BORDER_CONSTANT
index d083ceb..dc71bc4 100644 (file)
@@ -370,6 +370,7 @@ static void cvUndistortPointsInternal( const CvMat* _src, CvMat* _dst, const CvM
                    const CvMat* _distCoeffs,
                    const CvMat* matR, const CvMat* matP, cv::TermCriteria criteria)
 {
+    CV_Assert(criteria.isValid());
     double A[3][3], RR[3][3], k[14]={0,0,0,0,0,0,0,0,0,0,0,0,0,0};
     CvMat matA=cvMat(3, 3, CV_64F, A), _Dk;
     CvMat _RR=cvMat(3, 3, CV_64F, RR);
index fb1ded4..b3d9b8f 100644 (file)
@@ -158,8 +158,9 @@ public:
         data.resize(size);
     }
 
-    void put(unsigned bits, int len)
+    inline void put_bits(unsigned bits, int len)
     {
+        CV_Assert(len >=0 && len < 32);
         if((m_pos == (data.size() - 1) && len > bits_free) || m_pos == data.size())
         {
             resize(int(2*data.size()));
@@ -182,6 +183,12 @@ public:
         }
     }
 
+    inline void put_val(int val, const unsigned * table)
+    {
+        unsigned code = table[(val) + 2];
+        put_bits(code >> 8, (int)(code & 255));
+    }
+
     void finish()
     {
         if(bits_free == 32)
@@ -1188,13 +1195,6 @@ public:
     void operator()( const cv::Range& range ) const CV_OVERRIDE
     {
         const int CAT_TAB_SIZE = 4096;
-        unsigned code = 0;
-
-#define JPUT_BITS(val, bits) output_buffer.put(val, bits)
-
-#define JPUT_HUFF(val, table) \
-    code = table[(val) + 2]; \
-    JPUT_BITS(code >> 8, (int)(code & 255))
 
         int x, y;
         int i, j;
@@ -1300,8 +1300,8 @@ public:
                             int cat = cat_table[val + CAT_TAB_SIZE];
 
                             //CV_Assert( cat <= 11 );
-                            JPUT_HUFF( cat, huff_dc_tab[is_chroma] );
-                            JPUT_BITS( val - (val < 0 ? 1 : 0), cat );
+                            output_buffer.put_val(cat, huff_dc_tab[is_chroma] );
+                            output_buffer.put_bits( val - (val < 0 ? 1 : 0), cat );
                         }
 
                         for( j = 1; j < 64; j++ )
@@ -1316,15 +1316,15 @@ public:
                             {
                                 while( run >= 16 )
                                 {
-                                    JPUT_HUFF( 0xF0, htable ); // encode 16 zeros
+                                    output_buffer.put_val( 0xF0, htable ); // encode 16 zeros
                                     run -= 16;
                                 }
 
                                 {
                                     int cat = cat_table[val + CAT_TAB_SIZE];
                                     //CV_Assert( cat <= 10 );
-                                    JPUT_HUFF( cat + run*16, htable );
-                                    JPUT_BITS( val - (val < 0 ? 1 : 0), cat );
+                                    output_buffer.put_val( cat + run*16, htable );
+                                    output_buffer.put_bits( val - (val < 0 ? 1 : 0), cat );
                                 }
 
                                 run = 0;
@@ -1333,7 +1333,7 @@ public:
 
                         if( run )
                         {
-                            JPUT_HUFF( 0x00, htable ); // encode EOB
+                            output_buffer.put_val( 0x00, htable ); // encode EOB
                         }
                     }
                 }